0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

refactor(core): store tenant promise in pool

when multiple concurrency requests for the same tenant arrives
it would create a lot of promises for the same tenant which
is unexpected. directly store the tenant creating promise to
avoid it.
This commit is contained in:
Gao Sun 2023-03-14 15:06:02 +08:00
parent 9db5729cfd
commit 6171082825
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
2 changed files with 7 additions and 10 deletions

View file

@ -52,10 +52,6 @@ export class EnvSet {
return this.#pool;
}
get poolSafe() {
return this.#pool;
}
get oidc() {
if (!this.#oidc) {
return throwNotLoadedError();

View file

@ -3,9 +3,10 @@ import LRUCache from 'lru-cache';
import Tenant from './Tenant.js';
export class TenantPool {
protected cache = new LRUCache<string, Tenant>({
protected cache = new LRUCache<string, Promise<Tenant>>({
max: 100,
dispose: (tenant) => {
dispose: async (entry) => {
const tenant = await entry;
void tenant.dispose();
},
});
@ -18,7 +19,7 @@ export class TenantPool {
}
console.log('Init tenant:', tenantId);
const newTenant = await Tenant.create(tenantId);
const newTenant = Tenant.create(tenantId);
this.cache.set(tenantId, newTenant);
return newTenant;
@ -26,10 +27,10 @@ export class TenantPool {
async endAll(): Promise<void> {
await Promise.all(
this.cache.dump().map(([, tenant]) => {
const { poolSafe } = tenant.value.envSet;
this.cache.dump().map(async ([, entry]) => {
const tenant = await entry.value;
return poolSafe?.end();
return tenant.envSet.end();
})
);
}