Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
net, tls, http: remove socket.ondrain
Browse files Browse the repository at this point in the history
Replace the ondrain hack with a regular 'drain' listener. Speeds up the
bytes/1024 http benchmark by about 1.2%.
  • Loading branch information
bnoordhuis committed Jan 24, 2012
1 parent 5988872 commit e806ad3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
14 changes: 8 additions & 6 deletions lib/http.js
Expand Up @@ -1406,13 +1406,15 @@ exports.get = function(options, cb) {
return req;
};


function ondrain() {
if (this._httpMessage) this._httpMessage.emit('drain');
}


function httpSocketSetup(socket) {
// NOTE: be sure not to use ondrain elsewhere in this file!
socket.ondrain = function() {
if (socket._httpMessage) {
socket._httpMessage.emit('drain');
}
};
socket.removeListener('drain', ondrain);

This comment has been minimized.

Copy link
@shigeki

shigeki Apr 24, 2012

@bnoordhuis The function 'ondrain' refers the same as the one added in the next line. It seems the same listener to be removed and added. If this line is intended to cleanup all drain listeners previously added elsewhere, is e40b07e right way to do it?

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Apr 24, 2012

Author Member

It's defense in depth, the idea being that if the ondrain handler inadvertently wasn't removed, it will be now. I'm not sure if removing all drain listeners is a good idea. It won't break anything in core but it might break third-party code that relies on this particular implementation detail.

This comment has been minimized.

Copy link
@shigeki

shigeki Apr 24, 2012

@bnoordhuis Thanks Ben. I understood. I missed to figure out that socket.ondrain and listener on socket.on('drain', ) are independent so that at every time socket is recycled through Agent, the listener need to be removed and added not to be duplicated while that of socket.ondrain always is overwritten.
The deep understanding of behaviors of connectionListener is still very hard for me. Sorry for bothering you.

This comment has been minimized.

Copy link
@bnoordhuis

bnoordhuis Apr 24, 2012

Author Member

No worries, Shigeki. lib/http.js is by far the most complicated part of Node. I work with it on a daily basis and even I don't have a comprehensive mental image of it.

socket.on('drain', ondrain);
}


Expand Down
2 changes: 0 additions & 2 deletions lib/net.js
Expand Up @@ -516,8 +516,6 @@ function afterWrite(status, handle, req, buffer) {
self._pendingWriteReqs--;

if (self._pendingWriteReqs == 0) {
// TODO remove all uses of ondrain - this is not a good hack.
if (self.ondrain) self.ondrain();
self.emit('drain');
}

Expand Down
3 changes: 0 additions & 3 deletions lib/tls.js
Expand Up @@ -461,9 +461,6 @@ CryptoStream.prototype._pull = function() {
debug('drain ' + (this === this.pair.cleartext ? 'clear' : 'encrypted'));
var self = this;
process.nextTick(function() {
if (typeof self.ondrain === 'function') {
self.ondrain();
}
self.emit('drain');
});
this._needDrain = false;
Expand Down

0 comments on commit e806ad3

Please sign in to comment.