mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
Merge pull request #3373 from logto-io/gao-end-connection-when-dispose
refactor(core): update cache size and end pool when tenant disposes
This commit is contained in:
commit
f9c00f6dc5
2 changed files with 11 additions and 8 deletions
|
@ -97,6 +97,10 @@ export class EnvSet {
|
|||
const endpoint = getTenantEndpoint(this.tenantId, EnvSet.values);
|
||||
this.#oidc = await loadOidcValues(appendPath(endpoint, '/oidc').href, oidcConfigs);
|
||||
}
|
||||
|
||||
async end() {
|
||||
await Promise.all([this.#pool?.end(), this.#queryClient?.end()]);
|
||||
}
|
||||
}
|
||||
|
||||
export { getTenantEndpoint } from './utils.js';
|
||||
|
|
|
@ -3,7 +3,12 @@ import LRUCache from 'lru-cache';
|
|||
import Tenant from './Tenant.js';
|
||||
|
||||
export class TenantPool {
|
||||
protected cache = new LRUCache<string, Tenant>({ max: 500 });
|
||||
protected cache = new LRUCache<string, Tenant>({
|
||||
max: 100,
|
||||
dispose: async (tenant) => {
|
||||
await tenant.envSet.end();
|
||||
},
|
||||
});
|
||||
|
||||
async get(tenantId: string): Promise<Tenant> {
|
||||
const tenant = this.cache.get(tenantId);
|
||||
|
@ -20,13 +25,7 @@ export class TenantPool {
|
|||
}
|
||||
|
||||
async endAll(): Promise<void> {
|
||||
await Promise.all(
|
||||
this.cache.dump().flatMap(([, tenant]) => {
|
||||
const { poolSafe, queryClientSafe } = tenant.value.envSet;
|
||||
|
||||
return [poolSafe?.end(), queryClientSafe?.end()];
|
||||
})
|
||||
);
|
||||
await Promise.all(this.cache.dump().map(async ([, tenant]) => tenant.value.envSet.end()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue