fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
504 stars 71 forks source link

Function for `timeWindow` #283

Closed AlexRMU closed 10 months ago

AlexRMU commented 1 year ago

Prerequisites

🚀 Feature Proposal

Add a function so that you can do a limit, for example 10 per minute AND 100 per hour AND 1000 per day.

Motivation

No response

Example

rateLimit: {
    max: async (req, key) => { /*... */ },
    timeWindow: async (req, key) => { /*... */ },
    // or
    max: {
        // key = window
        // value = max
        [60 * 1000]: 10,
        [60 * 60 * 1000]: async (req, key) => { /*... */ },
    }
}
Uzlopak commented 1 year ago

Well this feature request raises questions. What are the headers on the response you gonna set? I think it is useful what you suggest, but needs more specs. So please elaborate.

AlexRMU commented 1 year ago

Can be done as in nginx: https://serverfault.com/questions/976574/nginx-rate-limiting-using-multiple-rate-rules

You need to save the number of requests for the window separately

When requested:

mcollina commented 11 months ago

Would you like to send a Pull Request to address this issue? Remember to add unit tests.

AlexRMU commented 11 months ago

I tried, but realized that it would take a lot of architecture changes. I can help with tips or a review.

gurgunday commented 11 months ago

I think something similar can be achieved by using a function for the max parameter

mindrunner commented 10 months ago

I added function feature for timeWindow. Please have a look :)

gurgunday commented 10 months ago

Closed by #357