RateLimiters icon indicating copy to clipboard operation
RateLimiters copied to clipboard

Fixed Token Bucket Implementation

Open jmawebtech opened this issue 7 years ago • 1 comments

Hi,

I am using your tool to throttle orders from Amazon. Here are the parameters:

    //Loop through them calling GetOrder, since GetOrder has a max request quota of 6 with a refill rate of 1 a minute, with the added benefit of being able to call up to 50 orders per request. This means that I can make an initial request for 300 orders, then I must throttle down to 1 request a minute, requiring an additional 14 minutes for the remaining 700 orders.

    //For each order, I must call ListOrderItems. This has a max request limit of 30 and a restore rate of 1 every 2 seconds. This means that I can initially make 30 requests, then I must throttle to 1 every 2 seconds, which means it would take me about 485 seconds (or 8 minutes) to finish getting the order items for the rest of the orders.

Here is my code:

        listOrderRequestBucket = new FixedTokenBucket(6, 60, 1000);
        listOrderItemsRequestBucket = new FixedTokenBucket(1, 2, 1000);
        listFinancialEventsResponseBucket = new FixedTokenBucket(1, 2, 1000);

When I am throttled, I do this:

                    Timespan WaitTime;
                    bool shouldThrottle = listOrderRequestBucket.ShouldThrottle(1, out waitTime);

                    if (shouldThrottle)
                    {
                        Thread.Sleep(waitTime);
                        listOrderRequestBucket = new FixedTokenBucket(1, 60, 1000);
                    }

Is this what you recommend?

jmawebtech avatar Jul 12 '16 02:07 jmawebtech

I think you want the StepUpLeakyTokenBucket instead? (or maybe the StepDown per #6)

But for any strategy, the basic pattern is something like:

while(doIHaveMoreActions()) {
	myAction();
	while (yourBucket.ShouldThrottle(n, out snooze)) Thread.Sleep(snooze);
}

zaus avatar Dec 12 '16 17:12 zaus