Skip to content

Commit

Permalink
improve reconnection tie-breaking heuristic (#178)
Browse files Browse the repository at this point in the history
* use timestamp to tie-break connections

* don't use userData

* mafintosh's newest idea

* oh

* cleanup

* whitespace
  • Loading branch information
billiegoose authored Jun 28, 2024
1 parent 0fb70ee commit 88362e5
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,13 @@ module.exports = class Hyperswarm extends EventEmitter {
const existing = this._allConnections.get(conn.remotePublicKey)

if (existing) {
// If both connections are from the same peer,
// - pick the new one if the existing stream is already established (has sent and received bytes),
// because the other client must have lost that connection and be reconnecting
// - otherwise, pick the one thats expected to initiate in a tie break
const existingIsOutdated = existing.rawBytesRead > 0 && existing.rawBytesWritten > 0
const expectedInitiator = b4a.compare(conn.publicKey, conn.remotePublicKey) > 0
// if both connections are from the same peer, pick the one thats expected to initiate in a tie break
const keepNew = expectedInitiator === conn.isInitiator
const keepNew = existingIsOutdated || (expectedInitiator === conn.isInitiator)

if (keepNew === false) {
existing.write(KEEP_ALIVE) // check to see if its still alive actually
Expand Down

0 comments on commit 88362e5

Please sign in to comment.