java.lang.Object
io.jooby.handler.RateLimitHandler
- All Implemented Interfaces:
Route.Aware,Route.Before,Route.Filter
Rate limit handler using https://github.com/vladimir-bukhtoyarov/bucket4j.
NOTE: bucket4j must be included as part of your project dependencies (classpath).
Example 1: 10 requests per minute
{
Bandwidth limit = Bandwidth.simple(10, Duration.ofMinutes(1));
Bucket bucket = Bucket4j.builder().addLimit(limit).build();
before(new RateLimitHandler(bucket));
}
Example 2: 10 requests per minute per IP address
{
before(new RateLimitHandler(remoteAddress -> {
Bandwidth limit = Bandwidth.simple(10, Duration.ofMinutes(1));
return Bucket4j.builder().addLimit(limit).build();
}));
}
Example 3: 10 requests per minute using an ApiKey header.
{
before(new RateLimitHandler(key -> {
Bandwidth limit = Bandwidth.simple(10, Duration.ofMinutes(1));
return Bucket4j.builder().addLimit(limit).build();
}, "ApiKey"));
}
Example 4: Rate limit in a cluster
{
// Get one of the proxy manager from bucket4j
ProxyManager<String> buckets = ...;
before(RateLimitHandler.cluster(key -> {
buckets.getProxy(key, () -> {
return Bucket4j.configurationBuilder()
.addLimit(Bandwidth.classic(100, Refill.intervally(100, Duration.ofMinutes(1))))
.build();
});
}));
}
- Since:
- 2.5.2
- Author:
- edgar
-
Constructor Summary
ConstructorsConstructorDescriptionRateLimitHandler(io.github.bucket4j.Bucket bucket) Rate limiter with a shared/global bucket.RateLimitHandler(SneakyThrows.Function<String, io.github.bucket4j.Bucket> bucketFactory) Rate limit per IP/Remote Address.RateLimitHandler(SneakyThrows.Function<String, io.github.bucket4j.Bucket> bucketFactory, SneakyThrows.Function<Context, String> classifier) Rate limiter with a custom key provider.RateLimitHandler(SneakyThrows.Function<String, io.github.bucket4j.Bucket> bucketFactory, String headerName) Rate limit per header key. -
Method Summary
Modifier and TypeMethodDescriptionvoidExecute application code before next handler.static RateLimitHandlercluster(SneakyThrows.Function<String, io.github.bucket4j.Bucket> proxyManager) Rate limiter per IP/Remote address using a cluster.static RateLimitHandlercluster(SneakyThrows.Function<String, io.github.bucket4j.Bucket> proxyManager, SneakyThrows.Function<Context, String> classifier) Rate limiter per key using a cluster.static RateLimitHandlercluster(SneakyThrows.Function<String, io.github.bucket4j.Bucket> proxyManager, String headerName) Rate limiter per header key using a cluster.Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.jooby.Route.Aware
setRouteMethods inherited from interface io.jooby.Route.Before
apply, then, thenMethods inherited from interface io.jooby.Route.Filter
then
-
Constructor Details
-
RateLimitHandler
public RateLimitHandler(@NonNull SneakyThrows.Function<String, io.github.bucket4j.Bucket> bucketFactory) Rate limit per IP/Remote Address.- Parameters:
bucketFactory- Bucket factory.
-
RateLimitHandler
public RateLimitHandler(@NonNull SneakyThrows.Function<String, io.github.bucket4j.Bucket> bucketFactory, @NonNull String headerName) Rate limit per header key.- Parameters:
bucketFactory- Bucket factory.headerName- Header to use as key.
-
RateLimitHandler
public RateLimitHandler(@NonNull SneakyThrows.Function<String, io.github.bucket4j.Bucket> bucketFactory, @NonNull SneakyThrows.Function<Context, String> classifier) Rate limiter with a custom key provider.- Parameters:
bucketFactory- Bucket factory.classifier- Key provider.
-
RateLimitHandler
public RateLimitHandler(@NonNull io.github.bucket4j.Bucket bucket) Rate limiter with a shared/global bucket.- Parameters:
bucket- Bucket to use.
-
-
Method Details
-
cluster
@NonNull public static RateLimitHandler cluster(@NonNull SneakyThrows.Function<String, io.github.bucket4j.Bucket> proxyManager) Rate limiter per IP/Remote address using a cluster.- Parameters:
proxyManager- Cluster bucket configuration.- Returns:
- Rate limiter.
-
cluster
@NonNull public static RateLimitHandler cluster(@NonNull SneakyThrows.Function<String, io.github.bucket4j.Bucket> proxyManager, @NonNull String headerName) Rate limiter per header key using a cluster.- Parameters:
proxyManager- Cluster bucket configuration.headerName- Header to use as key.- Returns:
- Rate limiter.
-
cluster
public static RateLimitHandler cluster(@NonNull SneakyThrows.Function<String, io.github.bucket4j.Bucket> proxyManager, @NonNull SneakyThrows.Function<Context, String> classifier) Rate limiter per key using a cluster.- Parameters:
proxyManager- Cluster bucket configuration.classifier- Key provider.- Returns:
- Rate limiter.
-
apply
Description copied from interface:Route.BeforeExecute application code before next handler.- Specified by:
applyin interfaceRoute.Before- Parameters:
ctx- Web context.- Throws:
Exception- If something goes wrong.
-