Skip to content

Commit

Permalink
feat(share/discovery): Discard method (#2207)
Browse files Browse the repository at this point in the history
Fixes #2204

Regarding the case where we exhausted all the peers on the network and backed them all off. It can happen on the smaller network, and in the worst case, the node will wait 10 minutes(the default backoff time), which is acceptable considering peers from shrexsub #2105
  • Loading branch information
Wondertan committed May 16, 2023
1 parent 3b62c6b commit ac0f0e3
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions share/availability/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,30 @@ func (d *Discovery) Peers(ctx context.Context) ([]peer.ID, error) {
return d.set.Peers(ctx)
}

// Discard removes the peer from the peer set and rediscovers more if soft peer limit is not
// reached. Reports whether peer was removed with bool.
func (d *Discovery) Discard(id peer.ID) bool {
if !d.set.Contains(id) {
return false
}

d.host.ConnManager().Unprotect(id, rendezvousPoint)
d.connector.Backoff(id)
d.set.Remove(id)
d.onUpdatedPeers(id, false)
log.Debugw("removed peer from the peer set", "peer", id)

if d.set.Size() < d.set.Limit() {
// trigger discovery
select {
case d.triggerDisc <- struct{}{}:
default:
}
}

return true
}

// Advertise is a utility function that persistently advertises a service through an Advertiser.
// TODO: Start advertising only after the reachability is confirmed by AutoNAT
func (d *Discovery) Advertise(ctx context.Context) {
Expand Down Expand Up @@ -210,22 +234,8 @@ func (d *Discovery) disconnectsLoop(ctx context.Context, sub event.Subscription)
return
}

evnt := e.(event.EvtPeerConnectednessChanged)
if evnt.Connectedness == network.NotConnected && d.set.Contains(evnt.Peer) {
d.host.ConnManager().Unprotect(evnt.Peer, rendezvousPoint)
d.connector.Backoff(evnt.Peer)
d.set.Remove(evnt.Peer)
d.onUpdatedPeers(evnt.Peer, false)
log.Debugw("removed peer from the peer set",
"peer", evnt.Peer, "status", evnt.Connectedness.String())

if d.set.Size() < d.set.Limit() {
// trigger discovery
select {
case d.triggerDisc <- struct{}{}:
default:
}
}
if evnt := e.(event.EvtPeerConnectednessChanged); evnt.Connectedness == network.NotConnected {
d.Discard(evnt.Peer)
}
}
}
Expand Down

0 comments on commit ac0f0e3

Please sign in to comment.