diff --git a/CHANGELOG.md b/CHANGELOG.md index c19bc8fcc1..45955c6f08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -96,6 +96,8 @@ configuration is as follows: * [#5479](https://github.com/spacemeshos/go-spacemesh/pull/5479) p2p: make AutoNAT service limits configurable. * [#5489](https://github.com/spacemeshos/go-spacemesh/pull/5489) Fix problem in POST proving where too many files were opened at the same time. +* [#5494](https://github.com/spacemeshos/go-spacemesh/pull/5494) + Make routing discovery more configurable and less spammy by default. ## Release v1.3.5 diff --git a/p2p/dhtdiscovery/discovery.go b/p2p/dhtdiscovery/discovery.go index 96c8e59fe6..e945291143 100644 --- a/p2p/dhtdiscovery/discovery.go +++ b/p2p/dhtdiscovery/discovery.go @@ -28,8 +28,6 @@ const ( discoveryHighPeersDelay = 10 * time.Second protocolPrefix = "/spacekad" ProtocolID = protocolPrefix + "/kad/1.0.0" - findPeersRetryDelay = time.Second - advertiseRetryInterval = time.Second ) type Opt func(*Discovery) @@ -124,9 +122,27 @@ func AdvertiseForPeerDiscovery() Opt { } } -func WithAdvertiseInterval(aint time.Duration) Opt { +func WithAdvertiseRetryDelay(value time.Duration) Opt { return func(d *Discovery) { - d.advertiseInterval = aint + d.advertiseRetryDelay = value + } +} + +func WithAdvertiseDelay(value time.Duration) Opt { + return func(d *Discovery) { + d.advertiseDelay = value + } +} + +func WithAdvertiseInterval(value time.Duration) Opt { + return func(d *Discovery) { + d.advertiseInterval = value + } +} + +func WithFindPeersRetryDelay(value time.Duration) Opt { + return func(d *Discovery) { + d.findPeersRetryDelay = value } } @@ -138,15 +154,17 @@ type DiscoveryHost interface { func New(h DiscoveryHost, opts ...Opt) (*Discovery, error) { d := Discovery{ - public: true, - logger: zap.NewNop(), - h: h, - period: 10 * time.Second, - timeout: 30 * time.Second, - bootstrapDuration: 30 * time.Second, - minPeers: 20, - highPeers: 40, - advertiseInterval: time.Minute, + public: true, + logger: zap.NewNop(), + h: h, + period: 10 * time.Second, + timeout: 30 * time.Second, + bootstrapDuration: 30 * time.Second, + minPeers: 20, + highPeers: 40, + advertiseInterval: time.Minute, + advertiseRetryDelay: time.Minute, + findPeersRetryDelay: time.Minute, } for _, opt := range opts { opt(&d) @@ -183,7 +201,10 @@ type Discovery struct { bootstrapDuration time.Duration minPeers, highPeers int backup, bootnodes []peer.AddrInfo + advertiseDelay time.Duration advertiseInterval time.Duration + advertiseRetryDelay time.Duration + findPeersRetryDelay time.Duration } func (d *Discovery) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) { @@ -376,6 +397,13 @@ func (d *Discovery) peerHasTag(p peer.ID) bool { } func (d *Discovery) advertiseNS(ctx context.Context, ns string, active func() bool) error { + if d.advertiseDelay != 0 { + select { + case <-ctx.Done(): + return nil + case <-time.After(d.advertiseDelay): + } + } for { var ttl time.Duration if active == nil || active() { @@ -384,7 +412,7 @@ func (d *Discovery) advertiseNS(ctx context.Context, ns string, active func() bo ttl, err = d.disc.Advertise(ctx, ns, p2pdisc.TTL(d.advertiseInterval)) if err != nil { d.logger.Error("failed to re-advertise for discovery", zap.String("ns", ns), zap.Error(err)) - ttl = advertiseRetryInterval + ttl = d.advertiseRetryDelay } } else { ttl = d.advertiseInterval @@ -465,7 +493,7 @@ func (d *Discovery) findPeersContinuously(ctx context.Context, ns string) <-chan select { case <-ctx.Done(): return nil - case <-time.After(findPeersRetryDelay): + case <-time.After(d.findPeersRetryDelay): } peerCh = nil continue @@ -481,7 +509,7 @@ func (d *Discovery) findPeersContinuously(ctx context.Context, ns string) <-chan select { case <-ctx.Done(): return nil - case <-time.After(findPeersRetryDelay): + case <-time.After(d.findPeersRetryDelay): } continue } diff --git a/p2p/dhtdiscovery/discovery_test.go b/p2p/dhtdiscovery/discovery_test.go index 38c3eb07e5..8dfbd3af38 100644 --- a/p2p/dhtdiscovery/discovery_test.go +++ b/p2p/dhtdiscovery/discovery_test.go @@ -58,6 +58,7 @@ func TestSanity(t *testing.T) { Private(), WithLogger(logger), WithMode(dht.ModeServer), + WithFindPeersRetryDelay(1*time.Second), ) require.NoError(t, err) bootdisc.Start() @@ -111,6 +112,10 @@ func TestSanity(t *testing.T) { EnableRoutingDiscovery(), AdvertiseForPeerDiscovery(), WithMode(nodeOpts[i].dhtMode), + WithAdvertiseDelay(10 * time.Millisecond), + WithAdvertiseInterval(1 * time.Minute), + WithAdvertiseRetryDelay(1 * time.Second), + WithFindPeersRetryDelay(1 * time.Second), } if nodeOpts[i].lookForRelays { relayCh := make(chan peer.AddrInfo) diff --git a/p2p/host.go b/p2p/host.go index a3a156a15b..a0194f1058 100644 --- a/p2p/host.go +++ b/p2p/host.go @@ -89,13 +89,18 @@ func DefaultConfig() Config { PingInterval: time.Second, EnableTCPTransport: true, EnableQUICTransport: false, - AdvertiseInterval: time.Hour, AutoNATServer: AutoNATServer{ // Defaults taken from libp2p GlobalMax: 30, PeerMax: 3, ResetPeriod: time.Minute, }, + DiscoveryTimings: DiscoveryTimings{ + AdvertiseDelay: time.Hour, + AdvertiseInterval: time.Hour, + AdvertiseRetryDelay: time.Minute, + FindPeersRetryDelay: time.Minute, + }, } } @@ -112,45 +117,52 @@ type Config struct { MaxMessageSize int `mapstructure:"maxmessagesize"` // see https://lwn.net/Articles/542629/ for reuseport explanation - DisableReusePort bool `mapstructure:"disable-reuseport"` - DisableNatPort bool `mapstructure:"disable-natport"` - DisableConnectionManager bool `mapstructure:"disable-connection-manager"` - DisableResourceManager bool `mapstructure:"disable-resource-manager"` - DisableDHT bool `mapstructure:"disable-dht"` - Flood bool `mapstructure:"flood"` - Listen AddressList `mapstructure:"listen"` - Bootnodes []string `mapstructure:"bootnodes"` - Direct []string `mapstructure:"direct"` - MinPeers int `mapstructure:"min-peers"` - LowPeers int `mapstructure:"low-peers"` - HighPeers int `mapstructure:"high-peers"` - InboundFraction float64 `mapstructure:"inbound-fraction"` - OutboundFraction float64 `mapstructure:"outbound-fraction"` - AutoscalePeers bool `mapstructure:"autoscale-peers"` - AdvertiseAddress AddressList `mapstructure:"advertise-address"` - AcceptQueue int `mapstructure:"p2p-accept-queue"` - Metrics bool `mapstructure:"p2p-metrics"` - Bootnode bool `mapstructure:"p2p-bootnode"` - ForceReachability string `mapstructure:"p2p-reachability"` - ForceDHTServer bool `mapstructure:"force-dht-server"` - EnableHolepunching bool `mapstructure:"p2p-holepunching"` - PrivateNetwork bool `mapstructure:"p2p-private-network"` - RelayServer RelayServer `mapstructure:"relay-server"` - IP4Blocklist []string `mapstructure:"ip4-blocklist"` - IP6Blocklist []string `mapstructure:"ip6-blocklist"` - GossipQueueSize int `mapstructure:"gossip-queue-size"` - GossipValidationThrottle int `mapstructure:"gossip-validation-throttle"` - GossipAtxValidationThrottle int `mapstructure:"gossip-atx-validation-throttle"` - PingPeers []string `mapstructure:"ping-peers"` - PingInterval time.Duration `mapstructure:"ping-interval"` - Relay bool `mapstructure:"relay"` - StaticRelays []string `mapstructure:"static-relays"` - EnableTCPTransport bool `mapstructure:"enable-tcp-transport"` - EnableQUICTransport bool `mapstructure:"enable-quic-transport"` - EnableRoutingDiscovery bool `mapstructure:"enable-routing-discovery"` - RoutingDiscoveryAdvertise bool `mapstructure:"routing-discovery-advertise"` - AdvertiseInterval time.Duration `mapstructure:"advertise-interval"` - AutoNATServer AutoNATServer `mapstructure:"auto-nat-server"` + DisableReusePort bool `mapstructure:"disable-reuseport"` + DisableNatPort bool `mapstructure:"disable-natport"` + DisableConnectionManager bool `mapstructure:"disable-connection-manager"` + DisableResourceManager bool `mapstructure:"disable-resource-manager"` + DisableDHT bool `mapstructure:"disable-dht"` + Flood bool `mapstructure:"flood"` + Listen AddressList `mapstructure:"listen"` + Bootnodes []string `mapstructure:"bootnodes"` + Direct []string `mapstructure:"direct"` + MinPeers int `mapstructure:"min-peers"` + LowPeers int `mapstructure:"low-peers"` + HighPeers int `mapstructure:"high-peers"` + InboundFraction float64 `mapstructure:"inbound-fraction"` + OutboundFraction float64 `mapstructure:"outbound-fraction"` + AutoscalePeers bool `mapstructure:"autoscale-peers"` + AdvertiseAddress AddressList `mapstructure:"advertise-address"` + AcceptQueue int `mapstructure:"p2p-accept-queue"` + Metrics bool `mapstructure:"p2p-metrics"` + Bootnode bool `mapstructure:"p2p-bootnode"` + ForceReachability string `mapstructure:"p2p-reachability"` + ForceDHTServer bool `mapstructure:"force-dht-server"` + EnableHolepunching bool `mapstructure:"p2p-holepunching"` + PrivateNetwork bool `mapstructure:"p2p-private-network"` + RelayServer RelayServer `mapstructure:"relay-server"` + IP4Blocklist []string `mapstructure:"ip4-blocklist"` + IP6Blocklist []string `mapstructure:"ip6-blocklist"` + GossipQueueSize int `mapstructure:"gossip-queue-size"` + GossipValidationThrottle int `mapstructure:"gossip-validation-throttle"` + GossipAtxValidationThrottle int `mapstructure:"gossip-atx-validation-throttle"` + PingPeers []string `mapstructure:"ping-peers"` + PingInterval time.Duration `mapstructure:"ping-interval"` + Relay bool `mapstructure:"relay"` + StaticRelays []string `mapstructure:"static-relays"` + EnableTCPTransport bool `mapstructure:"enable-tcp-transport"` + EnableQUICTransport bool `mapstructure:"enable-quic-transport"` + EnableRoutingDiscovery bool `mapstructure:"enable-routing-discovery"` + RoutingDiscoveryAdvertise bool `mapstructure:"routing-discovery-advertise"` + DiscoveryTimings DiscoveryTimings `mapstructure:"discovery-timings"` + AutoNATServer AutoNATServer `mapstructure:"auto-nat-server"` +} + +type DiscoveryTimings struct { + AdvertiseDelay time.Duration `mapstructure:"advertise-delay"` + AdvertiseInterval time.Duration `mapstructure:"advertise-interval"` + AdvertiseRetryDelay time.Duration `mapstructure:"advertise-retry-delay"` + FindPeersRetryDelay time.Duration `mapstructure:"find-peers-retry-delay"` } type AutoNATServer struct { diff --git a/p2p/upgrade.go b/p2p/upgrade.go index 3ddd029c22..49f04dfd99 100644 --- a/p2p/upgrade.go +++ b/p2p/upgrade.go @@ -157,7 +157,10 @@ func Upgrade(h host.Host, opts ...Opt) (*Host, error) { discovery.WithDir(cfg.DataDir), discovery.WithBootnodes(bootnodes), discovery.WithLogger(fh.logger.Zap()), - discovery.WithAdvertiseInterval(fh.cfg.AdvertiseInterval), + discovery.WithAdvertiseDelay(fh.cfg.DiscoveryTimings.AdvertiseDelay), + discovery.WithAdvertiseInterval(fh.cfg.DiscoveryTimings.AdvertiseInterval), + discovery.WithAdvertiseRetryDelay(fh.cfg.DiscoveryTimings.AdvertiseInterval), + discovery.WithFindPeersRetryDelay(fh.cfg.DiscoveryTimings.FindPeersRetryDelay), } if cfg.PrivateNetwork { dopts = append(dopts, discovery.Private())