# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1465849814 -3600
#      Mon Jun 13 21:30:14 2016 +0100
# Node ID 62b890496de5dc6b64febc2aebc9f5caeadb9201
# Parent  bb3d5c20eaf664cca201fc9273c1df2402f5c6fa
chg: make timeout adjustable

Before this patch, chg will give up when it cannot connect to the new server
within 10 seconds. If the host has high load during that time, 10 seconds
is not enough.

This patch makes it adjustable using the CHGTIMEOUT environment variable.

diff --git a/contrib/chg/README b/contrib/chg/README
--- a/contrib/chg/README
+++ b/contrib/chg/README
@@ -28,3 +28,5 @@
 
  * CHGDEBUG enables debug messages.
  * CHGSOCKNAME specifies the socket path of the background cmdserver.
+ * CHGTIMEOUT specifies how many seconds chg will wait before giving up
+   connecting to a cmdserver. If it is 0, chg will wait forever. Default: 10
diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -249,7 +249,13 @@
 	int pst = 0;
 
 	debugmsg("try connect to %s repeatedly", opts->sockname);
-	for (unsigned int i = 0; i < 10 * 100; i++) {
+
+	unsigned int timeoutsec = 10;  /* default: 10 seconds */
+	const char *timeoutenv = getenv("CHGTIMEOUT");
+	if (timeoutenv)
+		sscanf(timeoutenv, "%u", &timeoutsec);
+
+	for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) {
 		hgclient_t *hgc = hgc_open(opts->sockname);
 		if (hgc)
 			return hgc;