Skip to content

Commit

Permalink
feat(share/p2p/discovery): Warning for discovery loop if haven't foun…
Browse files Browse the repository at this point in the history
…d wanted peers in 5 minutes (celestiaorg#2573)

(cherry picked from commit 4b96022)
  • Loading branch information
nodersteam authored and walldiss committed Sep 22, 2023
1 parent c3d4219 commit 7008d6a
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions share/p2p/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ const (

// retryTimeout defines time interval between discovery and advertise attempts.
retryTimeout = time.Second

// logInterval defines the time interval at which a warning message will be logged
// if the desired number of nodes is not detected.
logInterval = 5 * time.Minute
)

// discoveryRetryTimeout defines time interval between discovery attempts, needed for tests
Expand Down Expand Up @@ -195,26 +199,33 @@ func (d *Discovery) Advertise(ctx context.Context) {
}

// discoveryLoop ensures we always have '~peerLimit' connected peers.
// It starts peer discovery per request and restarts the process until the soft limit reached.
// It initiates peer discovery upon request and restarts the process until the soft limit is reached.
func (d *Discovery) discoveryLoop(ctx context.Context) {
t := time.NewTicker(discoveryRetryTimeout)
defer t.Stop()

warnTicker := time.NewTicker(logInterval)
defer warnTicker.Stop()

for {
// drain all previous ticks from channel
// drain all previous ticks from the channel
drainChannel(t.C)
select {
case <-t.C:
found := d.discover(ctx)
if !found {
// rerun discovery if amount of peers didn't reach the limit
if !d.discover(ctx) {
// rerun discovery if the number of peers hasn't reached the limit
continue
}
case <-ctx.Done():
return
}

select {
case <-d.triggerDisc:
case <-warnTicker.C:
if d.set.Size() < d.set.Limit() {
log.Warnf(
"Potentially degraded connectivity, unable to discover the desired amount of full node peers in %v. "+
"Number of peers discovered: %d. Required: %d.",
logInterval, d.set.Size(), d.set.Limit(),
)
}
// Do not break the loop; just continue
continue
case <-ctx.Done():
return
}
Expand Down

0 comments on commit 7008d6a

Please sign in to comment.