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

Commit

Permalink
net: remove unconditional getpeername() call
Browse files Browse the repository at this point in the history
Speeds up http_simple benchmark by about 1.0%
  • Loading branch information
bnoordhuis committed Oct 6, 2011
1 parent 065c6f4 commit 38dce40
Showing 1 changed file with 21 additions and 18 deletions.
39 changes: 21 additions & 18 deletions lib/net_uv.js
Expand Up @@ -339,6 +339,27 @@ Socket.prototype.setEncoding = function(encoding) {
};


Socket.prototype._getpeername = function() {
if (!this._handle || !this._handle.getpeername) {
return {};
}
if (!this._peername) {
this._peername = this._handle.getpeername();
}
return this._peername;
};


Socket.prototype.__defineGetter__('remoteAddress', function() {
return this._getpeername().address;
});


Socket.prototype.__defineGetter__('remotePort', function() {
return this._getpeername().port;
});


Socket.prototype.write = function(data /* [encoding], [fd], [cb] */) {
var encoding, fd, cb;

Expand Down Expand Up @@ -690,7 +711,6 @@ Server.prototype.address = function() {
function onconnection(clientHandle) {
var handle = this;
var self = handle.socket;
var peername;

debug('onconnection');

Expand All @@ -704,29 +724,12 @@ function onconnection(clientHandle) {
return;
}

// Todo: implement this for unix sockets
if (clientHandle.getpeername) {
peername = clientHandle.getpeername();
if (!peername.address || !peername.port) {
var err = errnoException(errno, 'accept');
clientHandle.close();
self.emit('error', err);
return;
}
}

var socket = new Socket({
handle: clientHandle,
allowHalfOpen: self.allowHalfOpen
});
socket.readable = socket.writable = true;

if (peername) {
socket.remoteAddress = peername.address;
socket.remotePort = peername.port;
// TODO: set family as well
}

socket.resume();

self.connections++;
Expand Down

0 comments on commit 38dce40

Please sign in to comment.