mirror of
https://github.com/Fabio286/antares.git
synced 2025-06-05 21:59:22 +02:00
feat: keepalive on mysql/postgre connections, should fix #577
This commit is contained in:
@ -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;
|
||||
|
||||
|
Reference in New Issue
Block a user