Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't spam refresh if there are <3 nodes to ping #188

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
})
Loading