Abstract out net.Server.prototype._rejectPending

Does the same timeout action for maxConnections as it does for EMFILE.

Conflicts:

	lib/net.js
This commit is contained in:
Ryan Dahl committed 2010-11-16 10:25:53 -08:00
1 parent 26b3f228ad
commit d2828ea2a5
2 files changed
+34 -25

No files matched your search

+2
View File
@@ -11,6 +11,8 @@ server = net.Server(function (socket) {
});
server.maxConnections = 128;
server.listen(9000);
var oldConnections, oldErrors;
+32 -25
View File
@@ -1086,7 +1086,7 @@ function Server (listener) {
self.watcher.callback = function () {
// Just in case we don't have a dummy fd.
getDummyFD();
if (self._acceptTimer) {
// Somehow the watcher got started again. Need to wait until
// the timer finishes.
@@ -1102,36 +1102,17 @@ function Server (listener) {
// Gracefully reject pending clients by freeing up a file
// descriptor.
rescueEMFILE(function() {
var acceptCount = 0;
// Accept and close the waiting clients one at a time.
// Single threaded programming ftw.
while (true) {
peerInfo = accept(self.fd);
if (!peerInfo) break;
close(peerInfo.fd);
// Don't become DoS'd by incoming requests
if (++acceptCount > 50) {
assert(!self._acceptTimer);
self.watcher.stop();
// Wait a second before accepting more.
self._acceptTimer = setTimeout(function () {
assert(parseInt(self.fd) >= 0);
self._acceptTimer = null;
self.watcher.start();
}, 1000);
break;
}
}
self._rejectPending();
});
return;
}
if (!peerInfo) return;
if (self.maxConnections && self.connections >= self.maxConnections) {
// Accept and close the connection.
// Close the connection we just had
close(peerInfo.fd);
// Reject all other pending connectins.
self._rejectPending();
return;
}
@@ -1166,6 +1147,32 @@ exports.createServer = function (listener) {
};
Server.prototype._rejectPending = function () {
var self = this;
var acceptCount = 0;
// Accept and close the waiting clients one at a time.
// Single threaded programming ftw.
while (true) {
peerInfo = accept(this.fd);
if (!peerInfo) return;
close(peerInfo.fd);
// Don't become DoS'd by incoming requests
if (++acceptCount > 50) {
assert(!this._acceptTimer);
this.watcher.stop();
// Wait a second before accepting more.
this._acceptTimer = setTimeout(function () {
assert(parseInt(self.fd) >= 0);
self._acceptTimer = null;
self.watcher.start();
}, 1000);
return;
}
}
};
// Listen on a UNIX socket
// server.listen("/tmp/socket");
//
@@ -1298,7 +1305,7 @@ function rescueEMFILE(callback) {
console.error("(node) Hit max file limit. Increase 'ulimit -n'.");
lastEMFILEWarning = now;
}
if (dummyFD) {
close(dummyFD);
dummyFD = null;