diff --git a/packages/core/src/env-set/index.ts b/packages/core/src/env-set/index.ts index 2a02e043b..79d09f34c 100644 --- a/packages/core/src/env-set/index.ts +++ b/packages/core/src/env-set/index.ts @@ -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'; diff --git a/packages/core/src/tenants/index.ts b/packages/core/src/tenants/index.ts index c27691ec7..8263b72f6 100644 --- a/packages/core/src/tenants/index.ts +++ b/packages/core/src/tenants/index.ts @@ -3,7 +3,12 @@ import LRUCache from 'lru-cache'; import Tenant from './Tenant.js'; export class TenantPool { - protected cache = new LRUCache({ max: 500 }); + protected cache = new LRUCache({ + max: 100, + dispose: async (tenant) => { + await tenant.envSet.end(); + }, + }); async get(tenantId: string): Promise { const tenant = this.cache.get(tenantId); @@ -20,13 +25,7 @@ export class TenantPool { } async endAll(): Promise { - 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())); } }