Spring-cloud-gateway: RateLimiter is not working

Created on 31 May 2018  Â·  10Comments  Â·  Source: spring-cloud/spring-cloud-gateway

Apart from redis connection, do we have to do anything else for a basic rate limiter to work? As I've been trying to create a rate limiter but it is not working as it is supposed to.

Rate limiter :
RedisRateLimiter redisRateLimiter() { return new RedisRateLimiter(1, 2); }

Portion of my route:
.route("redis_rate_check", r->r.path("/redisrate") .and().method(HttpMethod.GET) .filters(f -> f.requestRateLimiter(rate -> rate .setRateLimiter(redisRateLimiter())) ) .uri("http://localhost:9081/rate"))

Most helpful comment

@jayendra155 never tested using code configuration, but I guess you need a KeyResolver somewhere. I think the default one limits based on the Principal authentified. Maybe that is your problem.

@stevenNzonzi Rate limiter configuration doesnt work using shortcut form (see #167 ). Here is 2 working configuration:

Using a rate limiter bean:
````
filters:

  • name: RequestRateLimiter
    args:
    key-resolver: "#{@myKeyResolver}"
    rate-limiter: "#{@myRateLimiter}"
    ````

Hardcoding the configuration:
````
filters:

  • name: RequestRateLimiter
    args:
    key-resolver: "#{@myKeyResolver}"
    redis-rate-limiter.replenishRate: 1
    redis-rate-limiter.burstCapacity: 5
    ````

All 10 comments

I would like to know that too, whenever I use the rate limiter, my test endpoint returns a blank body with a 200 ok.
This is the bean that I created, nothing special,

@Bean
KeyResolver testKeyResolver() {
    return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("test"));
}

And this is my config file,

spring:
  cloud:
    gateway:
      routes:
      - id: redirectRoute
        uri: http://localhost:9081/getname
        predicates:
        - Path=/path
        filters:
        - RequestRateLimiter=100, 100, #{@testKeyResolver}
      - id: HeaderMatching
        uri: http://localhost:9080/world
        predicates:
        - Path=/header
        - Header=X-Request-Id, \d+
      - id: HystrixExample
        uri: http://localhost:9081/getname
        predicates:
        - Path=/checkfallback
        filters:
        - name: Hystrix
          args:
            name: fallbackcmd
            fallbackUri: forward:/world
      - id: MethodMatching
        uri: http://localhost:9080/world
        predicates:
        - Path=/post
        - Method=POST

This is the log I get in DEBUG mode

2018-06-05 17:10:03:364PM DEBUG [reactor-http-server-epoll-7] [o.s.c.g.h.RoutePredicateHandlerMapping] Mapping [Exchange: GET http://localhost:9080/path] to Route{id='redirectRoute', uri=http://localhost:9081/getname, order=0, predicate=org.springframework.cloud.gateway.handler.predicate.PathRoutePredicateFactory$$Lambda$669/388424292@65a2dc58, gatewayFilters=[OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory$$Lambda$671/1128014181@3d34d98d, order=1}]}
2018-06-05 17:10:03:365PM DEBUG [reactor-http-server-epoll-7] [o.s.c.g.handler.FilteringWebHandler] Sorted gatewayFilterFactories: [OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@698d6d30}, order=-1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardPathFilter@538b3c88}, order=0}, OrderedGatewayFilter{delegate=org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewayFilterFactory$$Lambda$671/1128014181@3d34d98d, order=1}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@3407aa4f}, order=10000}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@78065fcd}, order=2147483637}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@19b9f903}, order=2147483646}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.NettyRoutingFilter@13aed42b}, order=2147483647}, OrderedGatewayFilter{delegate=GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@51ed2f68}, order=2147483647}]
2018-06-05 17:10:03:367PM DEBUG [reactor-http-server-epoll-7] [o.s.h.s.r.ReactorHttpHandlerAdapter] Handling completed with success

@jayendra155 never tested using code configuration, but I guess you need a KeyResolver somewhere. I think the default one limits based on the Principal authentified. Maybe that is your problem.

@stevenNzonzi Rate limiter configuration doesnt work using shortcut form (see #167 ). Here is 2 working configuration:

Using a rate limiter bean:
````
filters:

  • name: RequestRateLimiter
    args:
    key-resolver: "#{@myKeyResolver}"
    rate-limiter: "#{@myRateLimiter}"
    ````

Hardcoding the configuration:
````
filters:

  • name: RequestRateLimiter
    args:
    key-resolver: "#{@myKeyResolver}"
    redis-rate-limiter.replenishRate: 1
    redis-rate-limiter.burstCapacity: 5
    ````

Thank you for the input, the page body is showing up now.
But the issue is rate limiting is not happening. Do I need to do anything else other than defining the bean and the configuration?

It's working now, there was an issue with the bean

    @Bean
    public KeyResolver testResolver(){
            return exchange -> Mono.just(exchange.getSession().subscribe().toString());
    }

and the configuration I used was

filters:
- name: RequestRateLimiter
  args:
    key-resolver: "#{@testResolver}"
    redis-rate-limiter.replenishRate: 1
    redis-rate-limiter.burstCapacity: 5

Thanks @stevenNzonzi @mathieufortin01 it's working now

requestrate limiter is not working for me..can someone help

@ronperei what is the issue you are facing ? please share your config and the exception you are getting if any

HI,

Config used :

spring:
cloud:
gateway:
routes:
- predicates:
- Path=/dn/**
filters:
- StripPrefix=1
- RequestRateLimiter=1, 2, userKeyResolver
uri: http://localhost:8090

@Bean
public KeyResolver userKeyResolver () {
return exchange -> Mono.just(exchange.getSession().subscribe().toString());
}

Using this , my request ratelimiter is not working…

Please help

[Email_CBE.gif]Ronisa Gonsalves
Consultant | AppsTwo CSD TS
Capgemini India | Mumbai
Tel.: +91-22-67557000/2248095
www.capgemini.comhttp://www.capgemini.com/

From: Jayendra Vikram Singh [mailto:[email protected]]
Sent: Wednesday, August 29, 2018 6:29 PM
To: spring-cloud/spring-cloud-gateway
Cc: Gonsalves, ronisa; Mention
Subject: Re: [spring-cloud/spring-cloud-gateway] RateLimiter is not working (#342)

@ronpereihttps://github.com/ronperei what is the issue you are facing ? please share your config and the exception you are getting if any

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHubhttps://github.com/spring-cloud/spring-cloud-gateway/issues/342#issuecomment-416943183, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AjNeSHkFI8utb1fO0YOBoEmUaM9vAMr6ks5uVpARgaJpZM4UU6-Z.
This message contains information that may be privileged or confidential and is the property of the Capgemini Group. It is intended only for the person to whom it is addressed. If you are not the intended recipient, you are not authorized to read, print, retain, copy, disseminate, distribute, or use this message or any part thereof. If you receive this message in error, please notify the sender immediately and delete all copies of this message.

Below configuration is working for me

- id: login
uri: http://login-app:3000
order: 1
predicates:
  - Path=/login
filters:
  - name: RequestRateLimiter
    args:
      redis-rate-limiter.replenishRate: 3
      redis-rate-limiter.burstCapacity: 3
      key-resolver: "#{@userRemoteAddressResolver}"

Bean defination

@Bean(name = "userRemoteAddressResolver")
    public KeyResolver userKeyResolver() {
        return exchange -> {
            return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
        };
    }

Dependency in gradle file

compile("org.springframework.boot:spring-boot-starter-data-redis-reactive")

It's working now, there was an issue with the bean

    @Bean
    public KeyResolver testResolver(){
            return exchange -> Mono.just(exchange.getSession().subscribe().toString());
    }

and the configuration I used was

filters:
- name: RequestRateLimiter
  args:
    key-resolver: "#{@testResolver}"
    redis-rate-limiter.replenishRate: 1
    redis-rate-limiter.burstCapacity: 5

How to test rate limiter?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

re6exp picture re6exp  Â·  37Comments

zjengjie picture zjengjie  Â·  27Comments

re6exp picture re6exp  Â·  36Comments

tony-clarke-amdocs picture tony-clarke-amdocs  Â·  32Comments

spencergibb picture spencergibb  Â·  36Comments