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

fix(share/discovery): fixes from Advertise logic audit #2163

Merged
merged 2 commits into from
May 4, 2023
Merged
Changes from 1 commit
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
24 changes: 13 additions & 11 deletions share/availability/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ const (
defaultRetryTimeout = time.Second
)

// waitF calculates time to restart announcing.
var waitF = func(ttl time.Duration) time.Duration {
return 7 * ttl / 8
}

type Parameters struct {
// PeersLimit defines the soft limit of FNs to connect to via discovery.
// Set 0 to disable.
Expand Down Expand Up @@ -67,8 +62,9 @@ func (p Parameters) withDefaults() Parameters {

func DefaultParameters() Parameters {
return Parameters{
PeersLimit: 5,
AdvertiseInterval: time.Hour * 8,
PeersLimit: 5,
// based on https://github.com/libp2p/go-libp2p-kad-dht/pull/793
AdvertiseInterval: time.Hour * 22,
}
}

Expand Down Expand Up @@ -151,32 +147,38 @@ func (d *Discovery) Peers(ctx context.Context) ([]peer.ID, error) {
// TODO: Start advertising only after the reachability is confirmed by AutoNAT
func (d *Discovery) Advertise(ctx context.Context) {
if d.params.AdvertiseInterval == -1 {
log.Warn("AdvertiseInterval is set to -1. Skipping advertising...")
return
}

timer := time.NewTimer(d.params.AdvertiseInterval)
defer timer.Stop()
for {
ttl, err := d.disc.Advertise(ctx, rendezvousPoint)
_, err := d.disc.Advertise(ctx, rendezvousPoint)
if err != nil {
log.Debugf("Error advertising %s: %s", rendezvousPoint, err.Error())
if ctx.Err() != nil {
return
}
log.Debugf("error advertising %s: %s", rendezvousPoint, err.Error())
Wondertan marked this conversation as resolved.
Show resolved Hide resolved

errTimer := time.NewTimer(time.Minute)
select {
case <-timer.C:
case <-errTimer.C:
errTimer.Stop()
if !timer.Stop() {
<-timer.C
}
timer.Reset(d.params.AdvertiseInterval)
Wondertan marked this conversation as resolved.
Show resolved Hide resolved
continue
case <-ctx.Done():
errTimer.Stop()
return
}
}

log.Debugf("advertised")
select {
case <-timer.C:
timer.Reset(waitF(ttl))
case <-ctx.Done():
return
}
Expand Down
Loading