limit database creation concurrency to 1

Signed-off-by: Fredrik Adelöw <freben@gmail.com>
This commit is contained in:
Fredrik Adelöw
2023-11-07 15:48:09 +01:00
parent 3358a433d4
commit aa13482090
4 changed files with 18 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-common': patch
---
Limit the database creation concurrency to one, defensively
+1
View File
@@ -96,6 +96,7 @@
"minimatch": "^5.0.0",
"mysql2": "^2.2.5",
"node-fetch": "^2.6.7",
"p-limit": "^3.1.0",
"pg": "^8.11.3",
"raw-body": "^2.4.1",
"tar": "^6.1.12",
@@ -18,6 +18,7 @@ import { Config } from '@backstage/config';
import { JsonObject } from '@backstage/types';
import { InputError } from '@backstage/errors';
import knexFactory, { Knex } from 'knex';
import limiterFactory from 'p-limit';
import { mergeDatabaseConfig } from './config';
import { DatabaseConnector } from './types';
@@ -35,6 +36,12 @@ type DatabaseClient =
| 'mysql2'
| string;
// This limits the number of concurrent CREATE DATABASE and CREATE SCHEMA
// commands, globally, to just one. This is overly defensive, and was added as
// an attempt to counteract the pool issues on recent node versions. See
// https://github.com/backstage/backstage/pull/19988
const ddlLimiter = limiterFactory(1);
/**
* Mapping of client type to supported database connectors
*
@@ -83,9 +90,8 @@ export async function ensureDatabaseExists(
): Promise<void> {
const client: DatabaseClient = dbConfig.getString('client');
return ConnectorMapping[client]?.ensureDatabaseExists?.(
dbConfig,
...databases,
return await ddlLimiter(() =>
ConnectorMapping[client]?.ensureDatabaseExists?.(dbConfig, ...databases),
);
}
@@ -100,9 +106,8 @@ export async function ensureSchemaExists(
): Promise<void> {
const client: DatabaseClient = dbConfig.getString('client');
return await ConnectorMapping[client]?.ensureSchemaExists?.(
dbConfig,
...schemas,
return await ddlLimiter(() =>
ConnectorMapping[client]?.ensureSchemaExists?.(dbConfig, ...schemas),
);
}
+1
View File
@@ -3560,6 +3560,7 @@ __metadata:
msw: ^1.0.0
mysql2: ^2.2.5
node-fetch: ^2.6.7
p-limit: ^3.1.0
pg: ^8.11.3
raw-body: ^2.4.1
supertest: ^6.1.3