Skip to content

Commit

Permalink
feat: mysql max lifetime 추가 (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
humy2833 committed Apr 16, 2024
1 parent 02745c7 commit dd58c95
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/cormo/lib/adapters/mysql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface AdapterSettingsMySQL {
collation?: string;
pool_size?: number;
query_timeout?: number;
max_lifetime?: number;
replication?: {
use_master_for_read?: boolean;
read_replicas: Array<{
Expand Down
16 changes: 16 additions & 0 deletions packages/cormo/lib/adapters/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,7 @@ class MySQLAdapter extends sql_base_1.SQLAdapterBase {
this._client._node_id = 'MASTER';
this._client.queryAsync = util_1.default.promisify(this._client.query);
this._client.getConnectionAsync = util_1.default.promisify(this._client.getConnection);
this._setEvent(this._client, settings.max_lifetime);
if (settings.replication) {
this._read_clients = [];
if (settings.replication.use_master_for_read) {
Expand All @@ -816,6 +817,7 @@ class MySQLAdapter extends sql_base_1.SQLAdapterBase {
read_client._node_id = `SLAVE${i + 1}`;
read_client.queryAsync = util_1.default.promisify(read_client.query);
read_client.getConnectionAsync = util_1.default.promisify(read_client.getConnection);
this._setEvent(read_client, settings.max_lifetime);
this._read_clients.push(read_client);
}
}
Expand Down Expand Up @@ -1278,6 +1280,20 @@ class MySQLAdapter extends sql_base_1.SQLAdapterBase {
}
return MySQLAdapter.wrapError(msg, cause);
}
/** @internal */
_setEvent(client, max_lifetime) {
if (!max_lifetime || max_lifetime < 0) {
return;
}
client.on('connection', (connection) => {
connection._connected_ts = Date.now();
});
client.on('release', (connection) => {
if (Date.now() - connection._connected_ts >= max_lifetime) {
connection.destroy();
}
});
}
}
exports.MySQLAdapter = MySQLAdapter;
function createAdapter(connection) {
Expand Down
18 changes: 18 additions & 0 deletions packages/cormo/src/adapters/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface AdapterSettingsMySQL {
collation?: string;
pool_size?: number;
query_timeout?: number;
max_lifetime?: number;
replication?: {
use_master_for_read?: boolean;
read_replicas: Array<{
Expand Down Expand Up @@ -872,6 +873,7 @@ export class MySQLAdapter extends SQLAdapterBase {
this._client._node_id = 'MASTER';
this._client.queryAsync = util.promisify(this._client.query);
this._client.getConnectionAsync = util.promisify(this._client.getConnection);
this._setEvent(this._client, settings.max_lifetime);

if (settings.replication) {
this._read_clients = [];
Expand All @@ -894,6 +896,7 @@ export class MySQLAdapter extends SQLAdapterBase {
read_client._node_id = `SLAVE${i + 1}`;
read_client.queryAsync = util.promisify(read_client.query);
read_client.getConnectionAsync = util.promisify(read_client.getConnection);
this._setEvent(read_client, settings.max_lifetime);
this._read_clients.push(read_client);
}
}
Expand Down Expand Up @@ -1374,6 +1377,21 @@ export class MySQLAdapter extends SQLAdapterBase {
}
return MySQLAdapter.wrapError(msg, cause);
}

/** @internal */
private _setEvent(client: any, max_lifetime?: number) {
if (!max_lifetime || max_lifetime <= 0) {
return;
}
client.on('connection', (connection: any) => {
connection._connected_ts = Date.now();
});
client.on('release', (connection: any) => {
if (Date.now() - connection._connected_ts >= max_lifetime) {
connection.destroy();
}
});
}
}

export function createAdapter(connection: Connection) {
Expand Down

0 comments on commit dd58c95

Please sign in to comment.