https://github.com/jfisbein/java-rate-limit
Java Library to control repetition of events backed by Redis.
https://github.com/jfisbein/java-rate-limit
concurrency java rate-limiter rate-limiting
Last synced: 6 months ago
JSON representation
Java Library to control repetition of events backed by Redis.
- Host: GitHub
- URL: https://github.com/jfisbein/java-rate-limit
- Owner: jfisbein
- License: apache-2.0
- Created: 2020-01-07T10:41:29.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2025-11-23T12:17:54.000Z (8 months ago)
- Last Synced: 2025-11-23T14:13:42.854Z (8 months ago)
- Topics: concurrency, java, rate-limiter, rate-limiting
- Language: Java
- Homepage:
- Size: 546 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://github.com/jfisbein/java-rate-limit)
java-rate-limit
=================
Java library to embed rate limit control functionality inside applications.
Usage
-----
```java
import org.sputnik.ratelimit.domain.CanDoResponse;
import org.sputnik.ratelimit.service.JedisConfiguration;
import org.sputnik.ratelimit.service.RateLimiter;
class LoginManager {
// Configure rate limiter to allow maximum 3 attempts every hour.
RateLimiter vc = new RateLimiter(JedisConfiguration.builder().setHost("localhost").build(),
new EventConfig("testLogin", 3, Duration.ofSeconds(3600)));
private boolean doLogin(String username, String password) {
boolean isValid = false;
CanDoResponse response = vc.canDoEvent("testLogin", username);
if (response.canDo()) {
vc.doEvent("testLogin", username);
// TODO: check credentials and set isValid value
} else {
log.warn(
"User " + username + " blocked due to exceeding number of login attempt, for " + (response.waitMillis() / 1000) + " seconds");
isValid = false;
}
if (isValid) {
vc.reset("testLogin", username);
}
return isValid;
}
}
```
Maven & Gradle
--------------
For **maven** integration simply add this dependency to your `pom.xml`:
```xml
net.saltando
java-rate-limit
x.y.z
```
For **gradle** integration simply add this dependency:
```groovy
compile 'net.saltando:java-rate-limit:x.y.z'
```
Where x.y.x is the desired version, always lastest versions is recommended, you can find it
at [Releases](https://github.com/jfisbein/java-rate-limit/releases) tab.
Javadoc
-------
Javadoc is available at https://javadoc.jitpack.io/com/github/jfisbein/java-rate-limit/latest/javadoc/index.html
Limitations
-----------
As no synchronization method is implemented, some edge race conditions could lead to get a false positive or negative response from the
method `canDoEvent`.