gubernator icon indicating copy to clipboard operation
gubernator copied to clipboard

Feature Request: Atomic Chaining of Limiters

Open colinskow opened this issue 4 years ago • 2 comments

Sometimes when using multiple resources at the same time I want to make sure they are all clear before beginning the work. For example before calling an external API, I want to make sure my own server has capacity to handle the work AND I want to make sure not to abuse the limits of the external API.

I would like to be able to include a chain (list) of limiters in one request. Gubernator would atomically determine if each limiter is UNDER_LIMIT, and only then increment all the hits. If any one of the requested limits is OVER_LIMIT, then the request fails and no hits are incremented.

The response would probably be a Boolean flag of whether the overall request is successful and a list of responses for each limiter requested. The longest reset_time for a failed limiter would be useful as well.

The closest way to achieve this currently is querying each limiter with zero hits to see if they are all open, and only then sending another request to deduct hits from each limiter. The problem is that this operation is not atomic so they could have filled up between requests.

colinskow avatar Feb 12 '20 20:02 colinskow

Here is an example of this feature in action in another rate limiter: https://github.com/animir/node-rate-limiter-flexible/wiki/RateLimiterUnion

colinskow avatar Feb 16 '20 21:02 colinskow

I've been thinking about this. We could support this by adding a Behavior to the GetRateLimitsReq like so.

message GetRateLimitsReq {
  repeated RateLimitReq requests = 1;
  // Behavior is a set of int32 flags that control the 
 // behavior that applies to all requests
  UnionBehavior behavior = 2;
}

Then a Behavior of ATOMIC_REQUESTS could be added as an option to UnionBehavior

thrawn01 avatar May 08 '20 16:05 thrawn01