Skip to content

Commit

Permalink
fix: respect enableKeepAlive option (#2016)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
n0v1 and n0v1 authored May 23, 2023
1 parent 440ad61 commit f465c3e
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ 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,
keepAliveInitialDelay: 0
});
```
The pool does not create all connections upfront but creates them on demand until the connection limit is reached.
Expand Down
4 changes: 3 additions & 1 deletion documentation_zh-cn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ const pool = mysql.createPool({
database: 'test',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
queueLimit: 0,
enableKeepAlive: true,
keepAliveInitialDelay: 0
});
```
该池不会预先创建所有连接,而是根据需要创建它们,直到达到连接限制。
Expand Down
7 changes: 4 additions & 3 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
// Optionally enable keep-alive on the socket.
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
Expand Down
2 changes: 1 addition & 1 deletion lib/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
5 changes: 2 additions & 3 deletions typings/mysql/lib/Pool.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,12 @@ 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;

/**
* 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;
}
Expand Down

0 comments on commit f465c3e

Please sign in to comment.