Skip to content

feat: add retry logic for Redis rate limiter#5939

Open
hiSandog wants to merge 1 commit intoFlowiseAI:mainfrom
hiSandog:feat/add-rate-limiting-hints
Open

feat: add retry logic for Redis rate limiter#5939
hiSandog wants to merge 1 commit intoFlowiseAI:mainfrom
hiSandog:feat/add-rate-limiting-hints

Conversation

@hiSandog
Copy link

Add exponential backoff retry mechanism when adding rate limiters with Redis store. Falls back to in-memory rate limiter if Redis is unavailable after max retries.

Add exponential backoff retry mechanism when adding rate limiters
with Redis store. Falls back to in-memory rate limiter if Redis is
unavailable after max retries.
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the resilience of the application's rate limiting system. By introducing retry logic with exponential backoff for Redis connections and providing a graceful fallback to an in-memory store, it ensures that rate limiting functionality remains operational even when facing temporary Redis outages or connection problems, thereby improving overall system stability.

Highlights

  • Redis Rate Limiter Robustness: Implemented an exponential backoff retry mechanism for creating Redis-backed rate limiters to handle transient connection issues.
  • Fallback to In-Memory Rate Limiter: Added a fallback to an in-memory rate limiter if Redis remains unavailable after the maximum number of retries, ensuring continued service availability.
Changelog
  • packages/server/src/utils/rateLimit.ts
    • Added retry logic with exponential backoff for Redis rate limiter initialization.
    • Implemented a fallback mechanism to an in-memory rate limiter if Redis connection attempts fail after multiple retries.
    • Included error logging for failed Redis rate limiter initialization attempts.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a retry mechanism for the Redis rate limiter with a fallback to an in-memory store. However, the retry logic is flawed, as it ineffectively handles Redis connection issues during initialization. Additionally, a critical security vulnerability exists: the rate limiter messages, both primary and fallback, are susceptible to Stored Cross-Site Scripting (XSS) due to direct use of unsanitized user-controlled configuration.

max: limit,
standardHeaders: true,
legacyHeaders: false,
message,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The message parameter, originating from chatFlow.apiConfig, is directly passed to the rateLimit middleware. This is susceptible to Stored Cross-Site Scripting (XSS) if it contains unsanitized HTML or JavaScript, as express-rate-limit sends this string as the response body. This is particularly risky if chatflows are shared or embedded in public websites.

rateLimit({
windowMs: duration * 1000,
max: limit,
message: `${message} (fallback mode - Redis unavailable)`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-high high

The same Stored XSS vulnerability exists in the fallback rate limiter message. The untrusted message is concatenated with a suffix and used as the response body for the in-memory rate limiter.

Comment on lines +103 to +129
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))
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-medium medium

The retry logic implemented here is ineffective for detecting Redis connection issues. The rateLimit() function and the RedisStore constructor are synchronous and do not perform network operations during initialization. Connection errors will only occur when the middleware is actually executed during an incoming request. Consequently, the try-catch block will not catch connection failures at this stage, and the fallback to the in-memory rate limiter will never be triggered during initialization, even if Redis is unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant