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

improve reconnection tie-breaking heuristic #178

Merged
merged 6 commits into from
Jun 28, 2024
Merged
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
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
mafintosh marked this conversation as resolved.
Show resolved Hide resolved
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
Loading