Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix: destroy hanging connections after timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed May 31, 2016
1 parent a008d1d commit 4a12169
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 4a12169

Please sign in to comment.