mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-04-07 00:15:23 +02:00
⚡ perf: Short-Circuit Config Override Resolution for Empty Principals (#12549)
Skip the getApplicableConfigs DB query when buildPrincipals returns an empty array, since there are no principals to match against.
This commit is contained in:
parent
8ed0bcf5ca
commit
a100aa5738
2 changed files with 32 additions and 1 deletions
|
|
@ -201,14 +201,16 @@ describe('createAppConfigService', () => {
|
|||
const deps = createDeps({ getApplicableConfigs: mockGetConfigs });
|
||||
const { getAppConfig } = createAppConfigService(deps);
|
||||
|
||||
// No role/userId → empty principals → short-circuits without calling getApplicableConfigs
|
||||
await getAppConfig();
|
||||
expect(mockGetConfigs).not.toHaveBeenCalled();
|
||||
|
||||
mockGetConfigs.mockResolvedValueOnce([
|
||||
{ priority: 10, overrides: { restricted: true }, isActive: true },
|
||||
]);
|
||||
const config = await getAppConfig({ role: 'ADMIN' });
|
||||
|
||||
expect(mockGetConfigs).toHaveBeenCalledTimes(2);
|
||||
expect(mockGetConfigs).toHaveBeenCalledTimes(1);
|
||||
expect((config as TestConfig).restricted).toBe(true);
|
||||
});
|
||||
|
||||
|
|
@ -229,6 +231,29 @@ describe('createAppConfigService', () => {
|
|||
expect((config as TestConfig).x).toBe('admin-only');
|
||||
});
|
||||
|
||||
it('skips getApplicableConfigs when buildPrincipals returns empty', async () => {
|
||||
const deps = createDeps({
|
||||
getUserPrincipals: jest.fn().mockResolvedValue([]),
|
||||
});
|
||||
const { getAppConfig } = createAppConfigService(deps);
|
||||
|
||||
const config = await getAppConfig({ userId: 'uid1', role: 'USER' });
|
||||
|
||||
expect(deps.getUserPrincipals).toHaveBeenCalledWith({ userId: 'uid1', role: 'USER' });
|
||||
expect(deps.getApplicableConfigs).not.toHaveBeenCalled();
|
||||
expect(config).toEqual(deps._baseConfig);
|
||||
});
|
||||
|
||||
it('skips getApplicableConfigs when no role or userId is provided', async () => {
|
||||
const deps = createDeps();
|
||||
const { getAppConfig } = createAppConfigService(deps);
|
||||
|
||||
const config = await getAppConfig();
|
||||
|
||||
expect(deps.getApplicableConfigs).not.toHaveBeenCalled();
|
||||
expect(config).toEqual(deps._baseConfig);
|
||||
});
|
||||
|
||||
it('falls back to base config on getApplicableConfigs error', async () => {
|
||||
const deps = createDeps({
|
||||
getApplicableConfigs: jest.fn().mockRejectedValue(new Error('DB down')),
|
||||
|
|
|
|||
|
|
@ -170,6 +170,12 @@ export function createAppConfigService(deps: AppConfigServiceDeps) {
|
|||
|
||||
try {
|
||||
const principals = await buildPrincipals(role, userId);
|
||||
|
||||
if (principals.length === 0) {
|
||||
await cache.set(cacheKey, baseConfig, overrideCacheTtl);
|
||||
return baseConfig;
|
||||
}
|
||||
|
||||
const configs = await getApplicableConfigs(principals);
|
||||
|
||||
if (configs.length === 0) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue