Skip to content

Commit ee5352b

Browse files
committed
add mysql keep alive
1 parent f6d6d20 commit ee5352b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

electron/drivers/mysql.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ export interface MySqlConnectionConfig {
151151
export default class MySQLDriver implements BaseDriver {
152152
db?: Pool;
153153
connectionString: MySqlConnectionConfig;
154+
keepAliveTimer: NodeJS.Timeout | null = null;
155+
isPinging = false;
154156

155157
constructor(connectionString: MySqlConnectionConfig) {
156158
this.connectionString = connectionString;
@@ -160,6 +162,10 @@ export default class MySQLDriver implements BaseDriver {
160162
if (this.db) {
161163
await this.db.end();
162164
}
165+
166+
if (this.keepAliveTimer) {
167+
clearInterval(this.keepAliveTimer);
168+
}
163169
}
164170

165171
protected async getConnection() {
@@ -170,7 +176,25 @@ export default class MySQLDriver implements BaseDriver {
170176
dateStrings: true,
171177
pool: { min: 1, max: 1 },
172178
connectionLimit: 1,
179+
enableKeepAlive: true,
173180
});
181+
182+
this.keepAliveTimer = setInterval(() => {
183+
if (this.isPinging) return;
184+
185+
this.isPinging = true;
186+
this.db
187+
?.getConnection()
188+
.then((conn) => {
189+
conn.ping();
190+
conn.release();
191+
})
192+
.catch(console.error)
193+
.finally(() => {
194+
this.isPinging = false;
195+
});
196+
}, 6000);
197+
174198
return this.db;
175199
}
176200

0 commit comments

Comments
 (0)