-
-
Notifications
You must be signed in to change notification settings - Fork 23.2k
fix: [FLOWISE-1] Unstable Redis, ECONNREFUSED 127.0.0.1 #5493
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
Open
natan-hoppe-workday
wants to merge
8
commits into
FlowiseAI:main
Choose a base branch
from
natan-hoppe-workday:bugfix/FLOWISE-1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−106
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
786aa55
Add RedisConnector class for unified initialization
NatanHoppe-Evisort ed5ade7
RedisConnector fixed
NatanHoppe-Evisort c79a377
Use `RedisConnector` instead of `Redis`
NatanHoppe-Evisort d1eae8a
Linted code
NatanHoppe-Evisort c9f97a3
Move RedisConnector to `components/src`
NatanHoppe-Evisort 997590e
Convert RedisConnector to support async
NatanHoppe-Evisort 598ae22
Add documentation
NatanHoppe-Evisort c356d5f
Move `RedisConnector` from `components/` to `server/`
NatanHoppe-Evisort File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import { InternalFlowiseError } from './errors/internalFlowiseError' | ||
| import logger from './utils/logger' | ||
| import { MODE } from './Interface' | ||
| import { Redis } from 'ioredis' | ||
| import { StatusCodes } from 'http-status-codes' | ||
|
|
||
| /** | ||
| * Class used to initialize and connect to Redis instance. | ||
| * | ||
| * Sync usage: | ||
| * const connector = new RedisConnector() | ||
| * const redis = connector.getRedisClient() | ||
| * | ||
| * Async usage: | ||
| * const connector = new RedisConnector() | ||
| * await connector.ready() // fully waits for Redis init | ||
| * const redis = connector.getRedisClient() | ||
| */ | ||
| export class RedisConnector { | ||
| /** | ||
| * @type {Redis} | ||
| */ | ||
| private redis!: Redis | ||
|
|
||
| /** | ||
| * @type {Record<string, unknown>} | ||
| */ | ||
| private connection!: Record<string, unknown> | ||
|
|
||
| /** | ||
| * @type {Promise<void>} | ||
| */ | ||
| private initPromise: Promise<void> | null = null | ||
|
|
||
| /** | ||
| * Sync constructor | ||
| * | ||
| * @constructor | ||
| */ | ||
| constructor() {} | ||
|
|
||
| /** | ||
| * Initializes Redis lazily (runs once). | ||
| * | ||
| * @returns {Promise<void>} | ||
| */ | ||
| private async init(): Promise<void> { | ||
| if (this.initPromise) return this.initPromise | ||
|
|
||
| this.initPromise = (async () => { | ||
| const keepAlive = | ||
| process.env.REDIS_KEEP_ALIVE && !isNaN(parseInt(process.env.REDIS_KEEP_ALIVE, 10)) | ||
| ? parseInt(process.env.REDIS_KEEP_ALIVE, 10) | ||
| : 0 | ||
|
|
||
| const tlsOptions = | ||
| process.env.REDIS_TLS === 'true' | ||
| ? { | ||
| cert: process.env.REDIS_CERT ? Buffer.from(process.env.REDIS_CERT, 'base64') : undefined, | ||
| key: process.env.REDIS_KEY ? Buffer.from(process.env.REDIS_KEY, 'base64') : undefined, | ||
| ca: process.env.REDIS_CA ? Buffer.from(process.env.REDIS_CA, 'base64') : undefined | ||
| } | ||
| : {} | ||
|
|
||
| switch (process.env.MODE) { | ||
| case MODE.QUEUE: | ||
| await this.initializeQueueMode(keepAlive, tlsOptions) | ||
| break | ||
|
|
||
| case MODE.MAIN: | ||
| throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, | ||
| `[server]: MODE ${process.env.MODE} not implemented` | ||
| ) | ||
|
|
||
| default: | ||
| throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, | ||
| `Unrecognized MODE - ${process.env.MODE}` | ||
| ) | ||
| } | ||
| })() | ||
|
|
||
| return this.initPromise | ||
| } | ||
|
|
||
| /** | ||
| * Queue mode initialization. | ||
| * | ||
| * @param {number} keepAlive - Keep alive in milliseconds (see https://redis.github.io/ioredis/index.html#RedisOptions) | ||
| * @param {Record<string, unknown>} tlsOptions - Record with key-value pairs (see https://redis.github.io/ioredis/index.html#RedisOptions) | ||
| */ | ||
| private async initializeQueueMode(keepAlive: number, tlsOptions: Record<string, unknown>): Promise<void> { | ||
| if (process.env.REDIS_URL) { | ||
| logger.info('[server] Queue mode using REDIS_URL.') | ||
|
|
||
| tlsOptions.rejectUnauthorized = | ||
| !(process.env.REDIS_URL.startsWith('rediss://') && process.env.REDIS_TLS !== 'true') | ||
|
|
||
| this.connection = { | ||
| keepAlive, | ||
| tls: tlsOptions, | ||
| enableReadyCheck: true, | ||
| reconnectOnError: this.connectOnError.bind(this) | ||
| } | ||
|
|
||
| this.redis = new Redis(process.env.REDIS_URL, this.connection) | ||
|
|
||
| } else { | ||
| logger.info('[server] Queue mode using HOST or localhost.') | ||
|
|
||
| this.connection = { | ||
| host: process.env.REDIS_HOST ?? 'localhost', | ||
| port: parseInt(process.env.REDIS_PORT || '6379'), | ||
| username: process.env.REDIS_USERNAME || undefined, | ||
| password: process.env.REDIS_PASSWORD || undefined, | ||
| keepAlive, | ||
| tls: tlsOptions, | ||
| enableReadyCheck: true, | ||
| reconnectOnError: this.connectOnError.bind(this) | ||
| } | ||
|
|
||
| this.redis = new Redis(this.connection) | ||
| } | ||
|
|
||
| try { | ||
| await this.redis.connect() | ||
| } catch (err: any) { | ||
| logger.error(`[server]: Redis connection failed - ${err.message}`) | ||
| throw new InternalFlowiseError(StatusCodes.INTERNAL_SERVER_ERROR, err.message) | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Function to handle Redis failure, used as callback. | ||
| * https://redis.github.io/ioredis/interfaces/CommonRedisOptions.html#reconnectOnError | ||
| * @param {Error} err | ||
| * @returns {number} 1 - Always reconnect to Redis in case of errors (does not retry the failed command) | ||
| * @see https://redis.github.io/ioredis/interfaces/CommonRedisOptions.html#reconnectOnError | ||
| */ | ||
| private connectOnError(err: Error): number { | ||
| logger.error(`[server]: Redis connection error - ${err.message}`) | ||
| return 1 | ||
| } | ||
|
|
||
| /** | ||
| * Sync-safe access: | ||
| * - If Redis isn't initialized: triggers async initialization. | ||
| * - Always returns the Redis instance synchronously. | ||
| * | ||
| * @returns {Redis} | ||
| */ | ||
| public getRedisClient(): Redis { | ||
| // Trigger async init if not yet started | ||
| void this.init() | ||
| return this.redis | ||
| } | ||
natan-hoppe-workday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Fully async safe usage: | ||
| * await connector.ready() | ||
| * | ||
| * @returns {Promise<void>} | ||
| */ | ||
| public async ready(): Promise<void> { | ||
| await this.init() | ||
| } | ||
|
|
||
| /** | ||
| * Sync-safe access | ||
| * | ||
| * @returns {Record<string, unknown>} | ||
| */ | ||
| public getRedisConnection(): Record<string, unknown> { | ||
| // Trigger async init if not yet started | ||
| void this.init() | ||
| return this.connection | ||
| } | ||
natan-hoppe-workday marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| export default RedisConnector | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.