add reconnect fallback if no peers are found with variable reconnect time (#511)

Co-authored-by: Tyler Sturos <55340199+tjsturos@users.noreply.github.com>
This commit is contained in:
winged-pegasus 2026-02-18 18:08:26 -09:00 committed by GitHub
parent 87f2872cc8
commit c12bd2f58e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 34 additions and 0 deletions

View File

@ -18,6 +18,7 @@ const (
defaultPingTimeout = 5 * time.Second
defaultPingPeriod = 30 * time.Second
defaultPingAttempts = 3
defaultPeerReconnectInterval = 60 * time.Second
defaultStreamListenMultiaddr = "/ip4/0.0.0.0/tcp/8340"
)
@ -76,6 +77,7 @@ type P2PConfig struct {
ValidateWorkers int `yaml:"validateWorkers"`
SubscriptionQueueSize int `yaml:"subscriptionQueueSize"`
PeerOutboundQueueSize int `yaml:"peerOutboundQueueSize"`
PeerReconnectCheckInterval time.Duration `yaml:"peerReconnectCheckInterval"`
}
// WithDefaults returns a copy of the P2PConfig with any missing fields set to
@ -220,5 +222,8 @@ func (c P2PConfig) WithDefaults() P2PConfig {
if cpy.PeerOutboundQueueSize == 0 {
cpy.PeerOutboundQueueSize = blossomsub.DefaultPeerOutboundQueueSize
}
if cpy.PeerReconnectCheckInterval == 0 {
cpy.PeerReconnectCheckInterval = defaultPeerReconnectInterval
}
return cpy
}

View File

@ -872,16 +872,45 @@ func (b *BlossomSub) background(ctx context.Context) {
refreshScores := time.NewTicker(DecayInterval)
defer refreshScores.Stop()
peerReconnectInterval := b.p2pConfig.PeerReconnectCheckInterval
peerReconnect := time.NewTicker(peerReconnectInterval)
defer peerReconnect.Stop()
for {
select {
case <-refreshScores.C:
b.refreshScores()
case <-peerReconnect.C:
b.checkAndReconnectPeers(ctx)
case <-ctx.Done():
return
}
}
}
func (b *BlossomSub) checkAndReconnectPeers(ctx context.Context) {
peerCount := len(b.h.Network().Peers())
if peerCount > 0 {
return
}
b.logger.Warn(
"no peers connected, attempting to re-bootstrap and discover",
zap.Duration("check_interval", b.p2pConfig.PeerReconnectCheckInterval),
)
if err := b.DiscoverPeers(ctx); err != nil {
b.logger.Error("peer reconnect failed", zap.Error(err))
}
newCount := len(b.h.Network().Peers())
if newCount > 0 {
b.logger.Info("peer reconnect succeeded", zap.Int("peers", newCount))
} else {
b.logger.Warn("peer reconnect: still no peers found, will retry at next interval")
}
}
func (b *BlossomSub) refreshScores() {
b.peerScoreMx.Lock()