From 4a121696d12faa85f1d386cd596c63b0b673ced3 Mon Sep 17 00:00:00 2001 From: dignifiedquire Date: Tue, 31 May 2016 14:48:18 +0200 Subject: [PATCH] fix: destroy hanging connections after timeout --- src/index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 383fd6a..a44e1e8 100644 --- a/src/index.js +++ b/src/index.js @@ -56,6 +56,17 @@ function TCP () { } handler(conn) }) + + listener.__connections = {} + listener.on('connection', (conn) => { + const key = `${conn.remoteAddress}:${conn.remotePort}` + listener.__connections[key] = conn + + conn.on('close', () => { + delete listener.__connections[key] + }) + }) + listener.listen(m.toOptions(), () => { // Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g) const address = listener.address() @@ -86,13 +97,22 @@ function TCP () { } this.close = (callback) => { + const closeTimeout = 300 + if (listeners.length === 0) { log('Called close with no active listeners') return callback() } - parallel(listeners.map((listener) => { - return (cb) => listener.close(cb) + parallel(listeners.map((listener) => (cb) => { + setTimeout(() => { + Object.keys(listener.__connections).forEach((key) => { + log('destroying %s', key) + listener.__connections[key].destroy() + }) + }, closeTimeout) + + listener.close(cb) }), callback) }