master
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 * SPDX-License-Identifier: curl
22 *
23 ***************************************************************************/
24#include "curl_setup.h"
26#include "urldata.h"
27#include "ratelimit.h"
29#define CURL_RLIMIT_MIN_RATE (4 * 1024) /* minimum step rate */
30#define CURL_RLIMIT_STEP_MIN_MS 2 /* minimum step duration */
32static void rlimit_update(struct Curl_rlimit *r,
33 const struct curltime *pts)
34{
35 timediff_t elapsed_us, elapsed_steps;
36 int64_t token_gain;
38 DEBUGASSERT(r->rate_per_step);
39 if((r->ts.tv_sec == pts->tv_sec) && (r->ts.tv_usec == pts->tv_usec))
40 return;
42 elapsed_us = curlx_ptimediff_us(pts, &r->ts);
43 if(elapsed_us < 0) { /* not going back in time */
44 DEBUGASSERT(0);
45 return;
46 }
48 elapsed_us += r->spare_us;
49 if(elapsed_us < r->step_us)
50 return;
52 /* we do the update */
53 r->ts = *pts;
54 elapsed_steps = elapsed_us / r->step_us;
55 r->spare_us = elapsed_us % r->step_us;
57 /* How many tokens did we gain since the last update? */
58 if(r->rate_per_step > (INT64_MAX / elapsed_steps))
59 token_gain = INT64_MAX;
60 else {
61 token_gain = r->rate_per_step * elapsed_steps;
62 }
64 if((INT64_MAX - token_gain) > r->tokens)
65 r->tokens += token_gain;
66 else
67 r->tokens = INT64_MAX;
69 /* Limit the token again by the burst rate (if set), so we
70 * do not suddenly have a huge number of tokens after inactivity. */
71 if(r->burst_per_step && (r->tokens > r->burst_per_step)) {
72 r->tokens = r->burst_per_step;
73 }
74}
76static void rlimit_tune_steps(struct Curl_rlimit *r,
77 int64_t tokens_total)
78{
79 int64_t tokens_last, tokens_main, msteps;
81 /* Tune the ratelimit at the start *if* we know how many tokens
82 * are expected to be consumed in total.
83 * The reason for tuning is that rlimit provides tokens to be consumed
84 * per "step" which starts out to be a second. The tokens may be consumed
85 * in full at the beginning of a step. The remainder of the second will
86 * have no tokens available, effectively blocking the consumption and
87 * so keeping the "step average" in line.
88 * This works will up to the last step. When no more tokens are needed,
89 * no wait will happen and the last step would be too fast. This is
90 * especially noticeable when only a few steps are needed.
91 *
92 * Example: downloading 1.5kb with a ratelimit of 1k could be done in
93 * roughly 1 second (1k in the first second and the 0.5 at the start of
94 * the second one).
95 *
96 * The tuning tries to make the last step small, using only
97 * 1 percent of the total tokens (at least 1). The rest of the tokens
98 * are to be consumed in the steps before by adjusting the duration of
99 * the step and the amount of tokens it provides. */
100 if(!r->rate_per_step ||
101 (tokens_total <= 1) ||
102 (tokens_total > (INT64_MAX / 1000)))
103 return;
105 /* Calculate tokens for the last step and the ones before. */
106 tokens_last = tokens_total / 100;
107 if(!tokens_last) /* less than 100 total, use 1 */
108 tokens_last = 1;
109 else if(tokens_last > CURL_RLIMIT_MIN_RATE)
110 tokens_last = CURL_RLIMIT_MIN_RATE;
111 DEBUGASSERT(tokens_last);
112 tokens_main = tokens_total - tokens_last;
113 DEBUGASSERT(tokens_main);
115 /* how many milli-steps will it take to consume those, give the
116 * original rate limit per second? */
117 DEBUGASSERT(r->step_us == CURL_US_PER_SEC);
119 msteps = (tokens_main * 1000 / r->rate_per_step);
120 if(msteps < CURL_RLIMIT_STEP_MIN_MS) {
121 /* Steps this small will not work. Do not tune. */
122 return;
123 }
124 else if(msteps < 1000) {
125 /* It needs less than one step to provide the needed tokens.
126 * Make it exactly that long and with exactly those tokens. */
127 r->step_us = (timediff_t)msteps * 1000;
128 r->rate_per_step = tokens_main;
129 r->tokens = r->rate_per_step;
130 }
131 else {
132 /* More than 1 step. Spread the remainder milli steps and
133 * the tokens they need to provide across all steps. If integer
134 * arithmetic can do it. */
135 curl_off_t ms_unaccounted = (msteps % 1000);
136 curl_off_t mstep_inc = (ms_unaccounted / (msteps / 1000));
137 if(mstep_inc) {
138 curl_off_t rate_inc = ((r->rate_per_step * mstep_inc) / 1000);
139 if(rate_inc) {
140 r->step_us = CURL_US_PER_SEC + ((timediff_t)mstep_inc * 1000);
141 r->rate_per_step += rate_inc;
142 r->tokens = r->rate_per_step;
143 if(r->burst_per_step) {
144 curl_off_t burst_inc = ((r->burst_per_step * mstep_inc) / 1000);
145 if(burst_inc)
146 r->burst_per_step += burst_inc;
147 }
148 }
149 }
150 }
151}
153void Curl_rlimit_change(struct Curl_rlimit *r,
154 int64_t rate_per_sec,
155 int64_t burst_per_sec)
156{
157 DEBUGASSERT(rate_per_sec >= 0);
158 DEBUGASSERT(burst_per_sec >= rate_per_sec || !burst_per_sec);
159 r->rate_per_step = r->rate_per_sec = rate_per_sec;
160 r->burst_per_step = r->burst_per_sec = burst_per_sec;
161 r->step_us = CURL_US_PER_SEC;
162 r->spare_us = 0;
163 if(r->tokens > r->rate_per_step)
164 r->tokens = r->rate_per_step;
165 /* ts and blocked stay as they are, a paused transfer stays paused */
166}
168void Curl_rlimit_start(struct Curl_rlimit *r, const struct curltime *pts,
169 int64_t total_tokens)
170{
171 /* A start always resets the values to initial defaults, then
172 * fine tunes the intervals for the total_tokens expected. */
173 r->rate_per_step = r->rate_per_sec;
174 r->burst_per_step = r->burst_per_sec;
175 r->step_us = CURL_US_PER_SEC;
176 r->spare_us = 0;
177 r->tokens = r->rate_per_step;
178 r->ts = *pts;
179 rlimit_tune_steps(r, total_tokens);
180}
182int64_t Curl_rlimit_per_step(struct Curl_rlimit *r)
183{
184 return r->rate_per_step;
185}
187bool Curl_rlimit_active(struct Curl_rlimit *r)
188{
189 return (r->rate_per_step > 0) || r->blocked;
190}
192bool Curl_rlimit_is_blocked(struct Curl_rlimit *r)
193{
194 return (bool)r->blocked;
195}
197int64_t Curl_rlimit_avail(struct Curl_rlimit *r,
198 const struct curltime *pts)
199{
200 struct curltime ts;
202 if(r->blocked)
203 return 0;
204 else if(r->rate_per_step) {
205 if(!pts) {
206 curlx_pnow(&ts);
207 pts = &ts;
208 }
209 rlimit_update(r, pts);
210 return r->tokens;
211 }
212 else
213 return INT64_MAX;
214}
216void Curl_rlimit_drain(struct Curl_rlimit *r, size_t tokens,
217 const struct curltime *pts)
218{
219 struct curltime ts;
221 if(r->blocked || !r->rate_per_step)
222 return;
224 if(!pts) {
225 curlx_pnow(&ts);
226 pts = &ts;
227 }
228 rlimit_update(r, pts);
229#if 8 <= SIZEOF_SIZE_T
230 if(tokens > INT64_MAX) {
231 r->tokens = INT64_MAX;
232 }
233 else
234#endif
235 {
236 int64_t val = (int64_t)tokens;
237 if((INT64_MIN + val) < r->tokens)
238 r->tokens -= val;
239 else
240 r->tokens = INT64_MIN;
241 }
242}
244timediff_t Curl_rlimit_wait_ms(struct Curl_rlimit *r,
245 const struct curltime *pts)
246{
247 timediff_t wait_us, elapsed_us;
249 if(r->blocked || !r->rate_per_step)
250 return 0;
251 rlimit_update(r, pts);
252 if(r->tokens > 0)
253 return 0;
255 /* How much time will it take tokens to become positive again?
256 * Deduct `spare_us` and check against already elapsed time */
257 wait_us = r->step_us - r->spare_us;
258 if(r->tokens < 0) {
259 curl_off_t debt_pct = ((-r->tokens) * 100 / r->rate_per_step);
260 if(debt_pct)
261 wait_us += (r->step_us * debt_pct / 100);
262 }
264 elapsed_us = curlx_ptimediff_us(pts, &r->ts);
265 if(elapsed_us >= wait_us)
266 return 0;
267 wait_us -= elapsed_us;
268 return (wait_us + 999) / 1000; /* in milliseconds */
269}
271timediff_t Curl_rlimit_next_step_ms(struct Curl_rlimit *r,
272 const struct curltime *pts)
273{
274 if(!r->blocked && r->rate_per_step) {
275 timediff_t elapsed_us, next_us;
277 elapsed_us = curlx_ptimediff_us(pts, &r->ts) + r->spare_us;
278 if(r->step_us > elapsed_us) {
279 next_us = r->step_us - elapsed_us;
280 return (next_us + 999) / 1000; /* in milliseconds */
281 }
282 }
283 return 0;
284}
286void Curl_rlimit_block(struct Curl_rlimit *r,
287 bool activate,
288 const struct curltime *pts)
289{
290 if(!activate == !r->blocked)
291 return;
293 r->ts = *pts;
294 r->blocked = activate;
295 if(!r->blocked) {
296 /* Start rate limiting fresh. The amount of time this was blocked
297 * does not generate extra tokens. */
298 Curl_rlimit_start(r, pts, -1);
299 }
300 else {
301 r->tokens = 0;
302 }
303}
Diagnostics4 git calls · 192 ms
CallStatusSizeTime0192 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=187 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch2002.9 KiB187 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=3002 git.response.cache=MISS git.response.duration_ms=187 git.fetch.wants=c000e9519273175698b5de85c3d14bc3d8b64589 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=3002 git.pkt.payload.max=2981 git.sideband.band1.bytes=2980 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e180e7da7b3014
- Request
- d95fa04f…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format