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.53k stars 3.33k forks source link

Make RedisRateLimiter extensible? #393

Open pavlo opened 6 years ago

pavlo commented 6 years ago

Hey guys!

In order to be able to rate limit per user API key (versus basing the limit on the route) I had to extend RedisRateLimiter to make isAllowed(String routeId, String apiKey) calculate the limits based up on the route and the apiKey. Please see the full thing here: https://stackoverflow.com/questions/50988095/rate-limiting-based-on-user-plan-in-spring-cloud-gateway

As you see it is somewhat hackish there because there's no good way to extend RedisRateLimiter - see I had to decorate it, copy some private/default scoped methods over as well as hack the @Primary annotation so it gets into the bean factory well.

Do you think RedisRateLimiter would be re-architected to be easier to extend? Thanks!

spencergibb commented 6 years ago

I suppose an interface that could be implemented to determine replenish rate and burst capacity could be useful. I'm not keen on having folks extend the rate limiter.

codependent commented 4 years ago

@spencergibb What about an out-of-the-box implementation that allows to have different rate/burst limits per API key as @pavlo pointed out? I think it's a quite common use case.