-
-
Notifications
You must be signed in to change notification settings - Fork 24k
feat: add retry logic for Redis rate limiter #5939
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -95,21 +95,51 @@ export class RateLimiterManager { | |
| const release = await this.rateLimiterMutex.acquire() | ||
| try { | ||
| if (process.env.MODE === MODE.QUEUE) { | ||
| this.rateLimiters.set( | ||
| id, | ||
| rateLimit({ | ||
| windowMs: duration * 1000, | ||
| max: limit, | ||
| standardHeaders: true, | ||
| legacyHeaders: false, | ||
| message, | ||
| store: new RedisStore({ | ||
| prefix: `rl:${id}`, | ||
| // @ts-expect-error - Known issue: the `call` function is not present in @types/ioredis | ||
| sendCommand: (...args: string[]) => this.redisClient.call(...args) | ||
| // Add retry logic for Redis connection issues | ||
| const maxRetries = 3 | ||
| let retryCount = 0 | ||
| let lastError: Error | null = null | ||
|
|
||
| while (retryCount < maxRetries) { | ||
| try { | ||
| this.rateLimiters.set( | ||
| id, | ||
| rateLimit({ | ||
| windowMs: duration * 1000, | ||
| max: limit, | ||
| standardHeaders: true, | ||
| legacyHeaders: false, | ||
| message, | ||
| store: new RedisStore({ | ||
| prefix: `rl:${id}`, | ||
| // @ts-expect-error - Known issue: the `call` function is not present in @types/ioredis | ||
| sendCommand: (...args: string[]) => this.redisClient.call(...args) | ||
| }) | ||
| }) | ||
| ) | ||
| break // Success, exit retry loop | ||
| } catch (error) { | ||
| lastError = error as Error | ||
| retryCount++ | ||
| if (retryCount < maxRetries) { | ||
| // Wait before retry (exponential backoff) | ||
| await new Promise(resolve => setTimeout(resolve, Math.pow(2, retryCount) * 100)) | ||
| } | ||
| } | ||
| } | ||
|
Comment on lines
+103
to
+129
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The retry logic implemented here is ineffective for detecting Redis connection issues. The |
||
|
|
||
| if (retryCount >= maxRetries && lastError) { | ||
| console.error(`Failed to add rate limiter after ${maxRetries} retries:`, lastError.message) | ||
| // Fall back to in-memory rate limiter if Redis fails | ||
| this.rateLimiters.set( | ||
| id, | ||
| rateLimit({ | ||
| windowMs: duration * 1000, | ||
| max: limit, | ||
| message: `${message} (fallback mode - Redis unavailable)` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }) | ||
| }) | ||
| ) | ||
| ) | ||
| } | ||
| } else { | ||
| this.rateLimiters.set( | ||
| id, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
messageparameter, originating fromchatFlow.apiConfig, is directly passed to therateLimitmiddleware. This is susceptible to Stored Cross-Site Scripting (XSS) if it contains unsanitized HTML or JavaScript, asexpress-rate-limitsends this string as the response body. This is particularly risky if chatflows are shared or embedded in public websites.