Skip to content

Commit

Permalink
Don't spam refresh if there are <3 nodes to ping
Browse files Browse the repository at this point in the history
  • Loading branch information
HDegroote committed Aug 26, 2024
1 parent 9db1bcc commit ab79af5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/announcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ module.exports = class Announcer {
}

const active = await resolved(pings)
if (active < MIN_ACTIVE) this.refresh() // we lost too many relay nodes, retry all
if (active < Math.min(pings.length, MIN_ACTIVE)) {
this.refresh() // we lost too many relay nodes, retry all
}

if (this.stopped) return

Expand Down
30 changes: 30 additions & 0 deletions test/announces.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const test = require('brittle')
const { swarm, toArray } = require('./helpers')
const DHT = require('../')
const HyperDHT = require('../')

test('server listen and findPeer', async function (t) {
const [a, b] = await swarm(t)
Expand Down Expand Up @@ -158,3 +159,32 @@ test('connect when we relay ourself', async function (t) {
}
}
})

test('announcer background does not over-trigger', async function (t) {
// Note: Not a great test, since it accesses private prop of dht-rpc/io
// Feel free to remove this test if _tid behaviour changes, since it's
// mostly used to document a previous bug

const testnet = await swarm(t, 2) // must be <=3 (less than announcer MIN_ACTIVE) to trigger previous bug
const bootstrap = testnet.bootstrap

const a = new HyperDHT({ bootstrap })

const initTid = a.io._tid
const server = a.createServer()
await server.listen()

// give some time for possible background spam
await new Promise(resolve => setTimeout(resolve, 500))

const requestsSent = initTid > 65536 - 50
? a.io._tid // close enough for this test (ignoring those before wrapping)
: a.io._tid - initTid

t.ok(
requestsSent < 50,
'No background spam of ping requests'
)

await a.destroy()
})

0 comments on commit ab79af5

Please sign in to comment.