mirror of
https://github.com/bitinflow/server.git
synced 2026-03-13 13:35:53 +00:00
server: make sure client id are released when unused
When clients disconnect, their tcp server should be shutdown and the id released after a grace period.
This commit is contained in:
@@ -123,6 +123,10 @@ var duplex = function(remote_host, remote_port, local_host, local_port) {
|
|||||||
}, 1000);
|
}, 1000);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
internal.on('connect', function() {
|
||||||
|
console.log('connected to local server');
|
||||||
|
});
|
||||||
|
|
||||||
upstream.pipe(internal).pipe(upstream);
|
upstream.pipe(internal).pipe(upstream);
|
||||||
})();
|
})();
|
||||||
|
|
||||||
|
|||||||
38
server.js
38
server.js
@@ -309,6 +309,11 @@ server.on('request', function(req, res) {
|
|||||||
|
|
||||||
var id = requested_id || rand_id();
|
var id = requested_id || rand_id();
|
||||||
|
|
||||||
|
// if the id already exists, this client must use something else
|
||||||
|
if (clients[id]) {
|
||||||
|
id = rand_id();
|
||||||
|
}
|
||||||
|
|
||||||
// maximum number of tcp connections the client can setup
|
// maximum number of tcp connections the client can setup
|
||||||
// each tcp channel allows for more parallel requests
|
// each tcp channel allows for more parallel requests
|
||||||
var max_tcp_sockets = 4;
|
var max_tcp_sockets = 4;
|
||||||
@@ -336,48 +341,61 @@ server.on('request', function(req, res) {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
var conn_timeout;
|
||||||
|
|
||||||
// user has 5 seconds to connect before their slot is given up
|
// user has 5 seconds to connect before their slot is given up
|
||||||
var conn_timeout = setTimeout(function() {
|
function maybe_tcp_close() {
|
||||||
|
conn_timeout = setTimeout(function() {
|
||||||
client_server.close();
|
client_server.close();
|
||||||
}, 5000);
|
}, 5000);
|
||||||
|
}
|
||||||
|
|
||||||
|
maybe_tcp_close();
|
||||||
|
|
||||||
// no longer accepting connections for this id
|
// no longer accepting connections for this id
|
||||||
client_server.on('close', function() {
|
client_server.on('close', function() {
|
||||||
|
log.trace('closed tcp socket for client(%s)', id);
|
||||||
|
clearTimeout(conn_timeout);
|
||||||
delete clients[id];
|
delete clients[id];
|
||||||
});
|
});
|
||||||
|
|
||||||
var count = 0;
|
|
||||||
client_server.on('connection', function(socket) {
|
client_server.on('connection', function(socket) {
|
||||||
|
|
||||||
// no more socket connections allowed
|
// no more socket connections allowed
|
||||||
if (count++ >= max_tcp_sockets) {
|
if (client.sockets.length >= max_tcp_sockets) {
|
||||||
return socket.end();
|
return socket.end();
|
||||||
}
|
}
|
||||||
|
|
||||||
log.trace('new connection for id: %s', id);
|
log.trace('new connection for id: %s', id);
|
||||||
|
|
||||||
// multiplexes socket data out to clients
|
|
||||||
socket.ondata = socketOnData;
|
|
||||||
|
|
||||||
// no need to close the client server
|
// no need to close the client server
|
||||||
clearTimeout(conn_timeout);
|
clearTimeout(conn_timeout);
|
||||||
|
|
||||||
// add socket to pool for this id
|
// multiplexes socket data out to clients
|
||||||
var idx = client.sockets.push(socket) - 1;
|
socket.ondata = socketOnData;
|
||||||
|
|
||||||
|
client.sockets.push(socket);
|
||||||
|
|
||||||
socket.on('close', function(had_error) {
|
socket.on('close', function(had_error) {
|
||||||
count--;
|
log.trace('client %s closed socket', id);
|
||||||
|
|
||||||
|
// remove this socket
|
||||||
|
var idx = client.sockets.indexOf(socket);
|
||||||
client.sockets.splice(idx, 1);
|
client.sockets.splice(idx, 1);
|
||||||
|
|
||||||
|
log.trace('remaining client sockets: %s', client.sockets.length);
|
||||||
|
|
||||||
// no more sockets for this ident
|
// no more sockets for this ident
|
||||||
if (client.sockets.length === 0) {
|
if (client.sockets.length === 0) {
|
||||||
delete clients[id];
|
log.trace('all client(%s) sockets disconnected', id);
|
||||||
|
maybe_tcp_close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// close will be emitted after this
|
// close will be emitted after this
|
||||||
socket.on('error', function(err) {
|
socket.on('error', function(err) {
|
||||||
log.error(err);
|
log.error(err);
|
||||||
|
socket.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user