git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Shawn O. Pearce" <spearce@spearce.org>
To: Robin Rosenberg <robin.rosenberg@dewire.com>
Cc: git@vger.kernel.org
Subject: [JGIT PATCH 2/6] Add remote.name.timeout to configure an IO timeout
Date: Fri, 19 Jun 2009 14:27:51 -0700	[thread overview]
Message-ID: <1245446875-31102-3-git-send-email-spearce@spearce.org> (raw)
In-Reply-To: <1245446875-31102-2-git-send-email-spearce@spearce.org>

An IO timeout can be useful if the remote peer stops responding,
and the user wants the application to abort rather than block
indefinitely waiting for more input.

This is a JGit specific extension to the standard remote format.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../spearce/jgit/transport/RemoteConfigTest.java   |   26 ++++++++++++++++
 .../org/spearce/jgit/transport/RemoteConfig.java   |   31 ++++++++++++++++++++
 2 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RemoteConfigTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RemoteConfigTest.java
index 6b72b64..3965bdb 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RemoteConfigTest.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/transport/RemoteConfigTest.java
@@ -78,6 +78,7 @@ writeConfig("[remote \"spearce\"]\n"
 		assertNotNull(rc.getFetchRefSpecs());
 		assertNotNull(rc.getPushRefSpecs());
 		assertNotNull(rc.getTagOpt());
+		assertEquals(0, rc.getTimeout());
 		assertSame(TagOpt.AUTO_FOLLOW, rc.getTagOpt());
 
 		assertEquals(1, allURIs.size());
@@ -423,4 +424,29 @@ checkFile(new File(db.getDirectory(), "config"), "[core]\n"
 				+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
 				+ "\ttagopt = --tags\n");
 	}
+
+	public void testSimpleTimeout() throws Exception {
+		writeConfig("[remote \"spearce\"]\n"
+				+ "url = http://www.spearce.org/egit.git\n"
+				+ "fetch = +refs/heads/*:refs/remotes/spearce/*\n"
+				+ "timeout = 12\n");
+		final RemoteConfig rc = new RemoteConfig(db.getConfig(), "spearce");
+		assertEquals(12, rc.getTimeout());
+	}
+
+	public void testSaveTimeout() throws Exception {
+		final RemoteConfig rc = new RemoteConfig(db.getConfig(), "origin");
+		rc.addURI(new URIish("/some/dir"));
+		rc.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/"
+				+ rc.getName() + "/*"));
+		rc.setTimeout(60);
+		rc.update(db.getConfig());
+		db.getConfig().save();
+
+		checkFile(new File(db.getDirectory(), "config"), "[core]\n"
+				+ "\trepositoryformatversion = 0\n" + "\tfilemode = true\n"
+				+ "[remote \"origin\"]\n" + "\turl = /some/dir\n"
+				+ "\tfetch = +refs/heads/*:refs/remotes/origin/*\n"
+				+ "\ttimeout = 60\n");
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/RemoteConfig.java b/org.spearce.jgit/src/org/spearce/jgit/transport/RemoteConfig.java
index 519a8a5..a621dc4 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/RemoteConfig.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/RemoteConfig.java
@@ -70,6 +70,8 @@
 
 	private static final String KEY_MIRROR = "mirror";
 
+	private static final String KEY_TIMEOUT = "timeout";
+
 	private static final boolean DEFAULT_MIRROR = false;
 
 	/** Default value for {@link #getUploadPack()} if not specified. */
@@ -120,6 +122,8 @@
 
 	private boolean mirror;
 
+	private int timeout;
+
 	/**
 	 * Parse a remote block from an existing configuration file.
 	 * <p>
@@ -170,6 +174,7 @@ public RemoteConfig(final RepositoryConfig rc, final String remoteName)
 		val = rc.getString(SECTION, name, KEY_TAGOPT);
 		tagopt = TagOpt.fromOption(val);
 		mirror = rc.getBoolean(SECTION, name, KEY_MIRROR, DEFAULT_MIRROR);
+		timeout = rc.getInt(SECTION, name, KEY_TIMEOUT, 0);
 	}
 
 	/**
@@ -200,6 +205,7 @@ public void update(final RepositoryConfig rc) {
 		set(rc, KEY_RECEIVEPACK, getReceivePack(), DEFAULT_RECEIVE_PACK);
 		set(rc, KEY_TAGOPT, getTagOpt().option(), TagOpt.AUTO_FOLLOW.option());
 		set(rc, KEY_MIRROR, mirror, DEFAULT_MIRROR);
+		set(rc, KEY_TIMEOUT, timeout, 0);
 	}
 
 	private void set(final RepositoryConfig rc, final String key,
@@ -218,6 +224,14 @@ private void set(final RepositoryConfig rc, final String key,
 			rc.setBoolean(SECTION, getName(), key, currentValue);
 	}
 
+	private void set(final RepositoryConfig rc, final String key,
+			final int currentValue, final int defaultValue) {
+		if (defaultValue == currentValue)
+			unset(rc, key);
+		else
+			rc.setInt(SECTION, getName(), key, currentValue);
+	}
+
 	private void unset(final RepositoryConfig rc, final String key) {
 		rc.unsetString(SECTION, getName(), key);
 	}
@@ -420,4 +434,21 @@ public boolean isMirror() {
 	public void setMirror(final boolean m) {
 		mirror = m;
 	}
+
+	/** @return timeout (in seconds) before aborting an IO operation. */
+	public int getTimeout() {
+		return timeout;
+	}
+
+	/**
+	 * Set the timeout before willing to abort an IO call.
+	 *
+	 * @param seconds
+	 *            number of seconds to wait (with no data transfer occurring)
+	 *            before aborting an IO read or write operation with this
+	 *            remote.  A timeout of 0 will block indefinitely.
+	 */
+	public void setTimeout(final int seconds) {
+		timeout = seconds;
+	}
 }
-- 
1.6.3.2.416.g04d0

  reply	other threads:[~2009-06-19 21:28 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-19 21:27 [JGIT PATCH 0/6] Add timeouts to network IO Shawn O. Pearce
2009-06-19 21:27 ` [JGIT PATCH 1/6] Create input and output streams that have timeouts Shawn O. Pearce
2009-06-19 21:27   ` Shawn O. Pearce [this message]
2009-06-19 21:27     ` [JGIT PATCH 3/6] Add timeouts to smart transport protocol clients Shawn O. Pearce
2009-06-19 21:27       ` [JGIT PATCH 4/6] Add timeouts to smart transport protocol servers Shawn O. Pearce
2009-06-19 21:27         ` [JGIT PATCH 5/6] Add timeouts to anonymous git:// daemon Shawn O. Pearce
2009-06-19 21:27           ` [JGIT PATCH 6/6] Add --timeout command line options Shawn O. Pearce
2009-06-20 22:28     ` [JGIT PATCH 2/6] Add remote.name.timeout to configure an IO timeout Robin Rosenberg
2009-06-20 22:54       ` Shawn O. Pearce
2009-06-22 21:09   ` [JGIT PATCH 1/6] Create input and output streams that have timeouts Robin Rosenberg
2009-06-23 16:41     ` [JGIT PATCH 1/6 v2] " Shawn O. Pearce

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1245446875-31102-3-git-send-email-spearce@spearce.org \
    --to=spearce@spearce.org \
    --cc=git@vger.kernel.org \
    --cc=robin.rosenberg@dewire.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).