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:
parent
9db5729cfd
commit
6171082825
2 changed files with 7 additions and 10 deletions
|
@ -52,10 +52,6 @@ export class EnvSet {
|
|||
return this.#pool;
|
||||
}
|
||||
|
||||
get poolSafe() {
|
||||
return this.#pool;
|
||||
}
|
||||
|
||||
get oidc() {
|
||||
if (!this.#oidc) {
|
||||
return throwNotLoadedError();
|
||||
|
|
|
@ -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();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue