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

feat(share/discovery): Discard method #2207

Merged
merged 3 commits into from
May 16, 2023
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
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:
}
}
Wondertan marked this conversation as resolved.
Show resolved Hide resolved

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
Loading