From 6f9e73f0581d70c4b1c5c17b846dda10c33ee21c Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sat, 9 May 2026 20:04:02 +0100 Subject: [PATCH] Add more detailed comments about the rate limiter. --- src/pv/loop.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/pv/loop.c b/src/pv/loop.c index 0785924..03e3d64 100644 --- a/src/pv/loop.c +++ b/src/pv/loop.c @@ -572,6 +572,11 @@ int pv_main_loop(pvstate_t state) if (1 == state->flags.trigger_exit) break; + /* + * If rate limiting is active, set "cansend" to the maximum + * transfer amount allowable that would maintain the + * requested rate. + */ if (state->control.rate_limit_active) { pv_elapsedtime_read(&cur_time); while (pv_elapsedtime_compare(&cur_time, &next_ratecheck) > 0) { @@ -579,14 +584,22 @@ int pv_main_loop(pvstate_t state) ((long double) (state->control.rate_limit)) / (long double) (1000000000.0 / (long double) (RATE_GRANULARITY)); + /* + * Cap the "target bytes amount to transfer" + * so it doesn't go too high - otherwise it + * just keeps going up while there is no, or + * slow, input data, and then when lots of + * input data does arrive, the rate limit is + * ineffective until the counter drops back + * down (see PR #62). + * + * Only apply this cap if it is at least + * 1.0, otherwise the target will always be + * below 1, and nothing will ever get + * transferred (see issue #193). + */ long double burst_max = ((long double) (state->control.rate_limit * RATE_BURST_WINDOW)); if (burst_max > 1.0 && rate_limited_target > burst_max) { - /* - * If the burst max is < 1 then - * capping to that will mean nothing - * ever gets sent, so turn off the - * burst limit at 1 and below. - */ rate_limited_target = burst_max; } pv_elapsedtime_add_nsec(&next_ratecheck, RATE_GRANULARITY);