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

Commit 4a12169

Browse files
fix: destroy hanging connections after timeout
1 parent a008d1d commit 4a12169

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/index.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ function TCP () {
5656
}
5757
handler(conn)
5858
})
59+
60+
listener.__connections = {}
61+
listener.on('connection', (conn) => {
62+
const key = `${conn.remoteAddress}:${conn.remotePort}`
63+
listener.__connections[key] = conn
64+
65+
conn.on('close', () => {
66+
delete listener.__connections[key]
67+
})
68+
})
69+
5970
listener.listen(m.toOptions(), () => {
6071
// Node.js likes to convert addr to IPv6 (when 0.0.0.0 for e.g)
6172
const address = listener.address()
@@ -86,13 +97,22 @@ function TCP () {
8697
}
8798

8899
this.close = (callback) => {
100+
const closeTimeout = 300
101+
89102
if (listeners.length === 0) {
90103
log('Called close with no active listeners')
91104
return callback()
92105
}
93106

94-
parallel(listeners.map((listener) => {
95-
return (cb) => listener.close(cb)
107+
parallel(listeners.map((listener) => (cb) => {
108+
setTimeout(() => {
109+
Object.keys(listener.__connections).forEach((key) => {
110+
log('destroying %s', key)
111+
listener.__connections[key].destroy()
112+
})
113+
}, closeTimeout)
114+
115+
listener.close(cb)
96116
}), callback)
97117
}
98118

0 commit comments

Comments
 (0)