Skip to content

Commit fa2f2d1

Browse files
committed
feat: lock setTimeout delay to 1d
1 parent 022d143 commit fa2f2d1

File tree

4 files changed

+17
-9
lines changed

4 files changed

+17
-9
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/ratelimiter",
3-
"version": "5.6.0",
3+
"version": "5.7.0",
44
"description": "Respect the rate limit rules of API's you need to consume.",
55
"license": "MIT",
66
"author": "João Lenon <lenon@athenna.io>",

src/ratelimiter/RateLimiterBuilder.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import type {
2121
} from '#src/types'
2222

2323
import { debug } from '#src/debug'
24-
import { Macroable, Options } from '@athenna/common'
24+
import { Macroable, Options, Parser } from '@athenna/common'
2525
import { RateLimitStore } from '#src/ratelimiter/RateLimitStore'
2626
import { RateLimitTarget } from '#src/ratelimiter/RateLimitTarget'
2727
import { MissingKeyException } from '#src/exceptions/MissingKeyException'
@@ -806,7 +806,7 @@ export class RateLimiterBuilder extends Macroable {
806806
await this.tryToRunQueueItem()
807807
}
808808

809-
this.timer = setTimeout(fire, options.delay)
809+
this.timer = setTimeout(fire, this.getDelay(options.delay))
810810
}
811811

812812
/**
@@ -979,6 +979,17 @@ export class RateLimiterBuilder extends Macroable {
979979
}
980980
}
981981

982+
/**
983+
* Locks the delay to 1 day to avoid big set timeout instances.
984+
*/
985+
private getDelay(delay: number) {
986+
if (delay > Parser.timeToMs('1d')) {
987+
return Parser.timeToMs('1d')
988+
}
989+
990+
return delay
991+
}
992+
982993
/**
983994
* Run the pending closure defined by user.
984995
*/

src/types/RateLimiterOptions.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
* file that was distributed with this source code.
88
*/
99

10-
import type {
11-
RateLimitRule,
12-
RateLimitRetryClosure
13-
} from '#src/types'
10+
import type { RateLimitRule, RateLimitRetryClosure } from '#src/types'
1411

1512
import type { RateLimitStore } from '#src/ratelimiter/RateLimitStore'
1613
import type { RateLimitTarget } from '#src/ratelimiter/RateLimitTarget'

0 commit comments

Comments
 (0)