Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <lenon@athenna.io>",
Expand Down
15 changes: 13 additions & 2 deletions src/ratelimiter/RateLimiterBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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))
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/types/RateLimitRetryCtx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down
8 changes: 3 additions & 5 deletions src/types/RateLimiterOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand Down