Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.
/ throttle Public archive

Java Rate Limiter Derived From Googles' Guava Implementation

License

Notifications You must be signed in to change notification settings

client-side/throttle

Repository files navigation

Throttle Build Status JCenter License codecov

Provides a mechanism to limit the rate of access to a resource.

Usage

A Throttle instance distributes permits at a desired rate, blocking if necessary until a permit is available.

Java Version Support

Version 0.2.6 is the latest version to support Java 8. Future versions will support the latest Java version that is out at the time of release.

Submit two tasks per second:
Throttle throttle = Throttle.create(2.0); // 2 permits per second
// ...
void submitTasks(List<Runnable> tasks, Executor executor) {
  for (Runnable task : tasks) {
    throttle.acquire();
    executor.execute(task);
  }
}
Cap data stream to 5kb per second:
Throttle throttle = Throttle.create(5000.0); // 5000 permits per second
// ...
void submitPacket(byte[] packet) {
  throttle.acquire(packet.length);
  networkService.send(packet);
}

Changes From Guava Rate Limiter

  • Nanosecond instead of microsecond accuracy.
  • Uses a ReentrantLock instead of synchronized blocks to support optional fair acquisition ordering.
  • Factoring out an interface class, Throttle, from the base abstract class.
  • Remove the need for any non-core-Java classes outside of the original RateLimiter and SmoothRateLimiter classes.
  • Remove the need for a SleepingStopwatch or similar class instance.
  • Guava provides rate limiters with either bursty or warm-up behavior. Throttle provides only a single strict rate limiter implementation that will never exceed the desired rate limit over a one second period.
  • Throws checked InterruptedException's or unchecked CompletionException's with the cause set to the corresponding InterruptedException if interrupted.

Dependency Management

repositories {
   jcenter()
}

dependencies {
   compile 'engineering.clientside:throttle:+'
}