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.5k stars 3.31k forks source link

Provide Quota Filter based on redis #1402

Open HappyTobi opened 4 years ago

HappyTobi commented 4 years ago

Hey there,

what do you think of a new quota filter (based on redis) where you can use a limit and different period "units" like: "second","minute","day","month","year","abs"

filters:
   - name: RequestQuotaFilter
      args:
         key-resolver: "principalNameKeyResolver"
         redis-quota-filter:
         limit: 10
        period: "DAY"

With a quota filter it would be easy to create one click urls, api-key urls or some other things. Filter will give you the possibility to limited a quota with periods you really need.

Currently you can only limit a quote by using the rate limiter where you can't setup a fine-grained quota (because the filter is not for setting up a quota check 😀).

So if you like the idea, i would like to implement the new Filter that can handle specific quotas for routes / service. To use it in a more generic way you can add your custom key-resolver (like the rate limiter).

What do you think?

If you like it, I can bring that feature / filter in.

Kind regards

EstebanDugueperoux2 commented 4 years ago

Hello,

Is PR #1432 replaces PR #1433? I'm interested by your work and more especially by a mean to define for a OAuth2 client_id differents rateLimit/Quota with different time periods. For example, for a OAuth2 client_id a limit of 40 requests/minutes plus a limit of 10.000 requests/day and a limit of 50.000 requests/month. Instead of having a second period specific rate limiter could we have a more generic one with a specified time period? Because If I don't mistake, the only difference between Quota and Rate Limiting is the defined period of time. A short time for rate limiting and a long period for Quota.

And instead of using a java.util.concurrent.TimeUnit, could we use a java.time.Duration or a java.time.Period, both inheriting from java.time.temporal.TemporalAmount?

Best Regards.

HappyTobi commented 4 years ago

Hi @EstebanDugueperoux2

The PR #1433 replaces the Is PR #1432 . There is more than just period also the logic is different.

For me both filters are complete different.

Kind Regards.

EstebanDugueperoux2 commented 4 years ago

Hello HappyTobi,

Ok I understand with rateLimiting the period is small and is a sliding window while with quota the period can be high and with a fixed window.

When testing your PR, I get tokensRemaining becoming negative, is it expected?

Best Regards.

HappyTobi commented 4 years ago

Hi @EstebanDugueperoux2,

i changed that, think it's better to return 0 instead of -1.

Kind regards

liemng commented 1 month ago

Is there a reason why this was not merged? Seems like a very useful feature to have...