use pump to pipe sockets

Ensures that destination socket close or destroy also does the same for
the source socket.
This commit is contained in:
Roman Shtylman
2018-05-16 10:21:56 -04:00
parent 743895720c
commit 317db73bdc
7 changed files with 97 additions and 50 deletions

View File

@@ -1,6 +1,6 @@
import http from 'http';
import TunnelAgent from './TunnelAgent';
import Debug from 'debug';
import pump from 'pump';
// A client encapsulates req/res handling using an agent
//
@@ -9,9 +9,11 @@ import TunnelAgent from './TunnelAgent';
class Client {
constructor(options) {
this.agent = options.agent;
this.debug = Debug('lt:Client');
}
handleRequest(req, res) {
this.debug('> %s', req.url);
const opt = {
path: req.url,
agent: this.agent,
@@ -20,23 +22,38 @@ class Client {
};
const clientReq = http.request(opt, (clientRes) => {
this.debug('< %s', req.url);
// write response code and headers
res.writeHead(clientRes.statusCode, clientRes.headers);
clientRes.pipe(res);
// using pump is deliberate - see the pump docs for why
pump(clientRes, res);
});
// this can happen when underlying agent produces an error
// in our case we 504 gateway error this?
// if we have already sent headers?
clientReq.once('error', (err) => {
// TODO(roman): if headers not sent - respond with gateway unavailable
});
req.pipe(clientReq);
// using pump is deliberate - see the pump docs for why
pump(req, clientReq);
}
handleUpgrade(req, socket) {
this.debug('> [up] %s', req.url);
socket.once('error', (err) => {
// These client side errors can happen if the client dies while we are reading
// We don't need to surface these in our logs.
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
console.error(err);
});
this.agent.createConnection({}, (err, conn) => {
this.debug('< [up] %s', req.url);
// any errors getting a connection mean we cannot service this request
if (err) {
socket.end();
@@ -45,6 +62,7 @@ class Client {
// socket met have disconnected while we waiting for a socket
if (!socket.readable || !socket.writable) {
conn.destroy();
socket.end();
return;
}
@@ -60,7 +78,9 @@ class Client {
arr.push('');
arr.push('');
conn.pipe(socket).pipe(conn);
// using pump is deliberate - see the pump docs for why
pump(conn, socket);
pump(socket, conn);
conn.write(arr.join('\r\n'));
});
}

View File

@@ -20,6 +20,8 @@ class ClientManager {
};
this.debug = Debug('lt:ClientManager');
this.graceTimeout = null;
}
// create a new tunnel with `id`
@@ -36,11 +38,13 @@ class ClientManager {
const maxSockets = this.opt.max_tcp_sockets;
const agent = new TunnelAgent({
clientId: id,
maxSockets: 10,
});
agent.on('online', () => {
this.debug('client online %s', id);
clearTimeout(this.graceTimeout);
});
agent.on('offline', () => {
@@ -48,7 +52,11 @@ class ClientManager {
// this period is short as the client is expected to maintain connections actively
// if they client does not reconnect on a dropped connection they need to re-establish
this.debug('client offline %s', id);
this.removeClient(id);
// client is given a grace period in which they can re-connect before they are _removed_
this.graceTimeout = setTimeout(() => {
this.removeClient(id);
}, 1000);
});
// TODO(roman): an agent error removes the client, the user needs to re-connect?
@@ -81,6 +89,7 @@ class ClientManager {
}
removeClient(id) {
this.debug('removing client: %s', id);
const client = this.clients[id];
if (!client) {
return;

View File

@@ -46,6 +46,12 @@ describe('ClientManager', () => {
const closePromise = new Promise(resolve => socket.once('close', resolve));
socket.end();
await closePromise;
// should still have client - grace period has not expired
assert(manager.hasClient('foobar'));
// wait past grace period (1s)
await new Promise(resolve => setTimeout(resolve, 1500));
assert(!manager.hasClient('foobar'));
});
}).timeout(5000);
});

View File

@@ -25,10 +25,10 @@ class TunnelAgent extends Agent {
// once a socket is available it is handed out to the next callback
this.waitingCreateConn = [];
this.debug = Debug('lt:TunnelAgent');
this.debug = Debug(`lt:TunnelAgent[${options.clientId}]`);
// track maximum allowed sockets
this.activeSockets = 0;
this.connectedSockets = 0;
this.maxTcpSockets = options.maxTcpSockets || DEFAULT_MAX_SOCKETS;
// new tcp server to service requests for this client
@@ -36,6 +36,7 @@ class TunnelAgent extends Agent {
// flag to avoid double starts
this.started = false;
this.closed = false;
}
listen() {
@@ -48,8 +49,7 @@ class TunnelAgent extends Agent {
server.on('close', this._onClose.bind(this));
server.on('connection', this._onConnection.bind(this));
server.on('error', (err) => {
// where do these errors come from?
// other side creates a connection and then is killed?
// These errors happen from killed connections, we don't worry about them
if (err.code == 'ECONNRESET' || err.code == 'ETIMEDOUT') {
return;
}
@@ -70,11 +70,12 @@ class TunnelAgent extends Agent {
}
_onClose() {
this.closed = true;
this.debug('closed tcp socket');
clearTimeout(this.connTimeout);
// we will not invoke these callbacks?
// TODO(roman): we could invoke these with errors...?
// this makes downstream have to handle this
// flush any waiting connections
for (const conn of this.waitingCreateConn) {
conn(new Error('closed'), null);
}
this.waitingCreateConn = [];
this.emit('end');
}
@@ -82,37 +83,23 @@ class TunnelAgent extends Agent {
// new socket connection from client for tunneling requests to client
_onConnection(socket) {
// no more socket connections allowed
if (this.activeSockets >= this.maxTcpSockets) {
if (this.connectedSockets >= this.maxTcpSockets) {
this.debug('no more sockets allowed');
socket.destroy();
return false;
}
// a new socket becomes available
if (this.activeSockets == 0) {
this.emit('online');
}
this.activeSockets += 1;
this.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// a single connection is enough to keep client id slot open
clearTimeout(this.connTimeout);
socket.once('close', (had_error) => {
this.debug('closed socket (error: %s)', had_error);
this.debug('removing socket');
this.activeSockets -= 1;
socket.once('close', (hadError) => {
this.debug('closed socket (error: %s)', hadError);
this.connectedSockets -= 1;
// remove the socket from available list
const idx = this.availableSockets.indexOf(socket);
if (idx >= 0) {
this.availableSockets.splice(idx, 1);
}
// need to track total sockets, not just active available
this.debug('remaining client sockets: %s', this.availableSockets.length);
// no more sockets for this session
// the session will become inactive if client does not reconnect
if (this.availableSockets.length <= 0) {
this.debug('connected sockets: %s', this.connectedSockets);
if (this.connectedSockets <= 0) {
this.debug('all sockets disconnected');
this.emit('offline');
}
@@ -125,28 +112,38 @@ class TunnelAgent extends Agent {
socket.destroy();
});
// make socket available for those waiting on sockets
this.availableSockets.push(socket);
if (this.connectedSockets === 0) {
this.emit('online');
}
// flush anyone waiting on sockets
this._callWaitingCreateConn();
}
this.connectedSockets += 1;
this.debug('new connection from: %s:%s', socket.address().address, socket.address().port);
// invoke when a new socket is available and there may be waiting createConnection calls
_callWaitingCreateConn() {
// if there are queued callbacks, give this socket now and don't queue into available
const fn = this.waitingCreateConn.shift();
if (!fn) {
if (fn) {
this.debug('giving socket to queued conn request');
setTimeout(() => {
fn(null, socket);
}, 0);
return;
}
this.debug('handling queued request');
this.createConnection({}, fn);
// make socket available for those waiting on sockets
this.availableSockets.push(socket);
}
// fetch a socket from the available socket pool for the agent
// if no socket is available, queue
// cb(err, socket)
createConnection(options, cb) {
if (this.closed) {
cb(new Error('closed'));
return;
}
this.debug('create connection');
// socket is a tcp connection back to the user hosting the site
const sock = this.availableSockets.shift();
@@ -154,7 +151,8 @@ class TunnelAgent extends Agent {
// wait until we have one
if (!sock) {
this.waitingCreateConn.push(cb);
this.debug('waiting');
this.debug('waiting connected: %s', this.connectedSockets);
this.debug('waiting available: %s', this.availableSockets.length);
return;
}