Skip to content

Commit

Permalink
add mysql keep alive
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Jan 3, 2025
1 parent f6d6d20 commit ee5352b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions electron/drivers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ export interface MySqlConnectionConfig {
export default class MySQLDriver implements BaseDriver {
db?: Pool;
connectionString: MySqlConnectionConfig;
keepAliveTimer: NodeJS.Timeout | null = null;
isPinging = false;

constructor(connectionString: MySqlConnectionConfig) {
this.connectionString = connectionString;
Expand All @@ -160,6 +162,10 @@ export default class MySQLDriver implements BaseDriver {
if (this.db) {
await this.db.end();
}

if (this.keepAliveTimer) {
clearInterval(this.keepAliveTimer);
}
}

protected async getConnection() {
Expand All @@ -170,7 +176,25 @@ export default class MySQLDriver implements BaseDriver {
dateStrings: true,
pool: { min: 1, max: 1 },
connectionLimit: 1,
enableKeepAlive: true,
});

this.keepAliveTimer = setInterval(() => {
if (this.isPinging) return;

this.isPinging = true;
this.db
?.getConnection()
.then((conn) => {
conn.ping();
conn.release();
})
.catch(console.error)
.finally(() => {
this.isPinging = false;
});
}, 6000);

return this.db;
}

Expand Down

0 comments on commit ee5352b

Please sign in to comment.