Class 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
  • Field Details

  • 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

      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 interface org.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 interface RateLimiter<RedisRateLimiter.Config>
    • getHeaders

      public Map<String,String> getHeaders(RedisRateLimiter.Config config, Long tokensLeft)