mirror of
https://github.com/syncthing/syncthing.git
synced 2026-11-21 09:31:22 -08:00
3 files changed
+25
-2
No files matched your search
@@ -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
|
||||
|
||||
@@ -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())
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in new issue
Block a user