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

[PATCH v2] http: add http.maxReceiveSpeed to limit receiving speed of "git-receive-pack"

From
Cchenan.xxw <haoyurenzhuxia@gmail.com>
Date
Aug 23, 2021, 16:04 UTC
Message-ID
<20210823160455.32397-1-chenan.xxw@alibaba-inc.com>
In-Reply-To
<20210819091433.348-1-chenan.xxw@alibaba-inc.com>

In order to avoid hogging all the available network bandwidth, users may want to limit the speed of receiving traffic for "git clone" or "git fetch".

Add `http.maxReceiveSpeed` to limit receiving speed of `git-receive-pack`. Can be overridden by `GIT_HTTP_MAX_RECEIVE_SPEED` environment variable.

The default is unlimited, same if the value is 0. The default unit is Bytes/s, common unit suffixes of k, m, or g are supported.

This configuration is valid for `clone`, `fetch`, `pull` commands of the https protocol.

Signed-off-by: chenan.xxw <chenan.xxw@alibaba-inc.com>
---
 Documentation/config/http.txt |  4 ++++
 http.c                        | 21 +++++++++++++++++++++
 2 files changed, 25 insertions(+)
diff --git a/Documentation/config/http.txt b/Documentation/config/http.txt
index 7003661c0d..9b9fb5e9c7 100644
--- a/Documentation/config/http.txt
+++ b/Documentation/config/http.txt
@@ -235,6 +235,10 @@ http.lowSpeedLimit, http.lowSpeedTime::
 	Can be overridden by the `GIT_HTTP_LOW_SPEED_LIMIT` and
 	`GIT_HTTP_LOW_SPEED_TIME` environment variables.
 
+http.maxReceiveSpeed::
+	Limit the speed of receiving traffic, defaults to unlimited. Can be
+	overridden by the `GIT_HTTP_MAX_RECEIVE_SPEED` environment variable.
+
 http.noEPSV::
 	A boolean which disables using of EPSV ftp command by curl.
 	This can helpful with some "poor" ftp servers which don't
diff --git a/http.c b/http.c
index 8119247149..b12d192ffe 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_max_receive_speed;
 static int curl_ftp_no_epsv;
 static const char *curl_http_proxy;
 static const char *http_proxy_authmethod;
@@ -362,6 +363,13 @@ static int http_options(const char *var, const char *value, void *cb)
 		return 0;
 	}
 
+	if (!strcmp("http.maxreceivespeed", var)) {
+		curl_max_receive_speed = (long)git_config_int(var, value);
+		if (curl_max_receive_speed < 0)
+			die(_("negative values are not allowed for http.maxreceivespeed"));
+		return 0;
+	}
+
 	if (!strcmp("http.noepsv", var)) {
 		curl_ftp_no_epsv = git_config_bool(var, value);
 		return 0;
@@ -974,6 +982,10 @@ static CURL *get_curl_handle(void)
 				 curl_low_speed_time);
 	}
 
+	if (curl_max_receive_speed >= 0)
+		curl_easy_setopt(result, CURLOPT_MAX_RECV_SPEED_LARGE,
+				 (curl_off_t)curl_max_receive_speed);
+
 	curl_easy_setopt(result, CURLOPT_MAXREDIRS, 20);
 #if LIBCURL_VERSION_NUM >= 0x071301
 	curl_easy_setopt(result, CURLOPT_POSTREDIR, CURL_REDIR_POST_ALL);
@@ -1105,6 +1117,8 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
 {
 	char *low_speed_limit;
 	char *low_speed_time;
+	char *mrs;
+	static const char mrs_env[] = "GIT_HTTP_MAX_RECEIVE_SPEED";
 	char *normalized_url;
 	struct urlmatch_config config = { STRING_LIST_INIT_DUP };
 
@@ -1197,6 +1211,13 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
 	if (low_speed_time != NULL)
 		curl_low_speed_time = strtol(low_speed_time, NULL, 10);
 
+	mrs = getenv(mrs_env);
+	if (mrs != NULL) {
+		curl_max_receive_speed = strtol(mrs, NULL, 10);
+		if (curl_max_receive_speed < 0)
+			die(_("negative values are not allowed for %s"), mrs_env);
+	}
+
 	if (curl_ssl_verify == -1)
 		curl_ssl_verify = 1;
 
-- 
2.33.0.1.g26e5a845c1.dirty
Previous: Xiaowen XiaNext: Jiang Xin
Message 10 of 12 in “add http.maxReceiveSpeed to limit git-receive-pack receiving speed”
  1. add http.maxReceiveSpeed to limit git-receive-pack receiving speedXia XiaoWen, Aug 19, 2021
  2. Bagas SanjayaAug 19, 2021
  3. Xiaowen XiaAug 19, 2021
  4. Junio C HamanoAug 19, 2021
  5. Jeff KingAug 20, 2021
  6. Randall S. BeckerAug 20, 2021
  7. Xiaowen XiaAug 21, 2021
  8. Junio C HamanoAug 21, 2021
  9. Xiaowen XiaAug 21, 2021
  10. http: add http.maxReceiveSpeed to limit receiving speed of "git-receive-pack"chenan.xxw, Aug 23, 2021
  11. Jiang XinAug 24, 2021
  12. Xiaowen XiaAug 31, 2021

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.