From ac0f0e31084c23e1a1352092211495632fdd9193 Mon Sep 17 00:00:00 2001 From: Hlib Kanunnikov Date: Tue, 16 May 2023 12:57:35 +0200 Subject: [PATCH] feat(share/discovery): Discard method (#2207) 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 --- share/availability/discovery/discovery.go | 42 ++++++++++++++--------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/share/availability/discovery/discovery.go b/share/availability/discovery/discovery.go index 727022fead..e8e5594e31 100644 --- a/share/availability/discovery/discovery.go +++ b/share/availability/discovery/discovery.go @@ -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) { @@ -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) } } }