extract ClientManager from server.js

Make client manager more robust when piping connections.
This commit is contained in:
Roman Shtylman
2017-12-11 21:16:55 -08:00
parent b4a36c78b0
commit 5fb9dbaaa1
8 changed files with 313 additions and 266 deletions

View File

@@ -16,6 +16,8 @@ const Proxy = function(opt) {
self.waiting = [];
self.id = opt.id;
self.activeSockets = 0;
// default max is 10
self.max_tcp_sockets = opt.max_tcp_sockets || 10;
@@ -72,6 +74,8 @@ Proxy.prototype._maybe_destroy = function() {
const self = this;
clearTimeout(self.conn_timeout);
// After last socket is gone, we give opportunity to connect again quickly
self.conn_timeout = setTimeout(function() {
// sometimes the server is already closed but the event has not fired?
try {
@@ -81,7 +85,7 @@ Proxy.prototype._maybe_destroy = function() {
catch (err) {
self._cleanup();
}
}, 5000);
}, 1000);
}
// new socket connection from client for tunneling requests to client
@@ -89,16 +93,19 @@ Proxy.prototype._handle_socket = function(socket) {
const self = this;
// no more socket connections allowed
if (self.sockets.length >= self.max_tcp_sockets) {
if (self.activeSockets >= self.max_tcp_sockets) {
return socket.end();
}
self.activeSockets = self.activeSockets + 1;
self.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// a single connection is enough to keep client id slot open
clearTimeout(self.conn_timeout);
socket.once('close', function(had_error) {
self.activeSockets = self.activeSockets - 1;
self.debug('closed socket (error: %s)', had_error);
// what if socket was servicing a request at this time?
@@ -133,10 +140,10 @@ Proxy.prototype._handle_socket = function(socket) {
Proxy.prototype._process_waiting = function() {
const self = this;
const wait_cb = self.waiting.shift();
if (wait_cb) {
const fn = self.waiting.shift();
if (fn) {
self.debug('handling queued request');
self.next_socket(wait_cb);
self.nextSocket(fn);
}
};
@@ -152,48 +159,31 @@ Proxy.prototype._cleanup = function() {
self.emit('end');
};
Proxy.prototype.next_socket = function(handler) {
Proxy.prototype.nextSocket = async function(fn) {
const self = this;
// socket is a tcp connection back to the user hosting the site
const sock = self.sockets.shift();
if (!sock) {
self.debug('no more client, queue callback');
self.waiting.push(handler);
self.debug('no more clients, queue callback');
self.waiting.push(fn);
return;
}
self.debug('processing request');
handler(sock)
.then(() => {
if (!sock.destroyed) {
self.debug('retuning socket');
self.sockets.push(sock);
}
await fn(sock);
// no sockets left to process waiting requests
if (self.sockets.length === 0) {
return;
}
if (!sock.destroyed) {
self.debug('retuning socket');
self.sockets.push(sock);
}
self._process_waiting();
})
.catch((err) => {
log.error(err);
// no sockets left to process waiting requests
if (self.sockets.length === 0) {
return;
}
if (!sock.destroyed) {
self.debug('retuning socket');
self.sockets.push(sock);
}
// no sockets left to process waiting requests
if (self.sockets.length === 0) {
return;
}
self._process_waiting();
})
self._process_waiting();
};
Proxy.prototype._done = function() {