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

dont consider peers stable if they havent been connected for less than MIN_CONNECTION_TIME #159

Merged
merged 3 commits into from
Nov 1, 2023
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
38 changes: 19 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,25 @@ module.exports = class Hyperswarm extends EventEmitter {
this._clientConnections++
let opened = false

conn.on('open', () => {
opened = true
this._connectDone()
this.connections.add(conn)
conn.removeListener('error', noop)
peerInfo._connected()
peerInfo.client = true
this.emit('connection', conn, peerInfo)
this._flushMaybe(peerInfo)

this.emit('update')
})
conn.on('error', err => {
if (this.relayThrough && shouldForceRelaying(err.code)) {
peerInfo.forceRelaying = true
// Reset the attempts in order to fast connect to relay
peerInfo.attempts = 0
}
})
conn.on('close', () => {
if (!opened) this._connectDone()
this.connections.delete(conn)
Expand All @@ -187,25 +206,6 @@ module.exports = class Hyperswarm extends EventEmitter {

this.emit('update')
})
conn.on('error', err => {
if (this.relayThrough && shouldForceRelaying(err.code)) {
peerInfo.forceRelaying = true
// Reset the attempts in order to fast connect to relay
peerInfo.attempts = 0
}
})
conn.on('open', () => {
opened = true
this._connectDone()
this.connections.add(conn)
conn.removeListener('error', noop)
peerInfo._connected()
peerInfo.client = true
this.emit('connection', conn, peerInfo)
this._flushMaybe(peerInfo)

this.emit('update')
})

this.emit('update')
}
Expand Down
9 changes: 8 additions & 1 deletion lib/peer-info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { EventEmitter } = require('events')
const b4a = require('b4a')

const MIN_CONNECTION_TIME = 15000

const VERY_LOW_PRIORITY = 0
const LOW_PRIORITY = 1
const NORMAL_PRIORITY = 2
Expand All @@ -16,6 +18,7 @@ module.exports = class PeerInfo extends EventEmitter {

this.reconnecting = true
this.proven = false
this.connectedTime = -1
this.banned = false
this.tried = false
this.explicit = false
Expand Down Expand Up @@ -59,10 +62,14 @@ module.exports = class PeerInfo extends EventEmitter {

_connected () {
this.proven = true
this.attempts = 0
this.connectedTime = Date.now()
}

_disconnected () {
if (this.connectedTime > -1) {
if ((Date.now() - this.connectedTime) >= MIN_CONNECTION_TIME) this.attempts = 0 // fast retry
this.connectedTime = -1
}
this.attempts++
}

Expand Down
Loading