The RateLimitJ Dropwizard module provides integration with Dropwizard and annotation based configuration
<dependency>
<groupId>es.moki.ratelimitj</groupId>
<artifactId>ratelimitj-dropwizard</artifactId>
<version>${ratelimitj-dropwizard.version}</version>
</dependency>
<dependency>
<groupId>es.moki.ratelimitj</groupId>
<artifactId>ratelimitj-redis</artifactId>
<version>${ratelimitj-redis.version}</version>
</dependency>
public class RateLimitApplication extends Application<Configuration> {
public void initialize(Bootstrap<Configuration> bootstrap) {
RedisClient redisClient = RedisClient.create("redis://localhost:7006");
RequestRateLimiterFactory factory = new RedisRateLimiterFactory(redisClient);
bootstrap.addBundle(new RateLimitBundle(factory));
}
}
public class RateLimitApplication extends Application<Configuration> {
public void initialize(Bootstrap<Configuration> bootstrap) {
RequestRateLimiterFactory factory = new InMemoryRateLimiterFactory();
bootstrap.addBundle(new RateLimitBundle(factory));
}
}
@Path("/user")
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {
@GET
@Path("/{id}")
@RateLimited(keys = { KeyPart.AUTHENTICATED }, rates = {@Rate(duration = 10, timeUnit = TimeUnit.HOURS, limit = 10)})
public Response getLimitedByAuthenticatedUser(@Auth PrincipalImpl principle, @PathParam("id") final Integer id) {
return Response.ok().build();
}
}
@Path("/user")
@Consumes(MediaType.APPLICATION_JSON)
public class UserResource {
@GET
@Path("/{id}")
@RateLimited(groupKeyPrefix = "group-name", keys = KeyPart.AUTHENTICATED, rates = {@Rate(duration = 10, timeUnit = TimeUnit.HOURS, limit = 10)})
public Response getSomething(final Integer id) {
return Response.ok().build();
}
@GET
@Path("/{id}")
@RateLimited(groupKeyPrefix = "group-name", keys = KeyPart.AUTHENTICATED, rates = {@Rate(duration = 10, timeUnit = TimeUnit.HOURS, limit = 10)})
public Response getSomethingElse(final Integer id) {
return Response.ok().build();
}
}
When introducing rate limiters to a production environment it can be helpful to first evaluate request patterns to avoid over limiting. To disable enforcement include the 'reportOnly = true' on the @RateLimit annotation.
@POST
@RateLimited(
reportOnly = true,
key = Key.DEFAULT,
rates = { @Rate(duration = 10, timeUnit = TimeUnit.HOURS, limit = 5) })
- Java 8