Skip to content

Commit

Permalink
fix(share/discovery): fixes from Advertise logic audit (#2163)
Browse files Browse the repository at this point in the history
* Use only TTL provided by us.
* Don't wait 8hs upon error
* Increase the advertise timeout
  • Loading branch information
Wondertan committed May 4, 2023
1 parent e8ae799 commit 0100141
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 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,41 @@ 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.Warn("error advertising %s: %s", rendezvousPoint, err.Error())

errTimer := time.NewTimer(time.Minute)
select {
case <-timer.C:
timer.Reset(d.params.AdvertiseInterval)
case <-errTimer.C:
errTimer.Stop()
if !timer.Stop() {
<-timer.C
}
continue
case <-ctx.Done():
errTimer.Stop()
return
}
}

log.Debugf("advertised")
if !timer.Stop() {
<-timer.C
}
timer.Reset(d.params.AdvertiseInterval)
select {
case <-timer.C:
timer.Reset(waitF(ttl))
case <-ctx.Done():
return
}
Expand Down

0 comments on commit 0100141

Please sign in to comment.