forked from danny-avila/LibreChat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): initial Redis support; fix(SearchBar): proper debounce (da…
…nny-avila#1039) * refactor: use keyv for search caching with 1 min expirations * feat: keyvRedis; chore: bump keyv, bun.lockb, add jsconfig for vscode file resolution * feat: api/search redis support * refactor(redis) use ioredis cluster for keyv fix(OpenID): when redis is configured, use redis memory store for express-session * fix: revert using uri for keyvredis * fix(SearchBar): properly debounce search queries, fix weird render behaviors * refactor: add authentication to search endpoint and show error messages in results * feat: redis support for violation logs * fix(logViolation): ensure a number is always being stored in cache * feat(concurrentLimiter): uses clearPendingReq, clears pendingReq on abort, redis support * fix(api/search/enable): query only when authenticated * feat(ModelService): redis support * feat(checkBan): redis support * refactor(api/search): consolidate keyv logic * fix(ci): add default empty value for REDIS_URI * refactor(keyvRedis): use condition to initialize keyvRedis assignment * refactor(connectDb): handle disconnected state (should create a new conn) * fix(ci/e2e): handle case where cleanUp did not successfully run * fix(getDefaultEndpoint): return endpoint from localStorage if defined and endpointsConfig is default * ci(e2e): remove afterAll messages as startup/cleanUp will clear messages * ci(e2e): remove teardown for CI until further notice * chore: bump playwright/test * ci(e2e): reinstate teardown as CI issue is specific to github env * fix(ci): click settings menu trigger by testid
- Loading branch information
1 parent
a9b33f7
commit 2b79eaf
Showing
29 changed files
with
461 additions
and
171 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,29 +1,48 @@ | ||
const Keyv = require('keyv'); | ||
const { pendingReqFile } = require('./keyvFiles'); | ||
const { LIMIT_CONCURRENT_MESSAGES } = process.env ?? {}; | ||
|
||
const keyv = new Keyv({ store: pendingReqFile, namespace: 'pendingRequests' }); | ||
const getLogStores = require('./getLogStores'); | ||
const { isEnabled } = require('../server/utils'); | ||
const { USE_REDIS, LIMIT_CONCURRENT_MESSAGES } = process.env ?? {}; | ||
const ttl = 1000 * 60 * 1; | ||
|
||
/** | ||
* Clear pending requests from the cache. | ||
* Clear or decrement pending requests from the cache. | ||
* Checks the environmental variable LIMIT_CONCURRENT_MESSAGES; | ||
* if the rule is enabled ('true'), pending requests in the cache are cleared. | ||
* if the rule is enabled ('true'), it either decrements the count of pending requests | ||
* or deletes the key if the count is less than or equal to 1. | ||
* | ||
* @module clearPendingReq | ||
* @requires keyv | ||
* @requires keyvFiles | ||
* @requires ./getLogStores | ||
* @requires ../server/utils | ||
* @requires process | ||
* | ||
* @async | ||
* @function | ||
* @returns {Promise<void>} A promise that either clears 'pendingRequests' from store or resolves with no value. | ||
* @param {Object} params - The parameters object. | ||
* @param {string} params.userId - The user ID for which the pending requests are to be cleared or decremented. | ||
* @param {Object} [params.cache] - An optional cache object to use. If not provided, a default cache will be fetched using getLogStores. | ||
* @returns {Promise<void>} A promise that either decrements the 'pendingRequests' count, deletes the key from the store, or resolves with no value. | ||
*/ | ||
const clearPendingReq = async () => { | ||
if (LIMIT_CONCURRENT_MESSAGES?.toLowerCase() !== 'true') { | ||
const clearPendingReq = async ({ userId, cache: _cache }) => { | ||
if (!userId) { | ||
return; | ||
} else if (!isEnabled(LIMIT_CONCURRENT_MESSAGES)) { | ||
return; | ||
} | ||
|
||
const namespace = 'pending_req'; | ||
const cache = _cache ?? getLogStores(namespace); | ||
|
||
if (!cache) { | ||
return; | ||
} | ||
|
||
await keyv.clear(); | ||
const key = `${USE_REDIS ? namespace : ''}:${userId ?? ''}`; | ||
const currentReq = +((await cache.get(key)) ?? 0); | ||
|
||
if (currentReq && currentReq >= 1) { | ||
await cache.set(key, currentReq - 1, ttl); | ||
} else { | ||
await cache.delete(key); | ||
} | ||
}; | ||
|
||
module.exports = clearPendingReq; |
This file contains 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 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
const keyvFiles = require('./keyvFiles'); | ||
const getLogStores = require('./getLogStores'); | ||
const logViolation = require('./logViolation'); | ||
const clearPendingReq = require('./clearPendingReq'); | ||
|
||
module.exports = { ...keyvFiles, getLogStores, logViolation, clearPendingReq }; | ||
module.exports = { ...keyvFiles, getLogStores, logViolation }; |
This file contains 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,14 @@ | ||
const KeyvRedis = require('@keyv/redis'); | ||
|
||
const { REDIS_URI } = process.env; | ||
|
||
let keyvRedis; | ||
|
||
if (REDIS_URI) { | ||
keyvRedis = new KeyvRedis(REDIS_URI, { useRedisSets: false }); | ||
keyvRedis.on('error', (err) => console.error('KeyvRedis connection error:', err)); | ||
} else { | ||
// console.log('REDIS_URI not provided. Redis module will not be initialized.'); | ||
} | ||
|
||
module.exports = keyvRedis; |
This file contains 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 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,4 @@ | ||
const Redis = require('ioredis'); | ||
const { REDIS_URI } = process.env ?? {}; | ||
const redis = new Redis.Cluster(REDIS_URI); | ||
module.exports = redis; |
This file contains 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,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ES6", | ||
"module": "CommonJS", | ||
// "checkJs": true, // Report errors in JavaScript files | ||
"baseUrl": "./", | ||
"paths": { | ||
"*": ["*", "node_modules/*"], | ||
"~/*": ["./*"] | ||
} | ||
}, | ||
"exclude": ["node_modules"] | ||
} |
This file contains 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 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 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 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
Oops, something went wrong.