RateLimiters
RateLimiters copied to clipboard
Incorrect token reset in StepUpLeakyTokenBucket
tokens = stepTokens;
should it be:
tokens = bucketTokenCapacity;
similar to the StepDownLeakyTokenBucket?
It might just be that I don't really understand the difference or how to use it, but when using the StepUp I always wait when calling throttle.ShouldWait
, while the StepDown style will pass on the number of tokens before starting to wait.
I'm testing with something like:
// just to see what's happening while the throttle is snoozing...
var continueInterstitial = true;
var inBetweenLoops = 0;
var repeatLoops = 0;
var ct = new CancellationTokenSource();
var inBetween = Task.Run(() => {
while( continueInterstitial )
{
Dump(" Loop {3}:{2} at {1} Interstitial: {0}", backoff, SystemTime.UtcNow, inBetweenLoops, repeatLoops);
Thread.Sleep(stepDuration + 50);
inBetweenLoops++;
repeatLoops++;
}
},
ct.Token);
// the actual "test"
Func<int, Action<int>, TimeSpan> test = (repeat, inbetween) => {
var start = SystemTime.UtcNow;
log("Starting Test: {0:O} {1}", start, backoff);
for (int i = 0; i < repeat; i++)
{
repeatLoops = 0;
Dump("Try {0}: {1}", i, backoff);
TimeSpan snooze;
while (backoff.ShouldThrottle(1, out snooze)) Thread.Sleep(snooze);
Dump("End Wait: {0:O} {1}", SystemTime.UtcNow, backoff);
if (inbetween != null) inbetween(i);
}
var end = SystemTime.UtcNow;
var result = end - start;
Dump("Ended Test: {0:O} in {2} {1}", end, backoff, result);
return result;
};
var duration = test(tries, null);
continueInterstitial = false;
ct.Cancel();