fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
483 stars 67 forks source link

Time window with exponential backoff #279

Open KaranH15 opened 1 year ago

KaranH15 commented 1 year ago

Prerequisites

🚀 Feature Proposal

Right now in timeWindow option we can set retries for regular interval of time. There is no option for allowing exponential backoff time. E.g If I want user to retry for following time windows - 1min, 5 min, 15min ........ No option to configure that rn.

Motivation

No response

Example

No response

climba03003 commented 1 year ago

The current implementation do not keep how many times for an ip to be banned. Those data would require a persistence storage other than redis.

I think the idea good, but not sure if it worth for storing such data.

Uzlopak commented 1 year ago

Just because I have the code currently open in my vscode: Exponential back off can be implemented by the following code:

/**
 * Exponential backoff is the process of a client periodically retrying a
 * failed request over an increasing amount of time. It is a standard error
 * handling strategy for network applications. The Core Reporting API is
 * designed with the expectation that clients which choose to retry failed
 * requests do so using exponential backoff. Besides being "required", using
 * exponential backoff increases the efficiency of bandwidth usage, reduces
 * the number of requests required to get a successful response, and
 * maximizes the throughput of requests in concurrent environments.
 *
 * @see https://developers.google.com/analytics/devguides/reporting/core/v3/errors#backoff
 */
function exponentialDelay(retryNumber) {
    const delayInSeconds = Math.pow(2, retryNumber)
    const randomMs = ~~(1000 * Math.random())
    return delayInSeconds * 1000 + randomMs
}