public class RateLimitHandler extends Object implements Route.Before
{
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();
});
}));
}
Constructor and Description |
---|
RateLimitHandler(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.
|
Modifier and Type | Method and Description |
---|---|
void |
apply(Context ctx)
Execute application code before next handler.
|
static RateLimitHandler |
cluster(SneakyThrows.Function<String,io.github.bucket4j.Bucket> proxyManager)
Rate limiter per IP/Remote address using a cluster.
|
static RateLimitHandler |
cluster(SneakyThrows.Function<String,io.github.bucket4j.Bucket> proxyManager,
SneakyThrows.Function<Context,String> classifier)
Rate limiter per key using a cluster.
|
static RateLimitHandler |
cluster(SneakyThrows.Function<String,io.github.bucket4j.Bucket> proxyManager,
String headerName)
Rate limiter per header key using a cluster.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
then, then
public RateLimitHandler(@Nonnull SneakyThrows.Function<String,io.github.bucket4j.Bucket> bucketFactory)
bucketFactory
- Bucket factory.public RateLimitHandler(@Nonnull SneakyThrows.Function<String,io.github.bucket4j.Bucket> bucketFactory, @Nonnull String headerName)
bucketFactory
- Bucket factory.headerName
- Header to use as key.public RateLimitHandler(@Nonnull SneakyThrows.Function<String,io.github.bucket4j.Bucket> bucketFactory, @Nonnull SneakyThrows.Function<Context,String> classifier)
bucketFactory
- Bucket factory.classifier
- Key provider.public RateLimitHandler(@Nonnull io.github.bucket4j.Bucket bucket)
bucket
- Bucket to use.@Nonnull public static RateLimitHandler cluster(@Nonnull SneakyThrows.Function<String,io.github.bucket4j.Bucket> proxyManager)
proxyManager
- Cluster bucket configuration.@Nonnull public static RateLimitHandler cluster(@Nonnull SneakyThrows.Function<String,io.github.bucket4j.Bucket> proxyManager, @Nonnull String headerName)
proxyManager
- Cluster bucket configuration.headerName
- Header to use as key.public static RateLimitHandler cluster(@Nonnull SneakyThrows.Function<String,io.github.bucket4j.Bucket> proxyManager, @Nonnull SneakyThrows.Function<Context,String> classifier)
proxyManager
- Cluster bucket configuration.classifier
- Key provider.public void apply(@Nonnull Context ctx) throws Exception
Route.Before
apply
in interface Route.Before
ctx
- Web context.Exception
- If something goes wrong.Copyright © 2020. All rights reserved.