diff --git a/package-lock.json b/package-lock.json index f66355c..aa7788e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/ratelimiter", - "version": "5.6.0", + "version": "5.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/ratelimiter", - "version": "5.6.0", + "version": "5.7.0", "license": "MIT", "devDependencies": { "@athenna/cache": "^5.2.0", diff --git a/package.json b/package.json index 88d1a1d..d93375a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/ratelimiter", - "version": "5.6.0", + "version": "5.7.0", "description": "Respect the rate limit rules of API's you need to consume.", "license": "MIT", "author": "João Lenon ", diff --git a/src/ratelimiter/RateLimiterBuilder.ts b/src/ratelimiter/RateLimiterBuilder.ts index af42744..fdb9c16 100644 --- a/src/ratelimiter/RateLimiterBuilder.ts +++ b/src/ratelimiter/RateLimiterBuilder.ts @@ -21,7 +21,7 @@ import type { } from '#src/types' import { debug } from '#src/debug' -import { Macroable, Options } from '@athenna/common' +import { Macroable, Options, Parser } from '@athenna/common' import { RateLimitStore } from '#src/ratelimiter/RateLimitStore' import { RateLimitTarget } from '#src/ratelimiter/RateLimitTarget' import { MissingKeyException } from '#src/exceptions/MissingKeyException' @@ -806,7 +806,7 @@ export class RateLimiterBuilder extends Macroable { await this.tryToRunQueueItem() } - this.timer = setTimeout(fire, options.delay) + this.timer = setTimeout(fire, this.getDelay(options.delay)) } /** @@ -979,6 +979,17 @@ export class RateLimiterBuilder extends Macroable { } } + /** + * Locks the delay to 1 day to avoid big set timeout instances. + */ + private getDelay(delay: number) { + if (delay > Parser.timeToMs('1d')) { + return Parser.timeToMs('1d') + } + + return delay + } + /** * Run the pending closure defined by user. */ diff --git a/src/types/RateLimitRetryCtx.ts b/src/types/RateLimitRetryCtx.ts index e54f32f..212ab8a 100644 --- a/src/types/RateLimitRetryCtx.ts +++ b/src/types/RateLimitRetryCtx.ts @@ -7,7 +7,7 @@ * file that was distributed with this source code. */ -import type { RateLimitTarget } from '#src/types' +import type { RateLimitTarget } from '#src/ratelimiter/RateLimitTarget' export type RateLimitRetryCtx = { /** diff --git a/src/types/RateLimiterOptions.ts b/src/types/RateLimiterOptions.ts index 239f5ea..c43ece9 100644 --- a/src/types/RateLimiterOptions.ts +++ b/src/types/RateLimiterOptions.ts @@ -7,12 +7,10 @@ * file that was distributed with this source code. */ -import type { - RateLimitRule, - RateLimitTarget, - RateLimitRetryClosure -} from '#src/types' +import type { RateLimitRule, RateLimitRetryClosure } from '#src/types' + import type { RateLimitStore } from '#src/ratelimiter/RateLimitStore' +import type { RateLimitTarget } from '#src/ratelimiter/RateLimitTarget' export type RateLimiterOptions = { /**