spring-cloud / spring-cloud-gateway

An API Gateway built on Spring Framework and Spring Boot providing routing and more.
http://cloud.spring.io
Apache License 2.0
4.52k stars 3.32k forks source link

Dynamic rate limits per route. #2450

Open JavierBrRuiz opened 2 years ago

JavierBrRuiz commented 2 years ago

Is your feature request related to a problem? Please describe. I would like to implement something like a dynamic rate limiter. This is, suppose you have an exposed service, but you have observed that this service has some request's peaks at determined intervals of time.

For example you might have a service that receives 90% of the time an average of X request per minute (rpm) per origin but you have noticed that the 10% left occurs always between 9am and 10am and this 10% has an average of 2X rpm and this is normal because of the content the service serves, so you want to allow this traffic.

My question is if it is possible to make/use a RequestRedisRateLimiter filter to response dynamically depending on time of the day, day of the week, etc.

Describe the solution you'd like I would like to establish a route gateway like the following:

builder.routes().route(p -> p
                    .path("/api")
                    .and()
                    .header("Authorization")
                    .and()
                    .filters(f ->  f.requestRateLimiter().configure(
                                                          c->c.setRateLimiter(this.redisRateLimiter()));})
                    .uri("http://localhost:8080/api"))
                                        .build()

where once the gateway receives a request, the RedisRateLimiter will allow or deny the request but with a different limit/burst depending on the time of the day.

Describe alternatives you've considered I have been knocking my head trying to write my own filter based on RequestRateLimiterGatewayFilter, establishing multiples routes (like the code above) and adding custom predicates where the predicates check for the time day and give only access to the gateway with the right route.

None of them have worked, either because I have not the knowledge to achieve this dynamic rate fitler solution or because it is not even possible with spring cloud gateway redis rate limiting.

Additional context To me it does not sound as a bizarre idea, it is a quite simple concept. From monday to friday I have an average traffic so I establish a rate limiter based on this traffic. These same days, between 9am and 10am my traffic suffers a peak of request so I want my service to increase its limits and bursts. Anyway I have not find nothing similar on the web.

If anyone could help with ideas or possible solutions/workarounds it would be great!

ctlove0523 commented 2 years ago

@JavierBrRuiz dynamic change rate limit value in redis?

JavierBrRuiz commented 2 years ago

@ctlove0523 I do not know if this can be done but yes, the idea is to be able to change programmatically over time the limit and burst parameters set to a specific route, depending for example on what time of the day it is.

mxsoftwarelabs commented 2 years ago

I have a similar requirements, based on the package they have purchased to access APIs. This will allow the sales to sell custom API call rates and allow users to upgrade their packages if they wish to call the APIs at higher rate.

Rate limit configuration being burnt into rate limiter at the time of initialization makes it bit tricky.

adrianliz commented 1 year ago

Me too. I have one requirement to limit based on user type. I ended up with custom extension of RedisRateLimiter, but it would be awesome that Spring provide a customization.

kdhrubo commented 1 year ago

+1 Much needed feature

saurabh-slacklife commented 8 months ago

+1