From ec1fe22b83609208d130ab6b4c840999882b760b Mon Sep 17 00:00:00 2001 From: Christopher Fenn Date: Wed, 17 May 2023 10:51:17 +0200 Subject: [PATCH 1/5] respect enableKeepAlive option --- lib/connection.js | 7 ++++--- lib/connection_config.js | 2 +- typings/mysql/lib/Pool.d.ts | 3 +-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index ed8442aac9..b7eedfd230 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -52,9 +52,10 @@ class Connection extends EventEmitter { opts.config.host ); - // Enable keep-alive on the socket. It's disabled by default, but the - // user can enable it and supply an initial delay. - this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay); + // Enable keep-alive on the socket. It's enabled by default. + if (this.config.enableKeepAlive) { + this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay); + } // Enable TCP_NODELAY flag. This is needed so that the network packets // are sent immediately to the server diff --git a/lib/connection_config.js b/lib/connection_config.js index edb7fcff90..236cf6747f 100644 --- a/lib/connection_config.js +++ b/lib/connection_config.js @@ -115,7 +115,7 @@ class ConnectionConfig { this.debug = options.debug; this.trace = options.trace !== false; this.stringifyObjects = options.stringifyObjects || false; - this.enableKeepAlive = !!options.enableKeepAlive; + this.enableKeepAlive = options.enableKeepAlive !== false; this.keepAliveInitialDelay = options.keepAliveInitialDelay || 0; if ( options.timezone && diff --git a/typings/mysql/lib/Pool.d.ts b/typings/mysql/lib/Pool.d.ts index 1bace502db..916155c10d 100644 --- a/typings/mysql/lib/Pool.d.ts +++ b/typings/mysql/lib/Pool.d.ts @@ -44,8 +44,7 @@ declare namespace Pool { queueLimit?: number; /** - * Enable keep-alive on the socket. It's disabled by default, but the - * user can enable it and supply an initial delay. + * Enable keep-alive on the socket. (Default: true) */ enableKeepAlive?: boolean; From 51f822ad75ed8a29bfac4559fba7f3ff3d4ae489 Mon Sep 17 00:00:00 2001 From: Christopher Fenn Date: Wed, 17 May 2023 11:34:01 +0200 Subject: [PATCH 2/5] improve comment --- lib/connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/connection.js b/lib/connection.js index b7eedfd230..8e861bca8a 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -52,7 +52,7 @@ class Connection extends EventEmitter { opts.config.host ); - // Enable keep-alive on the socket. It's enabled by default. + // Optionally enable keep-alive on the socket. if (this.config.enableKeepAlive) { this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay); } From 43e443acce38d248204448b5ffd4f1ea69e168c3 Mon Sep 17 00:00:00 2001 From: Christopher Fenn Date: Wed, 17 May 2023 12:07:51 +0200 Subject: [PATCH 3/5] document default for keepAliveInitialDelay --- typings/mysql/lib/Pool.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typings/mysql/lib/Pool.d.ts b/typings/mysql/lib/Pool.d.ts index 916155c10d..4ad74e772f 100644 --- a/typings/mysql/lib/Pool.d.ts +++ b/typings/mysql/lib/Pool.d.ts @@ -49,7 +49,7 @@ declare namespace Pool { enableKeepAlive?: boolean; /** - * If keep-alive is enabled users can supply an initial delay. + * If keep-alive is enabled users can supply an initial delay. (Default: 0) */ keepAliveInitialDelay?: number; } From c3bbb5d6e6407097ae6533f1ba65a2ef942dd788 Mon Sep 17 00:00:00 2001 From: Christopher Fenn Date: Wed, 17 May 2023 13:21:28 +0200 Subject: [PATCH 4/5] add enableKeepAlive option to pool examples --- README.md | 3 ++- documentation_zh-cn/README.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2c43809678..46a2e31fd5 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,8 @@ const pool = mysql.createPool({ connectionLimit: 10, maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit` idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000 - queueLimit: 0 + queueLimit: 0, + enableKeepAlive: true }); ``` The pool does not create all connections upfront but creates them on demand until the connection limit is reached. diff --git a/documentation_zh-cn/README.md b/documentation_zh-cn/README.md index 9e33860d42..92da668f45 100644 --- a/documentation_zh-cn/README.md +++ b/documentation_zh-cn/README.md @@ -132,7 +132,8 @@ const pool = mysql.createPool({ database: 'test', waitForConnections: true, connectionLimit: 10, - queueLimit: 0 + queueLimit: 0, + enableKeepAlive: true }); ``` 该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。 From 6107c1dc6a3ad4ddbf85b0685232b9f8b76032e0 Mon Sep 17 00:00:00 2001 From: Christopher Fenn Date: Mon, 22 May 2023 16:15:56 +0200 Subject: [PATCH 5/5] also add keepAliveInitialDelay option to pool examples --- README.md | 3 ++- documentation_zh-cn/README.md | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 46a2e31fd5..7f7e8fed43 100644 --- a/README.md +++ b/README.md @@ -135,7 +135,8 @@ const pool = mysql.createPool({ maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit` idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000 queueLimit: 0, - enableKeepAlive: true + enableKeepAlive: true, + keepAliveInitialDelay: 0 }); ``` The pool does not create all connections upfront but creates them on demand until the connection limit is reached. diff --git a/documentation_zh-cn/README.md b/documentation_zh-cn/README.md index 92da668f45..db420eb84f 100644 --- a/documentation_zh-cn/README.md +++ b/documentation_zh-cn/README.md @@ -133,7 +133,8 @@ const pool = mysql.createPool({ waitForConnections: true, connectionLimit: 10, queueLimit: 0, - enableKeepAlive: true + enableKeepAlive: true, + keepAliveInitialDelay: 0 }); ``` 该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。