diff --git a/cmd/infra/stdht/main.go b/cmd/infra/stdht/main.go index 363bef11e..4c1d7a2d1 100644 --- a/cmd/infra/stdht/main.go +++ b/cmd/infra/stdht/main.go @@ -129,7 +129,7 @@ func run(ctx context.Context, cli CLI) error { } defer dht.Close() - slog.InfoContext(ctx, "Discovery DHT server started", "peer", dht.ID(), "maxConnections", cli.MaxConnections, "maxConnectionsPerSubnet", cli.MaxConnectionsPerSubnet) + slog.InfoContext(ctx, "Discovery DHT server started", "peer", dht.ID(), "maxConnections", cli.MaxConnections, "maxConnectionsPerSubnet", cli.MaxConnectionsPerSubnet, "addrs", dht.Addrs()) var httpServer *http.Server var httpErrors <-chan error diff --git a/internal/dht/dht.go b/internal/dht/dht.go index 8969f7394..4dbd29353 100644 --- a/internal/dht/dht.go +++ b/internal/dht/dht.go @@ -268,6 +268,10 @@ func NewSyncthingDHT(options Options) (*DHT, error) { return nil, err } + if err := d.Bootstrap(context.Background()); err != nil { + return nil, err + } + s := &DHT{ host: h, ipfs: d, @@ -296,6 +300,15 @@ func (s *DHT) Peers() int { return s.ipfs.RoutingTable().Size() } +func (s *DHT) Addrs() []string { + addrs := s.ipfs.FilteredAddrs() + straddrs := make([]string, len(addrs)) + for i, a := range addrs { + straddrs[i] = a.String() + } + return straddrs +} + func (s *DHT) Close() error { return errors.Join(s.ipfs.Close(), s.host.Close()) } diff --git a/lib/discover/peertopeer.go b/lib/discover/peertopeer.go index 7f48dc7a2..571f8f9c9 100644 --- a/lib/discover/peertopeer.go +++ b/lib/discover/peertopeer.go @@ -34,6 +34,7 @@ import ( const ( p2pIdentityKey = "dht-identity" p2pIdentityLifetime = 7 * 24 * time.Hour + p2pServeLoopTick = time.Minute ) var errNotRunning = errors.New("not running") @@ -142,6 +143,9 @@ func (s *PeerToPeerDiscovery) Peers() int { } func (s *PeerToPeerDiscovery) Serve(ctx context.Context) error { + slog.Debug("Starting") + defer slog.Debug("Stopping") + rdht, err := dht.NewSyncthingDHT(s.options) if err != nil { return err @@ -160,6 +164,7 @@ func (s *PeerToPeerDiscovery) Serve(ctx context.Context) error { var next time.Time var addressChangePending bool connectRetryDelay := time.Second + connectFail := 0 for { select { case <-eventSub.C(): @@ -179,6 +184,10 @@ func (s *PeerToPeerDiscovery) Serve(ctx context.Context) error { s.setError(errors.New("no discovery DHT peers")) timer.Reset(connectRetryDelay) connectRetryDelay = min(connectRetryDelay*2, announceErrorRetryInterval) + connectFail++ + if connectFail > 10 { + return nil // restart + } case time.Now().After(next), !slices.Equal(addresses, published): err := s.Announce(ctx) @@ -191,8 +200,9 @@ func (s *PeerToPeerDiscovery) Serve(ctx context.Context) error { next = time.Now().Add(announceErrorRetryInterval) slog.Debug("Failed to announce", "error", err, "next", next) } - timer.Reset(time.Until(next)) + timer.Reset(min(time.Until(next), p2pServeLoopTick)) connectRetryDelay = time.Second + connectFail = 0 default: timer.Reset(time.Until(next))