-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathlimiter.ts
46 lines (40 loc) · 1.46 KB
/
limiter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import Bottleneck from 'bottleneck';
export interface RateLimiterConfig {
size: number;
increaseInterval: number;
increaseAmount: number;
}
// default values are matching https://docs.blockfrost.io/#section/Limits
// burst 500 reqs/50s with 10req/1s cool-off
export const RATE_LIMITER_DEFAULT_CONFIG = {
size: 500,
increaseInterval: 1000,
increaseAmount: 10,
};
export const getLimiter = (config: RateLimiterConfig) => {
// see Bottleneck docs https://www.npmjs.com/package/bottleneck#constructor=
const limiter = new Bottleneck({
/*
How many jobs can be executed before the limiter stops executing jobs.
If reservoir reaches 0, no jobs will be executed until it is no longer 0.
New jobs will still be queued up.
*/
reservoir: config.size,
/*
The increment applied to reservoir when reservoirIncreaseInterval is in use.
*/
reservoirIncreaseAmount: config.increaseAmount,
/* Every reservoirRefreshInterval milliseconds, the reservoir value will be automatically updated to the value of reservoirRefreshAmount.
The reservoirRefreshInterval value should be a multiple of 250(5000 for Clustering).
*/
reservoirIncreaseInterval: config.increaseInterval,
/*
The maximum value that reservoir can reach when reservoirIncreaseInterval is in use.
*/
reservoirIncreaseMaximum: config.size,
});
limiter.on('error', function (error) {
console.error(error);
});
return limiter;
};