0
Fork 0
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:
Gao Sun 2023-03-13 11:20:07 +08:00 committed by GitHub
commit f9c00f6dc5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 8 deletions

View file

@ -97,6 +97,10 @@ export class EnvSet {
const endpoint = getTenantEndpoint(this.tenantId, EnvSet.values); const endpoint = getTenantEndpoint(this.tenantId, EnvSet.values);
this.#oidc = await loadOidcValues(appendPath(endpoint, '/oidc').href, oidcConfigs); 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'; export { getTenantEndpoint } from './utils.js';

View file

@ -3,7 +3,12 @@ import LRUCache from 'lru-cache';
import Tenant from './Tenant.js'; import Tenant from './Tenant.js';
export class TenantPool { 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> { async get(tenantId: string): Promise<Tenant> {
const tenant = this.cache.get(tenantId); const tenant = this.cache.get(tenantId);
@ -20,13 +25,7 @@ export class TenantPool {
} }
async endAll(): Promise<void> { async endAll(): Promise<void> {
await Promise.all( await Promise.all(this.cache.dump().map(async ([, tenant]) => tenant.value.envSet.end()));
this.cache.dump().flatMap(([, tenant]) => {
const { poolSafe, queryClientSafe } = tenant.value.envSet;
return [poolSafe?.end(), queryClientSafe?.end()];
})
);
} }
} }