Respect Reddit rate limit delays
This commit is contained in:
1 parent
7bcdbe236a
commit
50de1c180d
5 files changed
+77
-10
No files matched your search
+24
-2
@@ -3,6 +3,26 @@ import type { BrowserContext } from 'playwright';
|
||||
import type { Config } from '../config/index.js';
|
||||
import type { Logger } from '../utils/logger.js';
|
||||
|
||||
export class RedditRateLimitError extends Error {
|
||||
constructor(public readonly retryAfterMs: number) {
|
||||
const retryAfterSeconds = Math.ceil(retryAfterMs / 1000);
|
||||
super(`Reddit API rate limit reached; retry after ${retryAfterSeconds}s`);
|
||||
this.name = 'RedditRateLimitError';
|
||||
}
|
||||
}
|
||||
|
||||
function getRateLimitDelayMs(headers: unknown): number {
|
||||
if (!headers || typeof headers !== 'object') return 60_000;
|
||||
|
||||
const values = headers as Record<string, unknown>;
|
||||
const rawDelay = values['retry-after'] ?? values['x-ratelimit-reset'];
|
||||
const seconds = typeof rawDelay === 'number' ? rawDelay : Number.parseFloat(String(rawDelay));
|
||||
|
||||
return Number.isFinite(seconds) && seconds >= 0
|
||||
? Math.ceil(seconds * 1000)
|
||||
: 60_000;
|
||||
}
|
||||
|
||||
export interface RedditPost {
|
||||
id: string;
|
||||
name: string; // fullname like "t3_xxxxx"
|
||||
@@ -134,8 +154,10 @@ export class RedditApiClient {
|
||||
return [];
|
||||
}
|
||||
if (error.response?.status === 429) {
|
||||
this.logger.warn(`Rate limited by Reddit, waiting...`);
|
||||
throw error;
|
||||
const retryAfterMs = getRateLimitDelayMs(error.response.headers);
|
||||
const rateLimitError = new RedditRateLimitError(retryAfterMs);
|
||||
this.logger.warn(rateLimitError.message);
|
||||
throw rateLimitError;
|
||||
}
|
||||
}
|
||||
this.logger.error(`Error fetching posts from r/${subreddit}:`, error);
|
||||
|
||||
Reference in new issue
Block a user