From 31eb77d4469848cfc170673776a22567ee2435a7 Mon Sep 17 00:00:00 2001 From: vyzo Date: Thu, 29 Nov 2018 12:24:40 +0200 Subject: [PATCH 1/2] add delay in initial relay advertisement to allow the dht time to bootstrap --- p2p/host/relay/autorelay_test.go | 3 ++- p2p/host/relay/relay.go | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/p2p/host/relay/autorelay_test.go b/p2p/host/relay/autorelay_test.go index ce50ea8c09..4d57d78048 100644 --- a/p2p/host/relay/autorelay_test.go +++ b/p2p/host/relay/autorelay_test.go @@ -26,9 +26,10 @@ import ( // test specific parameters func init() { - autonat.AutoNATIdentifyDelay = 100 * time.Millisecond + autonat.AutoNATIdentifyDelay = 500 * time.Millisecond autonat.AutoNATBootDelay = 1 * time.Second relay.BootDelay = 1 * time.Second + relay.AdvertiseBootDelay = 1 * time.Millisecond manet.Private4 = []*net.IPNet{} } diff --git a/p2p/host/relay/relay.go b/p2p/host/relay/relay.go index 69b6737078..3f282a13c1 100644 --- a/p2p/host/relay/relay.go +++ b/p2p/host/relay/relay.go @@ -2,6 +2,7 @@ package relay import ( "context" + "time" basic "github.com/libp2p/go-libp2p/p2p/host/basic" @@ -10,6 +11,10 @@ import ( ma "github.com/multiformats/go-multiaddr" ) +var ( + AdvertiseBootDelay = 5 * time.Second +) + // RelayHost is a Host that provides Relay services. type RelayHost struct { *basic.BasicHost @@ -25,7 +30,10 @@ func NewRelayHost(ctx context.Context, bhost *basic.BasicHost, advertise discove advertise: advertise, } bhost.AddrsFactory = h.hostAddrs - discovery.Advertise(ctx, advertise, RelayRendezvous) + go func() { + time.Sleep(AdvertiseBootDelay) + discovery.Advertise(ctx, advertise, RelayRendezvous) + }() return h } From 5d0a6e23ce373afa83515d34bedc1493b4fac0bc Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 5 Dec 2018 10:02:56 +0200 Subject: [PATCH 2/2] use select for initial advertisement delay --- p2p/host/relay/relay.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/p2p/host/relay/relay.go b/p2p/host/relay/relay.go index 3f282a13c1..b5db9ce586 100644 --- a/p2p/host/relay/relay.go +++ b/p2p/host/relay/relay.go @@ -31,8 +31,11 @@ func NewRelayHost(ctx context.Context, bhost *basic.BasicHost, advertise discove } bhost.AddrsFactory = h.hostAddrs go func() { - time.Sleep(AdvertiseBootDelay) - discovery.Advertise(ctx, advertise, RelayRendezvous) + select { + case <-time.After(AdvertiseBootDelay): + discovery.Advertise(ctx, advertise, RelayRendezvous) + case <-ctx.Done(): + } }() return h }