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

Commit 0f4619a

Browse files
bring back connection cleanup
1 parent a711487 commit 0f4619a

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

src/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ const log = debug('libp2p:tcp')
1212
const createListener = require('./listener')
1313

1414
module.exports = class TCP {
15-
dial (ma, options, callback) {
15+
dial (ma, options, cb) {
1616
if (isFunction(options)) {
17-
callback = options
17+
cb = options
1818
options = {}
1919
}
2020

21-
if (!callback) {
22-
callback = () => {}
21+
if (!cb) {
22+
cb = () => {}
2323
}
2424

2525
const cOpts = ma.toOptions()
2626
log('Connecting to %s %s', cOpts.port, cOpts.host)
27-
const socket = toPull.duplex(net.connect(cOpts, callback))
27+
const socket = toPull.duplex(net.connect(cOpts, cb))
2828

2929
socket.getObservedAddrs = (cb) => {
3030
return cb(null, [ma])

src/listener.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const toPull = require('stream-to-pull-stream')
1212
const getMultiaddr = require('./get-multiaddr')
1313

1414
const IPFS_CODE = 421
15+
const CLOSE_TIMEOUT = 2000
1516

1617
module.exports = (handler) => {
1718
const listener = net.createServer((socket) => {
@@ -24,7 +25,41 @@ module.exports = (handler) => {
2425
handler(conn)
2526
})
2627

28+
// Keep track of open connections to destroy in case of timeout
2729
listener.__connections = {}
30+
listener.on('connection', (socket) => {
31+
const key = `${socket.remoteAddress}:${socket.remotePort}`
32+
listener.__connections[key] = socket
33+
34+
socket.on('close', () => {
35+
delete listener.__connections[key]
36+
})
37+
})
38+
39+
listener._close = listener.close
40+
listener.close = (options, cb) => {
41+
if (typeof options === 'function') {
42+
cb = options
43+
options = {}
44+
}
45+
cb = cb || (() => {})
46+
options = options || {}
47+
48+
let closed = false
49+
listener._close(cb)
50+
listener.once('close', () => {
51+
closed = true
52+
})
53+
setTimeout(() => {
54+
if (closed) return
55+
56+
log('unable to close graciously, destroying conns')
57+
Object.keys(listener.__connections).forEach((key) => {
58+
log('destroying %s', key)
59+
listener.__connections[key].destroy()
60+
})
61+
}, options.timeout || CLOSE_TIMEOUT)
62+
}
2863
let ipfsId
2964
let listeningAddr
3065

@@ -41,10 +76,14 @@ module.exports = (handler) => {
4176
return listener._listen(lOpts.port, lOpts.host, cb)
4277
}
4378

44-
listener.getAddrs = (callback) => {
79+
listener.getAddrs = (cb) => {
4580
const multiaddrs = []
4681
const address = listener.address()
4782

83+
if (!address) {
84+
return cb(new Error('Listener is not ready yet'))
85+
}
86+
4887
// Because TCP will only return the IPv6 version
4988
// we need to capture from the passed multiaddr
5089
if (listeningAddr.toString().indexOf('ip4') !== -1) {
@@ -77,7 +116,7 @@ module.exports = (handler) => {
77116
multiaddrs.push(ma)
78117
}
79118

80-
callback(null, multiaddrs)
119+
cb(null, multiaddrs)
81120
}
82121

83122
return listener

0 commit comments

Comments
 (0)