mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00
🌐 feat: Configurable Redis Cluster Mode with Single URI Support (#9039)
* ✨ feat: Add support for enabling Redis cluster configuration * ✨ feat: Enhance Redis client initialization to support cluster configuration without multiple URIs * ✨ feat: Add tests for USE_REDIS_CLUSTER configuration and validation * 🐞 fix: Remove unnecessary blank line in cacheConfig tests
This commit is contained in:
parent
ad1503abdc
commit
9b6395d955
3 changed files with 37 additions and 3 deletions
3
api/cache/cacheConfig.js
vendored
3
api/cache/cacheConfig.js
vendored
|
@ -52,7 +52,8 @@ const cacheConfig = {
|
||||||
REDIS_CONNECT_TIMEOUT: math(process.env.REDIS_CONNECT_TIMEOUT, 10000),
|
REDIS_CONNECT_TIMEOUT: math(process.env.REDIS_CONNECT_TIMEOUT, 10000),
|
||||||
/** Queue commands when disconnected */
|
/** Queue commands when disconnected */
|
||||||
REDIS_ENABLE_OFFLINE_QUEUE: isEnabled(process.env.REDIS_ENABLE_OFFLINE_QUEUE ?? 'true'),
|
REDIS_ENABLE_OFFLINE_QUEUE: isEnabled(process.env.REDIS_ENABLE_OFFLINE_QUEUE ?? 'true'),
|
||||||
|
/** Enable redis cluster without the need of multiple URIs */
|
||||||
|
USE_REDIS_CLUSTER: isEnabled(process.env.USE_REDIS_CLUSTER ?? 'false'),
|
||||||
CI: isEnabled(process.env.CI),
|
CI: isEnabled(process.env.CI),
|
||||||
DEBUG_MEMORY_CACHE: isEnabled(process.env.DEBUG_MEMORY_CACHE),
|
DEBUG_MEMORY_CACHE: isEnabled(process.env.DEBUG_MEMORY_CACHE),
|
||||||
|
|
||||||
|
|
33
api/cache/cacheConfig.spec.js
vendored
33
api/cache/cacheConfig.spec.js
vendored
|
@ -14,6 +14,7 @@ describe('cacheConfig', () => {
|
||||||
delete process.env.REDIS_KEY_PREFIX_VAR;
|
delete process.env.REDIS_KEY_PREFIX_VAR;
|
||||||
delete process.env.REDIS_KEY_PREFIX;
|
delete process.env.REDIS_KEY_PREFIX;
|
||||||
delete process.env.USE_REDIS;
|
delete process.env.USE_REDIS;
|
||||||
|
delete process.env.USE_REDIS_CLUSTER;
|
||||||
delete process.env.REDIS_PING_INTERVAL;
|
delete process.env.REDIS_PING_INTERVAL;
|
||||||
delete process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES;
|
delete process.env.FORCED_IN_MEMORY_CACHE_NAMESPACES;
|
||||||
|
|
||||||
|
@ -101,6 +102,38 @@ describe('cacheConfig', () => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('USE_REDIS_CLUSTER configuration', () => {
|
||||||
|
test('should default to false when USE_REDIS_CLUSTER is not set', () => {
|
||||||
|
const { cacheConfig } = require('./cacheConfig');
|
||||||
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be false when USE_REDIS_CLUSTER is set to false', () => {
|
||||||
|
process.env.USE_REDIS_CLUSTER = 'false';
|
||||||
|
|
||||||
|
const { cacheConfig } = require('./cacheConfig');
|
||||||
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be true when USE_REDIS_CLUSTER is set to true', () => {
|
||||||
|
process.env.USE_REDIS_CLUSTER = 'true';
|
||||||
|
|
||||||
|
const { cacheConfig } = require('./cacheConfig');
|
||||||
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(true);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should work with USE_REDIS enabled and REDIS_URI set', () => {
|
||||||
|
process.env.USE_REDIS_CLUSTER = 'true';
|
||||||
|
process.env.USE_REDIS = 'true';
|
||||||
|
process.env.REDIS_URI = 'redis://localhost:6379';
|
||||||
|
|
||||||
|
const { cacheConfig } = require('./cacheConfig');
|
||||||
|
expect(cacheConfig.USE_REDIS_CLUSTER).toBe(true);
|
||||||
|
expect(cacheConfig.USE_REDIS).toBe(true);
|
||||||
|
expect(cacheConfig.REDIS_URI).toBe('redis://localhost:6379');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('REDIS_CA file reading', () => {
|
describe('REDIS_CA file reading', () => {
|
||||||
test('should be null when REDIS_CA is not set', () => {
|
test('should be null when REDIS_CA is not set', () => {
|
||||||
const { cacheConfig } = require('./cacheConfig');
|
const { cacheConfig } = require('./cacheConfig');
|
||||||
|
|
4
api/cache/redisClients.js
vendored
4
api/cache/redisClients.js
vendored
|
@ -48,7 +48,7 @@ if (cacheConfig.USE_REDIS) {
|
||||||
};
|
};
|
||||||
|
|
||||||
ioredisClient =
|
ioredisClient =
|
||||||
urls.length === 1
|
urls.length === 1 && !cacheConfig.USE_REDIS_CLUSTER
|
||||||
? new IoRedis(cacheConfig.REDIS_URI, redisOptions)
|
? new IoRedis(cacheConfig.REDIS_URI, redisOptions)
|
||||||
: new IoRedis.Cluster(
|
: new IoRedis.Cluster(
|
||||||
urls.map((url) => ({ host: url.hostname, port: parseInt(url.port, 10) || 6379 })),
|
urls.map((url) => ({ host: url.hostname, port: parseInt(url.port, 10) || 6379 })),
|
||||||
|
@ -148,7 +148,7 @@ if (cacheConfig.USE_REDIS) {
|
||||||
};
|
};
|
||||||
|
|
||||||
keyvRedisClient =
|
keyvRedisClient =
|
||||||
urls.length === 1
|
urls.length === 1 && !cacheConfig.USE_REDIS_CLUSTER
|
||||||
? createClient({ url: cacheConfig.REDIS_URI, ...redisOptions })
|
? createClient({ url: cacheConfig.REDIS_URI, ...redisOptions })
|
||||||
: createCluster({
|
: createCluster({
|
||||||
rootNodes: urls.map((url) => ({ url: url.href })),
|
rootNodes: urls.map((url) => ({ url: url.href })),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue