Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mysql max lifetime 추가 #302

Merged
merged 3 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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