How to limit number of requests

Did you ever need to limit requests coming to your endpoints? Say, your maximum design capacity is near 10k requests/second, and you don’t want to probe your service on higher rates.

I choose to use RateLimiter - small class from Guava.

The init process aren’t so huge, just couple of lines:

RateLimiter rateLimiter = RateLimiter.create(10); // 10 requests/second

Then, just place rateLimiter in place, where you need to have limited requests:

boolean isAcquired = rateLimiter.tryAcquire();

if (!isAcquired) {
    throw new NotSoFastBuddyException(...);