From 17eeb6d38e45b553e35e004b748569971743ca18 Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Sun, 14 May 2023 18:48:21 +0200 Subject: [PATCH] feat: keepalive on mysql/postgre connections, should fix #577 --- src/main/libs/clients/MySQLClient.ts | 15 +++++++++++++++ src/main/libs/clients/PostgreSQLClient.ts | 15 +++++++++++++++ .../components/WorkspaceAddConnectionPanel.vue | 2 +- .../components/WorkspaceEditConnectionPanel.vue | 2 +- src/renderer/i18n/it-IT.ts | 4 ++-- 5 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/libs/clients/MySQLClient.ts b/src/main/libs/clients/MySQLClient.ts index b26ebffa..12d9ca76 100644 --- a/src/main/libs/clients/MySQLClient.ts +++ b/src/main/libs/clients/MySQLClient.ts @@ -9,6 +9,8 @@ export class MySQLClient extends AntaresCore { private _schema?: string; private _runningConnections: Map; private _connectionsToCommit: Map; + 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}\``); diff --git a/src/main/libs/clients/PostgreSQLClient.ts b/src/main/libs/clients/PostgreSQLClient.ts index ac2cbec6..93c16bab 100644 --- a/src/main/libs/clients/PostgreSQLClient.ts +++ b/src/main/libs/clients/PostgreSQLClient.ts @@ -84,6 +84,8 @@ export class PostgreSQLClient extends AntaresCore { private _schema?: string; private _runningConnections: Map; private _connectionsToCommit: Map; + 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; diff --git a/src/renderer/components/WorkspaceAddConnectionPanel.vue b/src/renderer/components/WorkspaceAddConnectionPanel.vue index e63adf37..abae1675 100644 --- a/src/renderer/components/WorkspaceAddConnectionPanel.vue +++ b/src/renderer/components/WorkspaceAddConnectionPanel.vue @@ -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({ diff --git a/src/renderer/components/WorkspaceEditConnectionPanel.vue b/src/renderer/components/WorkspaceEditConnectionPanel.vue index 4a1c0047..029bf57f 100644 --- a/src/renderer/components/WorkspaceEditConnectionPanel.vue +++ b/src/renderer/components/WorkspaceEditConnectionPanel.vue @@ -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 = ref(null); diff --git a/src/renderer/i18n/it-IT.ts b/src/renderer/i18n/it-IT.ts index 52be4c1f..58160056 100644 --- a/src/renderer/i18n/it-IT.ts +++ b/src/renderer/i18n/it-IT.ts @@ -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'