Skip to content

Commit

Permalink
send keepalives to peers on a network change (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
billiegoose authored Jun 28, 2024
1 parent 994c110 commit 9cd179c
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ const ERR_MISSING_TOPIC = 'Topic is required and must be a 32-byte buffer'
const ERR_DESTROYED = 'Swarm has been destroyed'
const ERR_DUPLICATE = 'Duplicate connection'

const KEEP_ALIVE = b4a.alloc(0)

module.exports = class Hyperswarm extends EventEmitter {
constructor (opts = {}) {
super()
Expand Down Expand Up @@ -303,7 +301,7 @@ module.exports = class Hyperswarm extends EventEmitter {
const keepNew = existingIsOutdated || (expectedInitiator === conn.isInitiator)

if (keepNew === false) {
existing.write(KEEP_ALIVE) // check to see if its still alive actually
existing.sendKeepAlive()
conn.on('error', noop)
conn.destroy(new Error(ERR_DUPLICATE))
return
Expand Down Expand Up @@ -394,6 +392,11 @@ module.exports = class Hyperswarm extends EventEmitter {
}

async _handleNetworkChange () {
// prioritize figuring out if existing connections are dead
for (const conn of this._allConnections) {
conn.sendKeepAlive()
}

const refreshes = []

for (const discovery of this._discovery.values()) {
Expand Down

0 comments on commit 9cd179c

Please sign in to comment.