Skip to content

Commit 42b974a

Browse files
authored
Make sure bootstrap connections are maintained in DHT off mode, needed for bootstrap server restarts resilience (#20)
1 parent 25a84d0 commit 42b974a

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

client.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ func NewClient(config Config) (Client, error) {
171171
clientLogger.Infof("Topic peer exchange enabled - will attempt direct connections to discovered peers")
172172
// Periodically check for new peer addresses learned via PX
173173
go c.attemptDirectConnectionsToTopicPeers(ctx)
174+
// Maintain bootstrap peer connections in DHT off mode
175+
go c.maintainBootstrapConnections(ctx, bootstrapPeers)
174176
}
175177

176178
return c, nil
@@ -782,6 +784,42 @@ func (c *client) shouldLogConnectionError(err error) bool {
782784
return true
783785
}
784786

787+
// maintainBootstrapConnections periodically checks bootstrap peer connections
788+
// and reconnects if they become disconnected. This is critical in DHT off mode
789+
// where bootstrap peers are the only way to stay connected to the network.
790+
func (c *client) maintainBootstrapConnections(ctx context.Context, bootstrapPeers []peer.AddrInfo) {
791+
if len(bootstrapPeers) == 0 {
792+
c.logger.Debugf("No bootstrap peers to maintain")
793+
return
794+
}
795+
796+
c.logger.Infof("Starting bootstrap peer maintenance for %d peers", len(bootstrapPeers))
797+
ticker := time.NewTicker(30 * time.Second)
798+
defer ticker.Stop()
799+
800+
for {
801+
select {
802+
case <-ctx.Done():
803+
c.logger.Infof("Bootstrap peer maintenance stopping")
804+
return
805+
case <-ticker.C:
806+
for _, peerInfo := range bootstrapPeers {
807+
// Check if we're still connected to this bootstrap peer
808+
if c.host.Network().Connectedness(peerInfo.ID) != network.Connected {
809+
c.logger.Infof("Bootstrap peer %s disconnected, attempting reconnection", peerInfo.ID.String()[:16])
810+
go func(pi peer.AddrInfo) {
811+
if err := c.host.Connect(ctx, pi); err != nil {
812+
c.logger.Warnf("Failed to reconnect to bootstrap peer %s: %v", pi.ID.String()[:16], err)
813+
} else {
814+
c.logger.Infof("Successfully reconnected to bootstrap peer %s", pi.ID.String()[:16])
815+
}
816+
}(peerInfo)
817+
}
818+
}
819+
}
820+
}
821+
}
822+
785823
// attemptDirectConnectionsToTopicPeers periodically queries connected peers for
786824
// addresses of topic peers we're not directly connected to.
787825
// This implements a simple peer address exchange by asking bootstrap servers for peer info.

0 commit comments

Comments
 (0)