From a6ecf60ad1aba076e2aa889740212a2c8fde5f9e Mon Sep 17 00:00:00 2001 From: Volodymyr Bychkovyak Date: Wed, 22 Mar 2023 02:49:46 -0700 Subject: [PATCH] fix rate limiting issues --- src/include/pv-internal.h | 1 + src/pv/loop.c | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/include/pv-internal.h b/src/include/pv-internal.h index db65eaa..244cbe2 100644 --- a/src/include/pv-internal.h +++ b/src/include/pv-internal.h @@ -34,6 +34,7 @@ extern "C" { #define PV_DISPLAY_FINETA 512 #define RATE_GRANULARITY 100000 /* usec between -L rate chunks */ +#define RATE_BURST_WINDOW 5 /* rate burst window (multiples of rate) */ #define REMOTE_INTERVAL 100000 /* usec between checks for -R */ #define BUFFER_SIZE 409600 /* default transfer buffer size */ #define BUFFER_SIZE_MAX 524288 /* max auto transfer buffer size */ diff --git a/src/pv/loop.c b/src/pv/loop.c index f1c5989..949ba5f 100644 --- a/src/pv/loop.c +++ b/src/pv/loop.c @@ -151,10 +151,15 @@ int pv_main_loop(pvstate_t state) || (cur_time.tv_sec == next_ratecheck.tv_sec && cur_time.tv_usec >= next_ratecheck.tv_usec)) { + target += ((long double) (state->rate_limit)) / (long double) (1000000 / RATE_GRANULARITY); + long double burstMax = ((long double) (state->rate_limit * RATE_BURST_WINDOW)); + if (target > burstMax) { + target = burstMax; + } pv_timeval_add_usec(&next_ratecheck, RATE_GRANULARITY); }