Skip to content

Commit

Permalink
attempt to fix dedupe & catch errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed Oct 20, 2023
1 parent 77920cf commit 8344aa1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/discovery/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ export class MdnsDiscovery extends TypedEmitter {
*/
#handleServiceUp({ address, port, name }) {
this.#log('serviceUp', name.slice(0, 7), address, port)
if (this.#noiseConnections.has(name)) {
this.#log(`Already connected to ${name.slice(0, 7)}`)
return
}
// if (this.#noiseConnections.has(name)) {
// this.#log(`Already connected to ${name.slice(0, 7)}`)
// return
// }
const socket = net.connect(port, address)
socket.on('error', this.#handleSocketError)
socket.once('connect', () => {
Expand Down
36 changes: 36 additions & 0 deletions tests/discovery/mdns.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,28 @@ test('mdns - discovery and sharing of data', (t) => {
const str = 'hi'

mdnsDiscovery1.on('connection', (stream) => {
stream.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
stream.write(str)
})

mdnsDiscovery2.on('connection', (stream) => {
stream.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
stream.on('data', (d) => {
t.is(d.toString(), str, 'expected data written')
Promise.all([
Expand All @@ -53,13 +71,31 @@ test('deduplicate incoming connections', async (t) => {
await discovery.start()

discovery.on('connection', (conn) => {
conn.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
localConnections.add(conn)
conn.on('close', () => localConnections.delete(conn))
})

const addrInfo = discovery.address()
for (let i = 0; i < 20; i++) {
noiseConnect(addrInfo, remoteKp).then((conn) => {
conn.on('error', (e) => {
// We expected connections to be closed when duplicates happen. On the
// closing side the error will be ERR_DUPLICATE, but on the other side
// the error will be an ECONNRESET - the error is not sent over the
// connection
const expectedError =
e.message === ERR_DUPLICATE || e.code === 'ECONNRESET'
t.ok(expectedError, 'connection closed with expected error')
})
conn.on('connect', () => remoteConnections.add(conn))
conn.on('close', () => remoteConnections.delete(conn))
})
Expand Down

0 comments on commit 8344aa1

Please sign in to comment.