https://github.com/client-side/throttle
Java Rate Limiter Derived From Googles' Guava Implementation
https://github.com/client-side/throttle
java rate-limiter rate-limiting throttle
Last synced: 1 day ago
JSON representation
Java Rate Limiter Derived From Googles' Guava Implementation
- Host: GitHub
- URL: https://github.com/client-side/throttle
- Owner: client-side
- License: apache-2.0
- Archived: true
- Created: 2016-10-19T17:43:12.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-12-31T14:29:24.000Z (almost 8 years ago)
- Last Synced: 2024-10-13T11:47:00.162Z (12 months ago)
- Topics: java, rate-limiter, rate-limiting, throttle
- Language: Java
- Homepage:
- Size: 172 KB
- Stars: 14
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Throttle [](https://travis-ci.org/client-side/throttle) [ ](https://bintray.com/client-side/clients/throttle/_latestVersion) [ ](LICENSE) [](https://codecov.io/gh/client-side/throttle)
>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:
```java
Throttle throttle = Throttle.create(2.0); // 2 permits per second
// ...
void submitTasks(List tasks, Executor executor) {
for (Runnable task : tasks) {
throttle.acquire();
executor.execute(task);
}
}
```###### Cap data stream to 5kb per second:
```java
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](src/main/java/engineering/clientside/throttle/Throttle.java#L83), from the base abstract class.
* Remove the need for any non-core-Java classes outside of the original [RateLimiter](https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/RateLimiter.java) and [SmoothRateLimiter](https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/SmoothRateLimiter.java) classes.
* Remove the need for a [SleepingStopwatch](https://github.com/google/guava/blob/master/guava/src/com/google/common/util/concurrent/RateLimiter.java#L395) 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
```groovy
repositories {
jcenter()
}dependencies {
compile 'engineering.clientside:throttle:+'
}
```