git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH 2/6] Add remote.name.timeout to configure an IO timeout

From
Shawn O. Pearce <spearce@spearce.org>
Date
Jun 19, 2009, 21:27 UTC
Message-ID
<1245446875-31102-3-git-send-email-spearce@spearce.org>
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
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 3 of 11 in “Add timeouts to network IO”
  1. 0/6 Add timeouts to network IOShawn O. Pearce, Jun 19, 2009
  2. 1/6 Create input and output streams that have timeoutsShawn O. Pearce, Jun 19, 2009
  3. 2/6 Add remote.name.timeout to configure an IO timeoutShawn O. Pearce, Jun 19, 2009
  4. 3/6 Add timeouts to smart transport protocol clientsShawn O. Pearce, Jun 19, 2009
  5. 4/6 Add timeouts to smart transport protocol serversShawn O. Pearce, Jun 19, 2009
  6. 5/6 Add timeouts to anonymous git:// daemonShawn O. Pearce, Jun 19, 2009
  7. 6/6 Add --timeout command line optionsShawn O. Pearce, Jun 19, 2009
  8. Robin RosenbergJun 20, 2009
  9. Shawn O. PearceJun 20, 2009
  10. Robin RosenbergJun 22, 2009
  11. 1/6 Create input and output streams that have timeoutsShawn O. Pearce, Jun 23, 2009

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.