dropwizard-ratelimit icon indicating copy to clipboard operation
dropwizard-ratelimit copied to clipboard

A redis-backed rate limiter for dropwizard

Note: this project is no longer maintained

Build Status Coverage Status

dropwizard-ratelimit is a dropwizard bundle that allows redis-backed rate limiting for your resources. It performs sliding-window rate limiting.

Requires Dropwizard ~0.8.0. Not tested on 1.0.

Usage

Add the following dependency to your pom.xml

<dependency>
  <groupId>ca.nlap.dropwizard-ratelimit</groupId>
  <artifactId>bundle</artifactId>
  <version>0.0.1</version>
</dependency>

Now add the bundle:

private RateLimitBundle<ExampleServiceConfiguration> rateLimitBundle = new RateLimitBundle<ExampleServiceConfiguration>() {
	@Override
	public RateLimitConfiguration getRateLimitConfiguration(
			ExampleServiceConfiguration configuration) {
		return configuration.getRateLimit();
	}
};

@Override
public void initialize(Bootstrap<ExampleConfiguration> bootstrap) {
	bootstrap.addBundle(rateLimitBundle);
}

Finally, annotate any resources you want to protect with @RateLimited:

import ca.nlap.dropwizard.ratelimit.RateLimited;
...

@Path("/")
public class ExampleResource {

	@GET
	@RateLimited
	public Response getResource() {
		return new Response.ok().build();
	}

}

Configuration

Configure as per the example conf.yaml. The properties are:

  • redisHost: Redis hostname
  • redisPort: Redis port

Example

The module example contains an API with a single resource that simply returns 200 OK. This is rate limited to 3 requests per second.

Start and configure redis. Run run-example.sh to start the service and test-example.sh to run the test. Multiple requests will be sent, demonstrating the rate limiting:

Hello
Hello
Hello
Too Many Requests

Open Work

The following is not yet available. Contributions are welcomed.

  • Per resource configuration

Contributions

Adapts code from https://github.com/anastasop/dropwizard-jedis
Rate limitng lua script from https://gist.github.com/josiahcarlson/80584b49da41549a7d5c