{"thread":{"id":"56324","subject":"[PATCH 1/2] add http.maxReceiveSpeed to limit git-receive-pack receiving speed","startedAt":"2021-08-19T13:18:26Z","lastAt":"2021-08-19T13:20:58Z","messageCount":2,"participants":["Xia XiaoWen"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"433129","messageId":"20210819131808.40759-1-chenan.xxw@alibaba-inc.com","threadId":"56324","inReplyTo":null,"subject":"[PATCH 1/2] add http.maxReceiveSpeed to limit git-receive-pack receiving speed","fromName":"Xia XiaoWen","fromEmail":"haoyurenzhuxia@gmail.com","sentAt":"2021-08-19T13:18:07Z","receivedAt":"2021-08-19T13:18:26Z","isPatch":true,"sender":{"key":"haoyurenzhuxia@gmail.com","avatar":null},"body":"Sometimes need to limit the receive speed of git `clone/fetch`\nbecause of the limited network bandwidth, otherwise will prevent\nother applications from using the network normally.\n\nAdd `http.maxReceiveSpeed` to limit `git-receive-pack` receiving\nspeed, Can be overridden by `GIT_HTTP_MAX_RECEIVE_SPEED` eivironment\nvariable.\n\nThe default is unlimited, same if the value is 0 or negative. The\ndefault unit is Bytes/s, common unit suffixes of k, m, or g are supported.\n\nthis configuration is valid for `clone`, `fetch`, `pull` commands of the\nhttps protocol, and only supports libcurl 7.15.5 and above.\n---\n http.c | 24 +++++++++++++++++++++++-\n 1 file changed, 23 insertions(+), 1 deletion(-)\n\ndiff --git a/http.c b/http.c\nindex 8119247149..12030cf3bc 100644\n--- a/http.c\n+++ b/http.c\n@@ -83,6 +83,9 @@ static const char *ssl_pinnedkey;\n static const char *ssl_cainfo;\n static long curl_low_speed_limit = -1;\n static long curl_low_speed_time = -1;\n+#if LIBCURL_VERSION_NUM >= 0x070f05\n+static ssize_t curl_max_receive_speed = -1;\n+#endif\n static int curl_ftp_no_epsv;\n static const char *curl_http_proxy;\n static const char *http_proxy_authmethod;\n@@ -361,7 +364,12 @@ static int http_options(const char *var, const char *value, void *cb)\n \t\tcurl_low_speed_time = (long)git_config_int(var, value);\n \t\treturn 0;\n \t}\n-\n+#if LIBCURL_VERSION_NUM >= 0x070f05\n+\tif (!strcmp(\"http.maxreceivespeed\", var)) {\n+\t\tcurl_max_receive_speed = git_config_ssize_t(var, value);\n+\t\treturn 0;\n+\t}\n+#endif\n \tif (!strcmp(\"http.noepsv\", var)) {\n \t\tcurl_ftp_no_epsv = git_config_bool(var, value);\n \t\treturn 0;\n@@ -974,6 +982,12 @@ static CURL *get_curl_handle(void)\n \t\t\t\t curl_low_speed_time);\n \t}\n \n+#if LIBCURL_VERSION_NUM >= 0x070f05\n+\tif (curl_max_receive_speed > 0)\n+\t\tcurl_easy_setopt(result, CURLOPT_MAX_RECV_SPEED_LARGE,\n+\t\t\t\t curl_max_receive_speed);\n+#endif\n+\n \tcurl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);\n #if LIBCURL_VERSION_NUM >= 0x071301\n \tcurl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);\n@@ -1105,6 +1119,9 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)\n {\n \tchar *low_speed_limit;\n \tchar *low_speed_time;\n+#if LIBCURL_VERSION_NUM >= 0x070f05\n+\tchar *max_receive_speed;\n+#endif\n \tchar *normalized_url;\n \tstruct urlmatch_config config = { STRING_LIST_INIT_DUP };\n \n@@ -1196,6 +1213,11 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)\n \tlow_speed_time = getenv(\"GIT_HTTP_LOW_SPEED_TIME\");\n \tif (low_speed_time != NULL)\n \t\tcurl_low_speed_time = strtol(low_speed_time, NULL, 10);\n+#if LIBCURL_VERSION_NUM >= 0x070f05\n+\tmax_receive_speed = getenv(\"GIT_HTTP_MAX_RECEIVE_SPEED\");\n+\tif (max_receive_speed && !git_parse_ssize_t(max_receive_speed, &curl_max_receive_speed))\n+\t\twarning(\"failed to parse GIT_HTTP_MAX_RECEIVE_SPEED: %s\", max_receive_speed);\n+#endif\n \n \tif (curl_ssl_verify == -1)\n \t\tcurl_ssl_verify = 1;\n-- \n2.28.1.49.gafcb914ae4.dirty.agit.6.3.1\n\n"},{"id":"433130","messageId":"20210819131808.40759-2-chenan.xxw@alibaba-inc.com","threadId":"56324","inReplyTo":"20210819131808.40759-1-chenan.xxw@alibaba-inc.com","subject":"[PATCH 2/2] negative values are ignored for http.maxreceivespeed","fromName":"Xia XiaoWen","fromEmail":"haoyurenzhuxia@gmail.com","sentAt":"2021-08-19T13:18:08Z","receivedAt":"2021-08-19T13:20:58Z","isPatch":true,"sender":{"key":"haoyurenzhuxia@gmail.com","avatar":null},"body":"---\n http.c | 6 ++++--\n 1 file changed, 4 insertions(+), 2 deletions(-)\n\ndiff --git a/http.c b/http.c\nindex 12030cf3bc..2e01ebf559 100644\n--- a/http.c\n+++ b/http.c\n@@ -84,7 +84,7 @@ static const char *ssl_cainfo;\n static long curl_low_speed_limit = -1;\n static long curl_low_speed_time = -1;\n #if LIBCURL_VERSION_NUM >= 0x070f05\n-static ssize_t curl_max_receive_speed = -1;\n+static ssize_t curl_max_receive_speed = 0;\n #endif\n static int curl_ftp_no_epsv;\n static const char *curl_http_proxy;\n@@ -983,7 +983,9 @@ static CURL *get_curl_handle(void)\n \t}\n \n #if LIBCURL_VERSION_NUM >= 0x070f05\n-\tif (curl_max_receive_speed > 0)\n+\tif (curl_max_receive_speed < 0)\n+\t\twarning(\"negative values are ignored for http.maxreceivespeed\");\n+\telse if (curl_max_receive_speed > 0)\n \t\tcurl_easy_setopt(result, CURLOPT_MAX_RECV_SPEED_LARGE,\n \t\t\t\t curl_max_receive_speed);\n #endif\n-- \n2.28.1.49.gafcb914ae4.dirty.agit.6.3.1\n\n"}]}