Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttling method calls using Guava RateLimiter class

I am trying to throttle the number of calls to a method per second. I tried to achieve this using Guava RateLimiter.

RateLimiter rateLimiter = RateLimiter.create(1.0);//Max 1 call per sec
rateLimiter.acquire();
performOperation();//The method whose calls are to be throttled.

However the methods to the call are not limited to 1 per second but are continuous.

The throttling can be achieved using Thread.sleep() but i wish to use Guava rather that sleep().

I would like to know the right way to achieve the method call trottling using Guava RateLimiter. I have checked the documentation for RateLimiter and tried to use the same but could not achieve the desired result.

like image 680
sujith Avatar asked Aug 07 '15 17:08

sujith


People also ask

How does guava RateLimiter work?

The RateLimiter API has also a very useful acquire() method that accepts a timeout and TimeUnit as arguments. Calling this method when there are no available permits will cause it to wait for specified time and then time out – if there are not enough available permits within the timeout.

How RateLimiter works?

RateLimiter uses the token bucket algorithm and accumulates the tokens. If the token consumption frequency is low, the requests can directly get the tokens without waiting. RateLimiter accumulates tokens to allow it to cope with sudden increase in traffic.

Is Guava RateLimiter thread safe?

RateLimiter is safe for concurrent use: It will restrict the total rate of calls from all threads. Note, however, that it does not guarantee fairness.

What is Resilience4j RateLimiter?

Resilience4j provides a RateLimiter which splits all nanoseconds from the start of epoch into cycles. Each cycle has a duration configured by RateLimiterConfig. limitRefreshPeriod .


1 Answers

You need to call acquire() on the same RateLimiter in every invocation, e.g. by making it available in performOperation():

public class RateLimiterTest {
    public static void main(String[] args) {
        RateLimiter limiter = RateLimiter.create(1.0);
        for (int i = 0; i < 10; i++) {
            performOperation(limiter);
        }
    }

    private static void performOperation(RateLimiter limiter) {
        limiter.acquire();
        System.out.println(new Date() + ": Beep");
    }
}

results in

Fri Aug 07 19:00:10 BST 2015: Beep
Fri Aug 07 19:00:11 BST 2015: Beep
Fri Aug 07 19:00:12 BST 2015: Beep
Fri Aug 07 19:00:13 BST 2015: Beep
Fri Aug 07 19:00:14 BST 2015: Beep
Fri Aug 07 19:00:15 BST 2015: Beep
Fri Aug 07 19:00:16 BST 2015: Beep
Fri Aug 07 19:00:17 BST 2015: Beep
Fri Aug 07 19:00:18 BST 2015: Beep
Fri Aug 07 19:00:19 BST 2015: Beep

like image 122
Jens Hoffmann Avatar answered Sep 19 '22 21:09

Jens Hoffmann