fix_: code review

This commit is contained in:
Richard Ramos 2024-09-25 18:14:20 -04:00
parent 6b0668371d
commit 4d0163dead
No known key found for this signature in database
GPG Key ID: 1CE87DB518195760
1 changed files with 42 additions and 30 deletions

View File

@ -399,7 +399,11 @@ func (w *Waku) getDiscV5BootstrapNodes(ctx context.Context, addresses []string,
go func(addr string) { go func(addr string) {
defer wg.Done() defer wg.Done()
if err := w.dnsDiscover(ctx, addr, retrieveENR, useOnlyDnsDiscCache); err != nil { if err := w.dnsDiscover(ctx, addr, retrieveENR, useOnlyDnsDiscCache); err != nil {
go w.retryDnsDiscoveryWithBackoff(ctx, addr, w.dnsDiscAsyncRetrievedSignal) wg.Add(1)
go func() {
defer wg.Done()
w.retryDnsDiscoveryWithBackoff(ctx, addr, w.dnsDiscAsyncRetrievedSignal)
}()
} }
}(addrString) }(addrString)
} else { } else {
@ -482,6 +486,10 @@ func (w *Waku) retryDnsDiscoveryWithBackoff(ctx context.Context, addr string, su
retries++ retries++
backoff := time.Second * time.Duration(math.Exp2(float64(retries))) backoff := time.Second * time.Duration(math.Exp2(float64(retries)))
if backoff > time.Minute {
backoff = time.Minute
}
t := time.NewTimer(backoff) t := time.NewTimer(backoff)
select { select {
case <-w.ctx.Done(): case <-w.ctx.Done():
@ -1761,49 +1769,53 @@ func (w *Waku) seedBootnodesForDiscV5() {
for { for {
select { select {
case <-w.dnsDiscAsyncRetrievedSignal: case <-w.dnsDiscAsyncRetrievedSignal:
if canQuery() { if !canQuery() {
err := w.restartDiscV5(true) continue
if err != nil {
w.logger.Warn("failed to restart discv5", zap.Error(err))
}
retries = 0
lastTry = now()
} }
err := w.restartDiscV5(true)
if err != nil {
w.logger.Warn("failed to restart discv5", zap.Error(err))
}
retries = 0
lastTry = now()
case <-ticker.C: case <-ticker.C:
if w.seededBootnodesForDiscV5 && len(w.node.Host().Network().Peers()) > 3 { if w.seededBootnodesForDiscV5 && len(w.node.Host().Network().Peers()) > 3 {
w.logger.Debug("not querying bootnodes", zap.Bool("seeded", w.seededBootnodesForDiscV5), zap.Int("peer-count", len(w.node.Host().Network().Peers()))) w.logger.Debug("not querying bootnodes", zap.Bool("seeded", w.seededBootnodesForDiscV5), zap.Int("peer-count", len(w.node.Host().Network().Peers())))
continue continue
} }
if canQuery() {
w.logger.Info("querying bootnodes to restore connectivity", zap.Int("peer-count", len(w.node.Host().Network().Peers())))
err := w.restartDiscV5(false)
if err != nil {
w.logger.Warn("failed to restart discv5", zap.Error(err))
}
lastTry = now() if !canQuery() {
retries++
// We reset the retries after a while and restart
if retries > bootnodesMaxRetries {
retries = 0
}
} else {
w.logger.Info("can't query bootnodes", zap.Int("peer-count", len(w.node.Host().Network().Peers())), zap.Int64("lastTry", lastTry), zap.Int64("now", now()), zap.Int64("backoff", bootnodesQueryBackoffMs*int64(math.Exp2(float64(retries)))), zap.Int("retries", retries)) w.logger.Info("can't query bootnodes", zap.Int("peer-count", len(w.node.Host().Network().Peers())), zap.Int64("lastTry", lastTry), zap.Int64("now", now()), zap.Int64("backoff", bootnodesQueryBackoffMs*int64(math.Exp2(float64(retries)))), zap.Int("retries", retries))
continue
} }
w.logger.Info("querying bootnodes to restore connectivity", zap.Int("peer-count", len(w.node.Host().Network().Peers())))
err := w.restartDiscV5(false)
if err != nil {
w.logger.Warn("failed to restart discv5", zap.Error(err))
}
lastTry = now()
retries++
// We reset the retries after a while and restart
if retries > bootnodesMaxRetries {
retries = 0
}
// If we go online, trigger immediately // If we go online, trigger immediately
case <-w.goingOnline: case <-w.goingOnline:
if canQuery() { if !canQuery() {
err := w.restartDiscV5(false) continue
if err != nil {
w.logger.Warn("failed to restart discv5", zap.Error(err))
}
retries = 0
lastTry = now()
} }
err := w.restartDiscV5(false)
if err != nil {
w.logger.Warn("failed to restart discv5", zap.Error(err))
}
retries = 0
lastTry = now()
case <-w.ctx.Done(): case <-w.ctx.Done():
w.logger.Debug("bootnode seeding stopped") w.logger.Debug("bootnode seeding stopped")
return return