feat: keepalive on mysql/postgre connections, should fix #577

This commit is contained in:
Fabio Di Stasio 2023-05-14 18:48:21 +02:00
parent 5e83b4466d
commit 17eeb6d38e
5 changed files with 34 additions and 4 deletions

View File

@ -9,6 +9,8 @@ export class MySQLClient extends AntaresCore {
private _schema?: string;
private _runningConnections: Map<string, number>;
private _connectionsToCommit: Map<string, mysql.Connection | mysql.PoolConnection>;
private _keepaliveTimer: NodeJS.Timer;
private _keepaliveMs: number;
_connection?: mysql.Connection | mysql.Pool;
_params: mysql.ConnectionOptions & {schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean};
@ -52,6 +54,7 @@ export class MySQLClient extends AntaresCore {
this._schema = null;
this._runningConnections = new Map();
this._connectionsToCommit = new Map();
this._keepaliveMs = 10*60*1000;
}
private _getType (field: mysql.FieldPacket & { columnType?: number; columnLength?: number }) {
@ -182,6 +185,8 @@ export class MySQLClient extends AntaresCore {
destroy () {
this._connection.end();
clearInterval(this._keepaliveTimer);
this._keepaliveTimer = undefined;
if (this._ssh) this._ssh.close();
}
@ -243,9 +248,19 @@ export class MySQLClient extends AntaresCore {
conn.query(`SET SESSION sql_mode = '${sqlMode.filter((m: string) => !['ANSI', 'ANSI_QUOTES'].includes(m)).join(',')}'`);
});
this._keepaliveTimer = setInterval(async () => {
await this.keepAlive();
}, this._keepaliveMs);
return connection;
}
private async keepAlive () {
const connection = await (this._connection as mysql.Pool).getConnection();
await connection.ping();
connection.release();
}
use (schema: string) {
this._schema = schema;
return this.raw(`USE \`${schema}\``);

View File

@ -84,6 +84,8 @@ export class PostgreSQLClient extends AntaresCore {
private _schema?: string;
private _runningConnections: Map<string, number>;
private _connectionsToCommit: Map<string, pg.Client | pg.PoolClient>;
private _keepaliveTimer: NodeJS.Timer;
private _keepaliveMs: number;
protected _connection?: pg.Client | pg.Pool;
private types: {[key: string]: string} = {};
private _arrayTypes: {[key: string]: string} = {
@ -104,6 +106,7 @@ export class PostgreSQLClient extends AntaresCore {
this._schema = null;
this._runningConnections = new Map();
this._connectionsToCommit = new Map();
this._keepaliveMs = 10*60*1000;
for (const key in pg.types.builtins) {
const builtinKey = key as builtinsTypes;
@ -222,14 +225,26 @@ export class PostgreSQLClient extends AntaresCore {
});
}
this._keepaliveTimer = setInterval(async () => {
await this.keepAlive();
}, this._keepaliveMs);
return connection;
}
destroy () {
this._connection.end();
clearInterval(this._keepaliveTimer);
this._keepaliveTimer = undefined;
if (this._ssh) this._ssh.close();
}
private async keepAlive () {
const connection = await this._connection.connect() as pg.PoolClient;
await connection.query('SELECT 1+1');
connection.release();
}
use (schema: string, connection?: pg.Client | pg.PoolClient) {
this._schema = schema;

View File

@ -416,7 +416,7 @@ const clients = [
{ name: 'MariaDB', slug: 'maria' },
{ name: 'PostgreSQL', slug: 'pg' },
{ name: 'SQLite', slug: 'sqlite' },
{ name: 'Firebird SQL (experimental)', slug: 'firebird' }
{ name: 'Firebird SQL', slug: 'firebird' }
];
const connection = ref({

View File

@ -429,7 +429,7 @@ const clients = [
{ name: 'MariaDB', slug: 'maria' },
{ name: 'PostgreSQL', slug: 'pg' },
{ name: 'SQLite', slug: 'sqlite' },
{ name: 'Firebird SQL (experimental)', slug: 'firebird' }
{ name: 'Firebird SQL', slug: 'firebird' }
];
const firstInput: Ref<HTMLInputElement> = ref(null);

View File

@ -22,7 +22,7 @@ export const itIT = {
settings: 'Impostazioni',
general: 'Generale',
themes: 'Temi',
update: 'Aggiornamento',
update: 'Aggiorna',
about: 'Informazioni',
language: 'Lingua',
version: 'Versione',
@ -337,7 +337,7 @@ export const itIT = {
executeSelectedQuery: 'Esegui la query selezionata',
defaultCopyType: 'Tipo di copia default',
showTableSize: 'Mostra dimensioni tabella nella sidebar',
showTableSizeDescription: 'Solo MySQL/MariaDB. Abilitare questa opzione può compmpromettere le performance in schemi con molte tabelle.',
showTableSizeDescription: 'Solo MySQL/MariaDB. Abilitare questa opzione può compromettere le performance in schemi con molte tabelle.',
searchForSchemas: 'Cerca schemi',
switchSearchMethod: 'Cambia metodo di ricerca',
noResultsPresent: 'Nessun risultato presente'