Class RedisRateLimiter
java.lang.Object
org.springframework.cloud.gateway.support.AbstractConfigurable<RedisRateLimiter.Config>
org.springframework.cloud.gateway.support.AbstractStatefulConfigurable<RedisRateLimiter.Config>
org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter<RedisRateLimiter.Config>
org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter
- All Implemented Interfaces:
EventListener, org.springframework.beans.factory.Aware, RateLimiter<RedisRateLimiter.Config>, Configurable<RedisRateLimiter.Config>, StatefulConfigurable<RedisRateLimiter.Config>, org.springframework.context.ApplicationContextAware, org.springframework.context.ApplicationListener<FilterArgsEvent>
@ConfigurationProperties("spring.cloud.gateway.server.webflux.redis-rate-limiter")
public class RedisRateLimiter
extends AbstractRateLimiter<RedisRateLimiter.Config>
implements org.springframework.context.ApplicationContextAware
See https://stripe.com/blog/rate-limiters and
https://gist.github.com/ptarjan/e38f45f2dfe601419ca3af937fff574d#file-1-check_request_rate_limiter-rb-L11-L34.
- Author:
- Spencer Gibb, Ronny Bräunlich, Denis Cutic, Andrey Muchnik
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface RateLimiter
RateLimiter.Response -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringBurst Capacity header name.static final StringRedis Rate Limiter property name.static final StringRedis Script name.static final StringRemaining Rate Limit header name.static final StringReplenish Rate Limit header name.static final StringRequested Tokens header name. -
Constructor Summary
ConstructorsConstructorDescriptionRedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity) This creates an instance with default static configuration, useful in Java DSL.RedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity, int defaultRequestedTokens) This creates an instance with default static configuration, useful in Java DSL.RedisRateLimiter(org.springframework.data.redis.core.ReactiveStringRedisTemplate redisTemplate, org.springframework.data.redis.core.script.RedisScript<List<Long>> script, ConfigurationService configurationService) -
Method Summary
Modifier and TypeMethodDescriptiongetHeaders(RedisRateLimiter.Config config, Long tokensLeft) reactor.core.publisher.Mono<RateLimiter.Response> This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically.booleanvoidsetApplicationContext(org.springframework.context.ApplicationContext context) Used when setting default configuration in constructor.voidsetBurstCapacityHeader(String burstCapacityHeader) voidsetIncludeHeaders(boolean includeHeaders) voidsetRemainingHeader(String remainingHeader) voidsetReplenishRateHeader(String replenishRateHeader) voidsetRequestedTokensHeader(String requestedTokensHeader) Methods inherited from class AbstractRateLimiter
getConfigurationPropertyName, onApplicationEvent, setConfigurationService, toStringMethods inherited from class AbstractStatefulConfigurable
getConfigMethods inherited from class AbstractConfigurable
getConfigClass, newConfigMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.context.ApplicationListener
supportsAsyncExecutionMethods inherited from interface Configurable
getConfigClass, newConfigMethods inherited from interface StatefulConfigurable
getConfig
-
Field Details
-
CONFIGURATION_PROPERTY_NAME
-
REDIS_SCRIPT_NAME
-
REMAINING_HEADER
-
REPLENISH_RATE_HEADER
-
BURST_CAPACITY_HEADER
-
REQUESTED_TOKENS_HEADER
-
-
Constructor Details
-
RedisRateLimiter
public RedisRateLimiter(org.springframework.data.redis.core.ReactiveStringRedisTemplate redisTemplate, org.springframework.data.redis.core.script.RedisScript<List<Long>> script, ConfigurationService configurationService) -
RedisRateLimiter
public RedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity) This creates an instance with default static configuration, useful in Java DSL.- Parameters:
defaultReplenishRate- how many tokens per second in token-bucket algorithm.defaultBurstCapacity- how many tokens the bucket can hold in token-bucket algorithm.
-
RedisRateLimiter
public RedisRateLimiter(int defaultReplenishRate, long defaultBurstCapacity, int defaultRequestedTokens) This creates an instance with default static configuration, useful in Java DSL.- Parameters:
defaultReplenishRate- how many tokens per second in token-bucket algorithm.defaultBurstCapacity- how many tokens the bucket can hold in token-bucket algorithm.defaultRequestedTokens- how many tokens are requested per request.
-
-
Method Details
-
isIncludeHeaders
public boolean isIncludeHeaders() -
setIncludeHeaders
public void setIncludeHeaders(boolean includeHeaders) -
getRemainingHeader
-
setRemainingHeader
-
getReplenishRateHeader
-
setReplenishRateHeader
-
getBurstCapacityHeader
-
setBurstCapacityHeader
-
getRequestedTokensHeader
-
setRequestedTokensHeader
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext context) throws org.springframework.beans.BeansException Used when setting default configuration in constructor.- Specified by:
setApplicationContextin interfaceorg.springframework.context.ApplicationContextAware- Parameters:
context- the ApplicationContext object to be used by this object- Throws:
org.springframework.beans.BeansException- if thrown by application context methods
-
isAllowed
This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.- Specified by:
isAllowedin interfaceRateLimiter<RedisRateLimiter.Config>
-
getHeaders
-