a guest Apr 18th, 2019 159 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- Can I do this? -> On yes: do action(s)
- Rate Limiter <-> Action Driver/Handler --|
- -> On no: deny action(s)
- A rate limiter is a module that regulates the number of actions performed (whether it be requests made from a client, or requests accepted by a server). For example, a component that sends requests to api.google.com should be aware that that API has a rate limit of 2000 READ requests per 100 seconds. This protects api.google.com from being overwhelmed with too many requests. To prevent Google from blacklisting or banning the component, it ought to use a rate limiter to regulate its own requests. As another example, the server on Google side can also use a rate limiter to short circuit and send a 429 to protect itself from abusive users; upon receiving an HTTP request, it consults its rate limiter and allows/denies based on the result of the rate limiter.
- When something uses the Rate Limiter, it first checks whether the Rate Limiter can handle a certain # of requests. If the rate limit has not been exceeded and has capacity, then the caller continues to perform the operations. The rate limiter tracks that the certain number of requests went through. However, if the rate limiter decides that a certain # request cannot go through, the rate limiter does not add those number of requests to its internal tracking, as they were denied!
- A rate limiter will have a rate of requests it can accept (something like 5 requests per minute, or N requests per M time). A rate limiter is expected to enforce this over time, and replenish its capacity as time progresses.
- QUESTION #1 = INTERFACE DESIGN
- Let's design the Rate Limiter interface that is called by a Requester. This interface has 2 functionalities:
- FUNCTIONALITY 1 = Check whether the Rate Limiter can handle N additional requests, within the given rate limit.
- - If YES, Rate Limiter internally records that N requests were processed, and replies to the Requester that the operations were permitted.
- - If NOT, Rate Limiter doesn't record any requests were processed, and replies to the Requester that the operations were NOT permitted.
- ** Note: Requests are "all or nothing". For example, if I request 10 operations at once and my capacity is 0 / 9, I should deny the request of 10 and remain at 0 / 9 capacity.
- FUNCTIONALITY 2 = Return the # of requests a Rate Limiter can accept right now, without exceeding the rate limit. For example, if my capacity is 5 / 9, return 4.
- QUESTION #2 = CODE IMPLEMENTATION
- Let's implement the interface you just designed, such that Rate Limiter enforces a limit of N requests per minute.
- QUESTION #3 = BONUS POINTS
- Let's build a separate module that utilizes your interface/implementation, which has the following capabilities:
- (A) Enforce a rate limit per Requester IP
- (B) Return the N Requester IPs with the lowest # of remaining capacity
RAW Paste Data