Class RedisRateLimiter
java.lang.Object
org.springframework.cloud.gateway.support.AbstractConfigurable<C>
org.springframework.cloud.gateway.support.AbstractStatefulConfigurable<C>
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.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
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface org.springframework.cloud.gateway.filter.ratelimit.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, int defaultBurstCapacity) This creates an instance with default static configuration, useful in Java DSL.RedisRateLimiter(int defaultReplenishRate, int 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 org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter
getConfigurationPropertyName, onApplicationEvent, setConfigurationService, toStringMethods inherited from class org.springframework.cloud.gateway.support.AbstractStatefulConfigurable
getConfigMethods inherited from class org.springframework.cloud.gateway.support.AbstractConfigurable
getConfigClass, newConfigMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.springframework.cloud.gateway.support.Configurable
getConfigClass, newConfigMethods inherited from interface org.springframework.cloud.gateway.support.StatefulConfigurable
getConfig
-
Field Details
-
CONFIGURATION_PROPERTY_NAME
Redis Rate Limiter property name.- See Also:
-
REDIS_SCRIPT_NAME
Redis Script name.- See Also:
-
REMAINING_HEADER
Remaining Rate Limit header name.- See Also:
-
REPLENISH_RATE_HEADER
Replenish Rate Limit header name.- See Also:
-
BURST_CAPACITY_HEADER
Burst Capacity header name.- See Also:
-
REQUESTED_TOKENS_HEADER
Requested Tokens header name.- See Also:
-
-
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, int 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, int 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
-