Skip to content

Commit f465c3e

Browse files
authored
fix: respect enableKeepAlive option (#2016)
* respect enableKeepAlive option * improve comment * document default for keepAliveInitialDelay * add enableKeepAlive option to pool examples * also add keepAliveInitialDelay option to pool examples --------- Co-authored-by: Christopher Fenn <cfenn@vwd.com>
1 parent 440ad61 commit f465c3e

File tree

5 files changed

+13
-9
lines changed

5 files changed

+13
-9
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ const pool = mysql.createPool({
134134
connectionLimit: 10,
135135
maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
136136
idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000
137-
queueLimit: 0
137+
queueLimit: 0,
138+
enableKeepAlive: true,
139+
keepAliveInitialDelay: 0
138140
});
139141
```
140142
The pool does not create all connections upfront but creates them on demand until the connection limit is reached.

documentation_zh-cn/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ const pool = mysql.createPool({
132132
database: 'test',
133133
waitForConnections: true,
134134
connectionLimit: 10,
135-
queueLimit: 0
135+
queueLimit: 0,
136+
enableKeepAlive: true,
137+
keepAliveInitialDelay: 0
136138
});
137139
```
138140
该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。

lib/connection.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ class Connection extends EventEmitter {
5252
opts.config.host
5353
);
5454

55-
// Enable keep-alive on the socket. It's disabled by default, but the
56-
// user can enable it and supply an initial delay.
57-
this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay);
55+
// Optionally enable keep-alive on the socket.
56+
if (this.config.enableKeepAlive) {
57+
this.stream.setKeepAlive(true, this.config.keepAliveInitialDelay);
58+
}
5859

5960
// Enable TCP_NODELAY flag. This is needed so that the network packets
6061
// are sent immediately to the server

lib/connection_config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class ConnectionConfig {
115115
this.debug = options.debug;
116116
this.trace = options.trace !== false;
117117
this.stringifyObjects = options.stringifyObjects || false;
118-
this.enableKeepAlive = !!options.enableKeepAlive;
118+
this.enableKeepAlive = options.enableKeepAlive !== false;
119119
this.keepAliveInitialDelay = options.keepAliveInitialDelay || 0;
120120
if (
121121
options.timezone &&

typings/mysql/lib/Pool.d.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ declare namespace Pool {
4444
queueLimit?: number;
4545

4646
/**
47-
* Enable keep-alive on the socket. It's disabled by default, but the
48-
* user can enable it and supply an initial delay.
47+
* Enable keep-alive on the socket. (Default: true)
4948
*/
5049
enableKeepAlive?: boolean;
5150

5251
/**
53-
* If keep-alive is enabled users can supply an initial delay.
52+
* If keep-alive is enabled users can supply an initial delay. (Default: 0)
5453
*/
5554
keepAliveInitialDelay?: number;
5655
}

0 commit comments

Comments
 (0)