Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - Make routing discovery more configurable and less spammy by default #5494

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
60 changes: 44 additions & 16 deletions p2p/dhtdiscovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
discoveryHighPeersDelay = 10 * time.Second
protocolPrefix = "/spacekad"
ProtocolID = protocolPrefix + "/kad/1.0.0"
findPeersRetryDelay = time.Second
advertiseRetryInterval = time.Second
)

type Opt func(*Discovery)
Expand Down Expand Up @@ -124,9 +122,27 @@
}
}

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
}
}

Expand All @@ -138,15 +154,17 @@

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)
Expand Down Expand Up @@ -183,7 +201,10 @@
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) {
Expand Down Expand Up @@ -376,6 +397,13 @@
}

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() {
Expand All @@ -384,7 +412,7 @@
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

Check warning on line 415 in p2p/dhtdiscovery/discovery.go

View check run for this annotation

Codecov / codecov/patch

p2p/dhtdiscovery/discovery.go#L415

Added line #L415 was not covered by tests
}
} else {
ttl = d.advertiseInterval
Expand Down Expand Up @@ -465,7 +493,7 @@
select {
case <-ctx.Done():
return nil
case <-time.After(findPeersRetryDelay):
case <-time.After(d.findPeersRetryDelay):

Check warning on line 496 in p2p/dhtdiscovery/discovery.go

View check run for this annotation

Codecov / codecov/patch

p2p/dhtdiscovery/discovery.go#L496

Added line #L496 was not covered by tests
}
peerCh = nil
continue
Expand All @@ -481,7 +509,7 @@
select {
case <-ctx.Done():
return nil
case <-time.After(findPeersRetryDelay):
case <-time.After(d.findPeersRetryDelay):
}
continue
}
Expand Down
5 changes: 5 additions & 0 deletions p2p/dhtdiscovery/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
92 changes: 52 additions & 40 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}

Expand All @@ -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 {
Expand Down
5 changes: 4 additions & 1 deletion p2p/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Loading