This commit is contained in:
jjbrunton committed 2026-01-05 00:17:06 +00:00
1 parent b780bff850
commit 7b3ea37127
1752 files changed
+1341 -166

No files matched your search

+11 -4
View File
@@ -15,7 +15,7 @@ export class RateLimiter {
this.queue = new PQueue({ concurrency: 1 });
}
async throttle<T>(fn: () => Promise<T>): Promise<T> {
async throttle<T>(fn: () => Promise<T>, shouldCount: (result: T) => boolean = () => true): Promise<T> {
return this.queue.add(async () => {
const now = Date.now();
@@ -36,6 +36,7 @@ export class RateLimiter {
}
// Enforce minimum delay with jitter
// We check against the previously completed message time
const timeSinceLastMessage = now - this.lastMessageTime;
const minDelay = this.config.rateLimit.minDelayBetweenMessagesMs;
const jitter = Math.random() * this.config.rateLimit.jitterMs;
@@ -46,10 +47,16 @@ export class RateLimiter {
await this.sleep(waitTime);
}
this.lastMessageTime = Date.now();
this.messagesThisHour++;
// Execute function
const result = await fn();
return fn();
// Update stats only if shouldCount returns true
if (shouldCount(result)) {
this.lastMessageTime = Date.now();
this.messagesThisHour++;
}
return result;
}) as Promise<T>;
}