Skip to content

Respect enableKeepAlive option #2016

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

Merged
merged 5 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
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