0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor: support setting pool sizes by env variable

This commit is contained in:
Gao Sun 2023-03-13 23:51:55 +08:00
parent 14eb677a61
commit 9c48f98fa1
No known key found for this signature in database
GPG key ID: 13EBE123E4773688
4 changed files with 18 additions and 7 deletions

View file

@ -2,7 +2,7 @@ import { assert } from '@silverhand/essentials';
import { createMockPool, createMockQueryResult, createPool, parseDsn } from 'slonik';
import { createInterceptors } from 'slonik-interceptor-preset';
const createPoolByEnv = async (databaseDsn: string, isTest: boolean) => {
const createPoolByEnv = async (databaseDsn: string, isTest: boolean, poolSize?: number) => {
// Database connection is disabled in unit test environment
if (isTest) {
return createMockPool({ query: async () => createMockQueryResult([]) });
@ -12,7 +12,7 @@ const createPoolByEnv = async (databaseDsn: string, isTest: boolean) => {
assert(parseDsn(databaseDsn).databaseName, new Error('Database name is required'));
return createPool(databaseDsn, { interceptors });
return createPool(databaseDsn, { interceptors, maximumPoolSize: poolSize });
};
export default createPoolByEnv;

View file

@ -6,7 +6,7 @@ import type { DatabasePool } from 'slonik';
import { createLogtoConfigLibrary } from '#src/libraries/logto-config.js';
import { createLogtoConfigQueries } from '#src/queries/logto-config.js';
import createPool from './create-pool.js';
import createPoolByEnv from './create-pool.js';
import loadOidcValues from './oidc.js';
import { throwNotLoadedError } from './throw-errors.js';
import { getTenantEndpoint } from './utils.js';
@ -37,7 +37,7 @@ export class EnvSet {
return this.values.dbUrl;
}
static sharedPool = createPool(this.dbUrl, this.isTest);
static sharedPool = createPoolByEnv(this.dbUrl, this.isTest, this.values.databasePoolSize);
#pool: Optional<DatabasePool>;
#oidc: Optional<Awaited<ReturnType<typeof loadOidcValues>>>;
@ -61,7 +61,11 @@ export class EnvSet {
}
async load() {
const pool = await createPool(this.databaseUrl, EnvSet.isTest);
const pool = await createPoolByEnv(
this.databaseUrl,
EnvSet.isTest,
EnvSet.values.databasePoolSize
);
this.#pool = pool;

View file

@ -1,11 +1,13 @@
import LRUCache from 'lru-cache';
import { EnvSet } from '#src/env-set/index.js';
import Tenant from './Tenant.js';
export class TenantPool {
protected cache = new LRUCache<string, Promise<Tenant>>({
max: 100,
dispose: async (entry) => {
max: EnvSet.values.tenantPoolSize,
dispose: (tenant) => {
const tenant = await entry;
void tenant.dispose();
},

View file

@ -98,6 +98,11 @@ export default class GlobalValues {
public readonly trustProxyHeader = yes(getEnv('TRUST_PROXY_HEADER'));
public readonly ignoreConnectorVersionCheck = yes(getEnv('IGNORE_CONNECTOR_VERSION_CHECK'));
/** Maximum number of tenants to keep in the tenant pool. */
public readonly tenantPoolSize = Number(getEnv('TENANT_POOL_SIZE', '100'));
/** Maximum number of clients to keep in a single database pool (i.e. per `Tenant` class). */
public readonly databasePoolSize = Number(getEnv('DATABASE_POOL_SIZE', '20'));
public get dbUrl(): string {
return this.databaseUrl;
}