Skip to content

akornatskyy/rate-limiter-java

Repository files navigation

rate-limiter-java

tests maven central

Limit the rate of incoming requests.

rate-limit

Usage

Initialize the rate limit to 5K requests per hour.

RateLimiterOptions options = RateLimiterOptions.builder()
    .max(5000)
    .duration(Duration.ofHours(1))
    .window(Duration.ofMinutes(1))
    .build();

Synchronous rate in memory limiter example.

RateLimiter limiter = new RateInMemoryLimiter(options);

// somewhere later in your code
RateLimit limit = limiter.addAndGet("key", 1);
if (!limit.hasRemaining()) {
  // not good
  return;
}

// all good

More examples:

Install

Add as a maven dependency:

<dependency>
  <groupId>com.github.akornatskyy</groupId>
  <artifactId>rate-limiter</artifactId>
  <version>1.1.4</version>
</dependency>

when used with redis lettuce:

<dependency>
  <groupId>com.github.akornatskyy</groupId>
  <artifactId>rate-limiter-lettuce</artifactId>
  <version>1.1.8</version>
</dependency>

Release

mvn versions:set -pl rate-limiter -DnewVersion=1.1.X
mvn -pl rate-limiter -P release clean deploy

mvn versions:set -pl rate-limiter-lettuce -DnewVersion=1.1.X
mvn -pl rate-limiter-lettuce -P release clean deploy

About

💤 Limit the rate of incoming requests.

Topics

Resources

License

Stars

Watchers

Forks