RateLimiter is "bursty" -- it aims for an average rate of 1/s, not strictly 1s between permits. There's a lengthy explanation in a non-javadoc comment:
Cheers,
Justin
--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
---
You received this message because you are subscribed to the Google Groups "guava-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
guava-discus...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/guava-discuss/5ae17980-e2d3-419d-afd1-d043f6b41368%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
guava-...@googlegroups.com
Project site: https://github.com/google/guava
This group: http://groups.google.com/group/guava-discuss
This list is for general discussion.
To report an issue: https://github.com/google/guava/issues/new
To get help: http://stackoverflow.com/questions/ask?tags=guava
--- You received this message because you are subscribed to a topic in the Google Groups "guava-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/guava-discuss/UST-DM5BMAo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to guava-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/guava-discuss/7c1590ae-b50f-406e-b046-a3c3ba1c38e8%40durchholz.org.
Hi David,
I'll let the Guava folks debate the naming, but even if TimeLimiter imposed a simple maximum then it still wouldn't guarantee that no two calls will hit the external service with less than a second between them, due to the inevitable delays between calling acquire(), marshalling the request, and sending it over the wire. If one call starts at 0ms and takes 100ms to go over the wire and then the next call starts at exactly 1000ms but only takes 10ms to go over the wire, the external service will see them with only 910ms between them. If you want to absolutely guarantee that no two calls arrive at the external service with less than 1s between them, you might have to bite the bullet and make all the calls from a single thread with Thread.sleep(1000) between them.
By the way, to see the bursty behavior empirically, I just ran the following test:
RateLimiter rateLimiter = RateLimiter.create(1.0);
long start = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
if (i == 5) Thread.sleep(1500L);
rateLimiter.acquire();
System.out.println((System.currentTimeMillis() - start) / 1000.0);
}
Here's the output I got:
0.001
1.005
2.004
3.004
4.004
5.508
6.004
7.005
8.003
9.005
Notice how the limiter is recovering the average rate by only having half a second between 5.5 and 6.0. But also notice that a few of the other time points are slightly less than 1000ms apart, which we can attribute to the inherent delays between calling acquire() and calling currentTimeMillis() as well as the imperfect nature of the system clock. The delays involved in calling an external API will be orders of magnitude larger.
Cheers,
Justin