Skip to content

Instantly share code, notes, and snippets.

@HeshanSudarshana
Last active October 9, 2019 07:24
Show Gist options
  • Select an option

  • Save HeshanSudarshana/369d585328c06ae97c2d23c9c14c4300 to your computer and use it in GitHub Desktop.

Select an option

Save HeshanSudarshana/369d585328c06ae97c2d23c9c14c4300 to your computer and use it in GitHub Desktop.
RateLimitter Implementation
// if you are allowing 5 requests per second
final RateLimiter rateLimiter = RateLimiter.create(5.0);
void throttler() {
rateLimiter.acquire(); // may wait
doSomething();
}
// if you are allowing 5000 bytes per second
final RateLimiter rateLimiter = RateLimiter.create(5000.0);
void submitPacket(byte[] packet) {
rateLimiter.acquire(packet.length);
networkService.send(packet);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment