A dropwizard bundle for rate limiting APIs
- Rate limiting a group of APIs
- Rate limiting individual APIs
- Rate limiting individual APIs based on clients using X-Client-Id
YAML file containing application configuration
rateLimiter:
groupLimits:
group: 15
namedLimits:
client1.ratelimit: 5
public class RateLimitApp extends Application<RateLimitConfiguration> {
@Override
public void initialize(Bootstrap<RateLimitConfiguration> bootstrap) {
super.initialize(bootstrap);
bootstrap.addBundle(new RateLimitBundle<RateLimitConfiguration>() {
@Override
protected RateLimitBundleConfiguration getRateLimitBundleConfiguration(RateLimitConfiguration configuration) {
return configuration.getRateLimitBundleConfiguration();
}
});
}
public void run(RateLimitConfiguration rateLimitConfiguration, Environment environment) throws Exception {
environment.jersey().register(new RateLimitDemoResource());
environment.jersey().register(new RateLimitByGroupDemoResource());
environment.jersey().register(new RateLimitByHeaderDemoResource());
}
}
public class RateLimitConfiguration extends Configuration {
private RateLimitBundleConfiguration rateLimitBundleConfiguration;
@JsonProperty("rateLimiter")
public RateLimitBundleConfiguration getRateLimitBundleConfiguration() {
return rateLimitBundleConfiguration;
}
public void setRateLimitBundleConfiguration(RateLimitBundleConfiguration rateLimitBundleConfiguration) {
this.rateLimitBundleConfiguration = rateLimitBundleConfiguration;
}
}
@Path("/ratelimit")
public class RateLimitDemoResource {
@GET
@RateLimit(ratePerSecond = 3)
public Response getSomething(){
return Response.ok().build();
}
}
@Path("/ratelimitbygroup")
public class RateLimitByGroupDemoResource {
@GET
@RateLimitByGroup("group")
public Response getSomething(){
return Response.ok().build();
}
}
@Path("/ratelimitbygroup2")
public class RateLimitByGroupDemoResource2 {
@GET
@RateLimitByGroup("group")
public Response getSomething2(){
return Response.ok().build();
}
}
getSomething() and getSomething2() put together has rate limit of 15.
@Path("/ratelimitbyheader")
public class RateLimitByHeaderDemoResource {
@GET
@RateLimitByHeader(header = "X-Client-Id", rateLimits = {@HeaderValue(value = "client1", nameLimit = "client1.ratelimit"),@HeaderValue(value = "client2", ratePerSecond = 20)})
public Response getSomething(){
return Response.ok().build();
}
}