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 Classes Modifier and Type Class Description static class
RedisRateLimiter.Config
-
Nested classes/interfaces inherited from interface org.springframework.cloud.gateway.filter.ratelimit.RateLimiter
RateLimiter.Response
-
-
Field Summary
Fields Modifier and Type Field Description static String
BURST_CAPACITY_HEADER
Burst Capacity header name.static String
CONFIGURATION_PROPERTY_NAME
Redis Rate Limiter property name.static String
REDIS_SCRIPT_NAME
Redis Script name.static String
REMAINING_HEADER
Remaining Rate Limit header name.static String
REPLENISH_RATE_HEADER
Replenish Rate Limit header name.static String
REQUESTED_TOKENS_HEADER
Requested Tokens header name.
-
Constructor Summary
Constructors Constructor Description RedisRateLimiter(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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getBurstCapacityHeader()
Map<String,String>
getHeaders(RedisRateLimiter.Config config, Long tokensLeft)
String
getRemainingHeader()
String
getReplenishRateHeader()
String
getRequestedTokensHeader()
reactor.core.publisher.Mono<RateLimiter.Response>
isAllowed(String routeId, String id)
This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically.boolean
isIncludeHeaders()
void
setApplicationContext(org.springframework.context.ApplicationContext context)
Used when setting default configuration in constructor.void
setBurstCapacityHeader(String burstCapacityHeader)
void
setIncludeHeaders(boolean includeHeaders)
void
setRemainingHeader(String remainingHeader)
void
setReplenishRateHeader(String replenishRateHeader)
void
setRequestedTokensHeader(String requestedTokensHeader)
-
Methods inherited from class org.springframework.cloud.gateway.filter.ratelimit.AbstractRateLimiter
getConfigurationPropertyName, onApplicationEvent, setConfigurationService, toString
-
Methods inherited from class org.springframework.cloud.gateway.support.AbstractStatefulConfigurable
getConfig
-
Methods inherited from class org.springframework.cloud.gateway.support.AbstractConfigurable
getConfigClass, newConfig
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.springframework.cloud.gateway.support.Configurable
getConfigClass, newConfig
-
Methods inherited from interface org.springframework.cloud.gateway.support.StatefulConfigurable
getConfig
-
-
-
-
Field Detail
-
CONFIGURATION_PROPERTY_NAME
public static final String CONFIGURATION_PROPERTY_NAME
Redis Rate Limiter property name.- See Also:
- Constant Field Values
-
REDIS_SCRIPT_NAME
public static final String REDIS_SCRIPT_NAME
Redis Script name.- See Also:
- Constant Field Values
-
REMAINING_HEADER
public static final String REMAINING_HEADER
Remaining Rate Limit header name.- See Also:
- Constant Field Values
-
REPLENISH_RATE_HEADER
public static final String REPLENISH_RATE_HEADER
Replenish Rate Limit header name.- See Also:
- Constant Field Values
-
BURST_CAPACITY_HEADER
public static final String BURST_CAPACITY_HEADER
Burst Capacity header name.- See Also:
- Constant Field Values
-
REQUESTED_TOKENS_HEADER
public static final String REQUESTED_TOKENS_HEADER
Requested Tokens header name.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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 Detail
-
isIncludeHeaders
public boolean isIncludeHeaders()
-
setIncludeHeaders
public void setIncludeHeaders(boolean includeHeaders)
-
getRemainingHeader
public String getRemainingHeader()
-
setRemainingHeader
public void setRemainingHeader(String remainingHeader)
-
getReplenishRateHeader
public String getReplenishRateHeader()
-
setReplenishRateHeader
public void setReplenishRateHeader(String replenishRateHeader)
-
getBurstCapacityHeader
public String getBurstCapacityHeader()
-
setBurstCapacityHeader
public void setBurstCapacityHeader(String burstCapacityHeader)
-
getRequestedTokensHeader
public String getRequestedTokensHeader()
-
setRequestedTokensHeader
public void setRequestedTokensHeader(String requestedTokensHeader)
-
setApplicationContext
public void setApplicationContext(org.springframework.context.ApplicationContext context) throws org.springframework.beans.BeansException
Used when setting default configuration in constructor.- Specified by:
setApplicationContext
in 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
public reactor.core.publisher.Mono<RateLimiter.Response> isAllowed(String routeId, String id)
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:
isAllowed
in interfaceRateLimiter<RedisRateLimiter.Config>
-
getHeaders
public Map<String,String> getHeaders(RedisRateLimiter.Config config, Long tokensLeft)
-
-