-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTokenBucket.ts
More file actions
95 lines (76 loc) · 2.39 KB
/
TokenBucket.ts
File metadata and controls
95 lines (76 loc) · 2.39 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { wait } from './wait';
export type Interval = number | 'second' | 'minute' | 'hour' | 'day';
export class TokenBucket {
bucketSize: number;
tokensPerInterval: number;
interval: number;
tokens: number;
lastRefillTimestamp: number;
maxWait: Interval;
constructor({ bucketSize, tokensPerInterval, interval, maxWait }) {
this.bucketSize = bucketSize;
this.tokens = bucketSize;
this.tokensPerInterval = tokensPerInterval;
if (typeof interval === 'string') {
switch (interval) {
case 'second':
this.interval = 1000;
break;
case 'minute':
this.interval = 1000 * 60;
this.maxWait = 1000 * 60;
break;
case 'hour':
this.interval = 1000 * 60 * 60;
this.maxWait = 1000 * 60 * 60;
break;
case 'day':
this.interval = 1000 * 60 * 60 * 24;
this.maxWait = 1000 * 60 * 60 * 24;
break;
default:
throw new Error(`Invalid interval: ${interval}, Invalid maxWait: ${maxWait}`);
}
} else {
this.interval = interval;
this.maxWait = maxWait;
}
this.lastRefillTimestamp = Date.now();
}
get remainingTokens(): number {
this.refillTokens();
return this.tokens;
}
tryRemoveTokens(count: number): boolean {
if (count > this.bucketSize) return false;
this.refillTokens();
if (count > this.tokens) return false;
return true;
}
removeTokens(count: number): Promise<number> | number {
if (count > this.bucketSize) {
throw new Error('ExceedsBucketSize');
}
this.refillTokens();
const retryLater = async () => {
const waitMs = Math.ceil((count - this.tokens) * (this.interval / this.tokensPerInterval));
if (waitMs > this.maxWait) {
throw new Error('ExceedsMaxWait');
}
await wait(waitMs);
return this.removeTokens(count);
};
if (count > this.tokens) return retryLater();
this.tokens -= count;
return this.tokens;
}
refillTokens(): boolean {
const now = Date.now();
const duration = Math.max(now - this.lastRefillTimestamp, 0);
this.lastRefillTimestamp = now;
const refillAmount = Math.floor(duration * (this.tokensPerInterval / this.interval));
const prevTokens = this.tokens;
this.tokens = Math.min(this.tokens + refillAmount, this.bucketSize);
return this.tokens > prevTokens;
}
}