我在python中编写了一些git命令,如果没有连接,偶尔git fetch
可能会挂起几个小时。有没有办法计算出来并报告失败?
答案 0 :(得分:0)
不,你不能,你需要使用包装器超时命令。
答案 1 :(得分:0)
我需要为此更改/设置超时时间
$ time git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
real 2m11.684s
user 0m0.002s
sys 0m0.005s
(2分11秒可能取决于我的sysctl设置)
并且由于这是stackoverflow,所以我必须按如下方式修补git
(而不是使用timeout
命令/包装器)来获得指定超时的这两种变体:
~/.gitconfig
指定的超时时间(以秒为单位):
[http]
connecttimeout = 10
所以
$ time git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Connection timed out after 10009 milliseconds
real 0m10.031s
user 0m0.000s
sys 0m0.004s
,以及一种通过环境覆盖~/.gitconfig
指定的超时的方法。 var:
$ time GIT_HTTP_CONNECT_TIMEOUT=2 git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Connection timed out after 2001 milliseconds
real 0m2.023s
user 0m0.000s
sys 0m0.004s
有问题的补丁(应用于最新的git master提交4c86140027f4a0d2caaa3ab4bd8bfc5ce3c11c8a日期:Wed Sep 18 11:55:13 2019 -0700)是:
diff --git a/http.c b/http.c
index 938b9e55af..c46737e48d 100644
--- a/http.c
+++ b/http.c
@@ -83,6 +83,7 @@ static const char *ssl_pinnedkey;
static const char *ssl_cainfo;
static long curl_low_speed_limit = -1;
static long curl_low_speed_time = -1;
+static long curl_connecttimeout = -1; // in seconds, see man 3 CURLOPT_CONNECTTIMEOUT, default(for me, depending on sysctl setting probably!) is 2min10sec
static int curl_ftp_no_epsv;
static const char *curl_http_proxy;
static const char *http_proxy_authmethod;
@@ -354,6 +355,10 @@ static int http_options(const char *var, const char *value, void *cb)
curl_low_speed_time = (long)git_config_int(var, value);
return 0;
}
+ if (!strcmp("http.connecttimeout", var)) { // overriden by env var GIT_HTTP_CONNECT_TIMEOUT
+ curl_connecttimeout = (long)git_config_int(var, value);
+ return 0;
+ }
if (!strcmp("http.noepsv", var)) {
curl_ftp_no_epsv = git_config_bool(var, value);
@@ -935,6 +940,11 @@ static CURL *get_curl_handle(void)
curl_easy_setopt(result, CURLOPT_LOW_SPEED_TIME,
curl_low_speed_time);
}
+ if (curl_connecttimeout >= 0) {
+ //-1 or any negative means don't set a timeout which means (for me, depending on sysctl settings, no doubt) 2min10sec eg. 130sec and quits like: fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
+ //0 means set timeout to 0 which practically doesn't set a timeout and is the same as using a negative value! aka 2min10sec and quits like for -1 (see above)
+ curl_easy_setopt(result, CURLOPT_CONNECTTIMEOUT, curl_connecttimeout); //10L = timeout in 10 seconds instead of 2min10s eg. `git fetch` when: fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out --- real 2m10.809s --- because git.xfce.org is down/not responding to ping! see $ man 3 CURLOPT_CONNECTTIMEOUT Setting a timeout quits like: fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
+ }
curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
#if LIBCURL_VERSION_NUM >= 0x071301
@@ -1061,6 +1071,7 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
{
char *low_speed_limit;
char *low_speed_time;
+ char *connecttimeout;
char *normalized_url;
struct urlmatch_config config = { STRING_LIST_INIT_DUP };
@@ -1151,6 +1162,9 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
low_speed_time = getenv("GIT_HTTP_LOW_SPEED_TIME");
if (low_speed_time != NULL)
curl_low_speed_time = strtol(low_speed_time, NULL, 10);
+ connecttimeout = getenv("GIT_HTTP_CONNECT_TIMEOUT"); // man 3 CURLOPT_CONNECTTIMEOUT ; can also be set in ~/.gitconfig as http.connecttimeout aka [http]\n\tconnecttimeout=10
+ if (connecttimeout != NULL)
+ curl_connecttimeout = strtol(connecttimeout, NULL, 10); //10 is base
if (curl_ssl_verify == -1)
curl_ssl_verify = 1;
@@ -1903,6 +1917,7 @@ static int http_request(const char *url,
curl_easy_setopt(slot->curl, CURLOPT_ENCODING, "");
curl_easy_setopt(slot->curl, CURLOPT_FAILONERROR, 0);
+
ret = run_one_slot(slot, &results);
if (options && options->content_type) {
其他:
覆盖~/.gitconfig
设置的超时,然后将超时设置为0(这意味着没有超时,因为CURLOPT_CONNECTTIMEOUT设置为0时libcurl的行为),或者根本没有设置任何超时(当前含义是相同的:无超时)因为git不会触摸/设置CURLOPT_CONNECTTIMEOUT),所以这些值都可以使用:0或任何负值,即。
$ time GIT_HTTP_CONNECT_TIMEOUT=0 git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
real 2m11.255s
user 0m0.000s
sys 0m0.004s
$ time GIT_HTTP_CONNECT_TIMEOUT=-1 git fetch
fatal: unable to access 'https://git.xfce.org/xfce/xfce4-panel/': Failed to connect to git.xfce.org port 443: Connection timed out
real 2m8.710s
user 0m0.000s
sys 0m0.006s
在archlinux上测试:
本地/卷曲7.66.0-1
本地/ git 2.23.0.r256.g4c86140027-1