Skip to content

Add 'drain' event to Protocol and Connection. #272

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

Closed
wants to merge 3 commits into from
Closed
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
34 changes: 34 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,40 @@ cannot be re-connected by design.
This logic will also be part of connection pool support once I add that to this
library.

## Idle connections

Connections emit `'drain'` events after they have finished all queued queries
(including query callbacks). This event could be used to detect idle
connections:

```javascript
function detectIdle(connection) {
connection._idleTimeout = null;

function clearIdleTimeout() {
if (connection.idleTimeout) {
clearTimeout(connection.idleTimeout);
}
}

connection.on('drain', function() {
clearIdleTimeout();
connection._idleTimeout = setTimeout(60000, function() {
console.error("Connection was idle for 60 seconds");
// or return to a pool, etc.
})
})

var query = connection.query;
connection.query = function() {
clearIdleTimeout();
query.apply(connection, arguments);
};
})

handleDisconnect(connection);
```

## Escaping query values

In order to avoid SQL Injection attacks, you should always escape any user
Expand Down
5 changes: 5 additions & 0 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Connection.prototype.connect = function(cb) {

this._socket.on('error', this._handleNetworkError.bind(this));
this._protocol.on('unhandledError', this._handleProtocolError.bind(this));
this._protocol.on('drain', this._handleProtocolDrain.bind(this));
this._protocol.on('close', this._handleProtocolClose.bind(this));
}

Expand Down Expand Up @@ -126,6 +127,10 @@ Connection.prototype._handleProtocolError = function(err) {
this.emit('error', err);
};

Connection.prototype._handleProtocolDrain = function(err) {
this.emit('drain', err);
};

Connection.prototype._handleProtocolClose = function(err) {
this.emit('close', err);
};
Expand Down
1 change: 1 addition & 0 deletions lib/protocol/Protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ Protocol.prototype._dequeue = function() {

var sequence = this._queue[0];
if (!sequence) {
this.emit('drain')
return;
}

Expand Down
22 changes: 22 additions & 0 deletions test/integration/connection/test-drain-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var common = require('../../common');
var connection = common.createConnection();
var assert = require('assert');

connection.connect();

var gotDrain = false;

connection.on('drain', function() {
gotDrain = true;
});

connection.query("SELECT 1", function(err) {
// drain is not emitted until after the callback completes
assert.equal(gotDrain, false);
assert.ok(!err);
process.nextTick(function() {
assert.equal(gotDrain, true);
connection.end()
})
})