Closed justinffs closed 6 years ago
When you get information back from an API about what the true capacity is at any given moment, I think the best option is to modify the number of tokens in the token bucket directly. Example:
this.limiter.tokenBucket.content = 2
This works fine for reducing the current number of tokens. The problem with adding more tokens is that any current pending calls to removeTokens
will already have a timer scheduled and there is no support (yet) for canceling that timer and retrying. It would be possible to add a refreshPending()
method but it's not something I have time to implement myself right now.
Thanks @jhurliman
I am using limiter to throttle calls to a DynamoDB table. DynamoDB will return the Consumed Capacity, which sometimes is 0. I would like to NOT delay the next call if the Consumed Capacity is 0 but I don't see a straight forward way to do this. I have tried setting then number of tokens to 2, then running tryRemoveTokens(1) if Consumed Capacity is > 0. I also tried the opposite and
this.limiter.tokensThisInterval+=1;
if Consumed Capacity is 0. Neither seems to work the way I thought it would.tryRemoveTokens
would run intoif (count > this.tokenBucket.tokensPerInterval - this.tokensThisInterval)
and return false.I am probably misunderstanding how this works. Here is example code snippets.