Skip to content

Commit

Permalink
fix: check backoff when join (#444)
Browse files Browse the repository at this point in the history
* feat: add some slack time to the backoff expiration

* fix: check backoff before sending GRAFT

* chore: address PR comments
  • Loading branch information
twoeths authored Jun 29, 2023
1 parent ec570ca commit fd8c61b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,6 @@ export const ACCEPT_FROM_WHITELIST_DURATION_MS = 1000
* The default MeshMessageDeliveriesWindow to be used in metrics.
*/
export const DEFAULT_METRIC_MESH_MESSAGE_DELIVERIES_WINDOWS = 1000

/** Wait for 1 more heartbeats before clearing a backoff */
export const BACKOFF_SLACK = 1
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { SimpleTimeCache } from './utils/time-cache.js'
import {
ACCEPT_FROM_WHITELIST_DURATION_MS,
ACCEPT_FROM_WHITELIST_MAX_MESSAGES,
ACCEPT_FROM_WHITELIST_THRESHOLD_SCORE
ACCEPT_FROM_WHITELIST_THRESHOLD_SCORE,
BACKOFF_SLACK
} from './constants.js'
import {
ChurnReason,
Expand Down Expand Up @@ -1648,7 +1649,8 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
const now = Date.now()
this.backoff.forEach((backoff, topic) => {
backoff.forEach((expire, id) => {
if (expire < now) {
// add some slack time to the expiration, see https://github.com/libp2p/specs/pull/289
if (expire + BACKOFF_SLACK * this.opts.heartbeatInterval < now) {
backoff.delete(id)
}
})
Expand Down Expand Up @@ -1793,6 +1795,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
this.metrics?.onJoin(topic)

const toAdd = new Set<PeerIdStr>()
const backoff = this.backoff.get(topic)

// check if we have mesh_n peers in fanout[topic] and add them to the mesh if we do,
// removing the fanout entry.
Expand All @@ -1804,8 +1807,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G

// remove explicit peers, peers with negative scores, and backoffed peers
fanoutPeers.forEach((id) => {
// TODO:rust-libp2p checks `self.backoffs.is_backoff_with_slack()`
if (!this.direct.has(id) && this.score.score(id) >= 0) {
if (!this.direct.has(id) && this.score.score(id) >= 0 && (!backoff || !backoff.has(id))) {
toAdd.add(id)
}
})
Expand All @@ -1821,7 +1823,7 @@ export class GossipSub extends EventEmitter<GossipsubEvents> implements PubSub<G
this.opts.D,
(id: PeerIdStr): boolean =>
// filter direct peers and peers with negative score
!toAdd.has(id) && !this.direct.has(id) && this.score.score(id) >= 0
!toAdd.has(id) && !this.direct.has(id) && this.score.score(id) >= 0 && (!backoff || !backoff.has(id))
)

newPeers.forEach((peer) => {
Expand Down

0 comments on commit fd8c61b

Please sign in to comment.