]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
ratelimit: reset on start
authorStefan Eissing <stefan@eissing.org>
Tue, 24 Mar 2026 12:41:51 +0000 (13:41 +0100)
committerDaniel Stenberg <daniel@haxx.se>
Tue, 24 Mar 2026 15:25:53 +0000 (16:25 +0100)
On any `Curl_rlimit_start()` the rate limit needs to reset its
values before calculating the effective step duration and adjust
the tokens/burst per step.

Add two fields to the struct to remember the original values.

Closes #21086

lib/ratelimit.c
lib/ratelimit.h

index 2d88cdd7c94428a1cddbb7cd8554d7123e33fabe..dc013757e9128000430f8458ba15e1223b79deea 100644 (file)
@@ -157,8 +157,8 @@ void Curl_rlimit_init(struct Curl_rlimit *r,
   DEBUGASSERT(rate_per_sec >= 0);
   DEBUGASSERT(burst_per_sec >= rate_per_sec || !burst_per_sec);
   DEBUGASSERT(pts);
-  r->rate_per_step = rate_per_sec;
-  r->burst_per_step = burst_per_sec;
+  r->rate_per_step = r->rate_per_sec = rate_per_sec;
+  r->burst_per_step = r->burst_per_sec = burst_per_sec;
   r->step_us = CURL_US_PER_SEC;
   r->spare_us = 0;
   r->tokens = r->rate_per_step;
@@ -169,8 +169,13 @@ void Curl_rlimit_init(struct Curl_rlimit *r,
 void Curl_rlimit_start(struct Curl_rlimit *r, const struct curltime *pts,
                        int64_t total_tokens)
 {
-  r->tokens = r->rate_per_step;
+  /* A start always resets the values to initial defaults, then
+   * fine tunes the intervals for the total_tokens expected. */
+  r->rate_per_step = r->rate_per_sec;
+  r->burst_per_step = r->burst_per_sec;
+  r->step_us = CURL_US_PER_SEC;
   r->spare_us = 0;
+  r->tokens = r->rate_per_step;
   r->ts = *pts;
   rlimit_tune_steps(r, total_tokens);
 }
index 7563734d65f56d4683c523a6abf71b4648bd23de..3c3e38b89587283b7db21e2257f4dac0f41f8c06 100644 (file)
@@ -55,6 +55,8 @@ struct Curl_easy;
  */
 
 struct Curl_rlimit {
+  int64_t rate_per_sec; /* rate tokens generated per second */
+  int64_t burst_per_sec; /* burst rate of tokens per second */
   int64_t rate_per_step; /* rate tokens generated per step us */
   int64_t burst_per_step; /* burst rate of tokens per step us */
   timediff_t step_us;     /* microseconds between token increases */