mirror of
https://github.com/fluencelabs/js-libp2p-tcp
synced 2025-04-25 15:12:18 +00:00
fix: destroy hanging connections after timeout
This commit is contained in:
parent
a008d1db34
commit
4a121696d1
24
src/index.js
24
src/index.js
@ -56,6 +56,17 @@ function TCP () {
|
|||||||
}
|
}
|
||||||
handler(conn)
|
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(), () => {
|
listener.listen(m.toOptions(), () => {
|
||||||
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
|
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
|
||||||
const address = listener.address()
|
const address = listener.address()
|
||||||
@ -86,13 +97,22 @@ function TCP () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.close = (callback) => {
|
this.close = (callback) => {
|
||||||
|
const closeTimeout = 300
|
||||||
|
|
||||||
if (listeners.length === 0) {
|
if (listeners.length === 0) {
|
||||||
log('Called close with no active listeners')
|
log('Called close with no active listeners')
|
||||||
return callback()
|
return callback()
|
||||||
}
|
}
|
||||||
|
|
||||||
parallel(listeners.map((listener) => {
|
parallel(listeners.map((listener) => (cb) => {
|
||||||
return (cb) => listener.close(cb)
|
setTimeout(() => {
|
||||||
|
Object.keys(listener.__connections).forEach((key) => {
|
||||||
|
log('destroying %s', key)
|
||||||
|
listener.__connections[key].destroy()
|
||||||
|
})
|
||||||
|
}, closeTimeout)
|
||||||
|
|
||||||
|
listener.close(cb)
|
||||||
}), callback)
|
}), callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user