@thunkier/thunkmetrc-wrapper 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/RateLimiter.d.ts +19 -0
- package/dist/RateLimiter.js +139 -0
- package/dist/index.d.ts +6760 -0
- package/dist/index.js +4782 -0
- package/package.json +20 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export declare class RateLimiterConfig {
|
|
2
|
+
enabled: boolean;
|
|
3
|
+
maxGetPerSecondPerFacility: number;
|
|
4
|
+
maxGetPerSecondIntegrator: number;
|
|
5
|
+
maxConcurrentGetPerFacility: number;
|
|
6
|
+
maxConcurrentGetIntegrator: number;
|
|
7
|
+
}
|
|
8
|
+
export declare class MetrcRateLimiter {
|
|
9
|
+
private config;
|
|
10
|
+
private integratorRate;
|
|
11
|
+
private integratorSem;
|
|
12
|
+
private facilityRates;
|
|
13
|
+
private facilitySems;
|
|
14
|
+
constructor(config?: RateLimiterConfig);
|
|
15
|
+
execute<T>(facility: string | null, isGet: boolean, op: () => Promise<T>): Promise<T>;
|
|
16
|
+
private executeRateLimited;
|
|
17
|
+
private getFacilityRate;
|
|
18
|
+
private getFacilitySem;
|
|
19
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MetrcRateLimiter = exports.RateLimiterConfig = void 0;
|
|
4
|
+
class RateLimiterConfig {
|
|
5
|
+
constructor() {
|
|
6
|
+
this.enabled = false;
|
|
7
|
+
this.maxGetPerSecondPerFacility = 50;
|
|
8
|
+
this.maxGetPerSecondIntegrator = 150;
|
|
9
|
+
this.maxConcurrentGetPerFacility = 10;
|
|
10
|
+
this.maxConcurrentGetIntegrator = 30;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
exports.RateLimiterConfig = RateLimiterConfig;
|
|
14
|
+
class Semaphore {
|
|
15
|
+
constructor(max) {
|
|
16
|
+
this.max = max;
|
|
17
|
+
this.tasks = [];
|
|
18
|
+
this.count = 0;
|
|
19
|
+
}
|
|
20
|
+
async acquire() {
|
|
21
|
+
if (this.count < this.max) {
|
|
22
|
+
this.count++;
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
return new Promise(resolve => this.tasks.push(resolve));
|
|
26
|
+
}
|
|
27
|
+
release() {
|
|
28
|
+
if (this.tasks.length > 0) {
|
|
29
|
+
const next = this.tasks.shift();
|
|
30
|
+
next();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
this.count--;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
class TokenBucket {
|
|
38
|
+
constructor(rate, capacity) {
|
|
39
|
+
this.rate = rate;
|
|
40
|
+
this.capacity = capacity;
|
|
41
|
+
this.tokens = capacity;
|
|
42
|
+
this.lastRefill = Date.now();
|
|
43
|
+
}
|
|
44
|
+
async wait() {
|
|
45
|
+
this.refill();
|
|
46
|
+
if (this.tokens >= 1) {
|
|
47
|
+
this.tokens -= 1;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const missing = 1 - this.tokens;
|
|
51
|
+
const waitMs = (missing / this.rate) * 1000;
|
|
52
|
+
await new Promise(resolve => setTimeout(resolve, waitMs));
|
|
53
|
+
return this.wait();
|
|
54
|
+
}
|
|
55
|
+
refill() {
|
|
56
|
+
const now = Date.now();
|
|
57
|
+
const elapsedSec = (now - this.lastRefill) / 1000;
|
|
58
|
+
this.tokens = Math.min(this.capacity, this.tokens + (elapsedSec * this.rate));
|
|
59
|
+
this.lastRefill = now;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
class MetrcRateLimiter {
|
|
63
|
+
constructor(config) {
|
|
64
|
+
this.facilityRates = new Map();
|
|
65
|
+
this.facilitySems = new Map();
|
|
66
|
+
this.config = config || new RateLimiterConfig();
|
|
67
|
+
this.integratorRate = new TokenBucket(this.config.maxGetPerSecondIntegrator, this.config.maxGetPerSecondIntegrator);
|
|
68
|
+
this.integratorSem = new Semaphore(this.config.maxConcurrentGetIntegrator);
|
|
69
|
+
}
|
|
70
|
+
async execute(facility, isGet, op) {
|
|
71
|
+
if (!this.config.enabled || !isGet) {
|
|
72
|
+
return op();
|
|
73
|
+
}
|
|
74
|
+
await this.integratorSem.acquire();
|
|
75
|
+
try {
|
|
76
|
+
if (facility) {
|
|
77
|
+
const sem = this.getFacilitySem(facility);
|
|
78
|
+
await sem.acquire();
|
|
79
|
+
try {
|
|
80
|
+
return await this.executeRateLimited(facility, op);
|
|
81
|
+
}
|
|
82
|
+
finally {
|
|
83
|
+
sem.release();
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
return await this.executeRateLimited(facility, op);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
finally {
|
|
91
|
+
this.integratorSem.release();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async executeRateLimited(facility, op) {
|
|
95
|
+
await this.integratorRate.wait();
|
|
96
|
+
if (facility) {
|
|
97
|
+
await this.getFacilityRate(facility).wait();
|
|
98
|
+
}
|
|
99
|
+
// Retry loop
|
|
100
|
+
while (true) {
|
|
101
|
+
try {
|
|
102
|
+
return await op();
|
|
103
|
+
}
|
|
104
|
+
catch (err) {
|
|
105
|
+
// Check for 429
|
|
106
|
+
if (err?.response?.status === 429 || (err?.message && err.message.includes('429'))) {
|
|
107
|
+
// Backoff
|
|
108
|
+
const retryAfterInfo = err?.response?.headers?.['retry-after'];
|
|
109
|
+
let waitMs = 1000;
|
|
110
|
+
if (retryAfterInfo) {
|
|
111
|
+
const seconds = parseInt(retryAfterInfo, 10);
|
|
112
|
+
if (!isNaN(seconds))
|
|
113
|
+
waitMs = seconds * 1000;
|
|
114
|
+
}
|
|
115
|
+
await new Promise(resolve => setTimeout(resolve, waitMs));
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
throw err;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
getFacilityRate(facility) {
|
|
123
|
+
let tb = this.facilityRates.get(facility);
|
|
124
|
+
if (!tb) {
|
|
125
|
+
tb = new TokenBucket(this.config.maxGetPerSecondPerFacility, this.config.maxGetPerSecondPerFacility);
|
|
126
|
+
this.facilityRates.set(facility, tb);
|
|
127
|
+
}
|
|
128
|
+
return tb;
|
|
129
|
+
}
|
|
130
|
+
getFacilitySem(facility) {
|
|
131
|
+
let sem = this.facilitySems.get(facility);
|
|
132
|
+
if (!sem) {
|
|
133
|
+
sem = new Semaphore(this.config.maxConcurrentGetPerFacility);
|
|
134
|
+
this.facilitySems.set(facility, sem);
|
|
135
|
+
}
|
|
136
|
+
return sem;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
exports.MetrcRateLimiter = MetrcRateLimiter;
|