Skip to content

Commit

Permalink
Merge pull request #1184 from libp2p/relay-shutdown
Browse files Browse the repository at this point in the history
don't use the context to shut down the relay
  • Loading branch information
marten-seemann authored Sep 13, 2021
2 parents 79d9954 + f3acea0 commit 9d3fd8b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
29 changes: 21 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/host/relay"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
holepunch "github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

autonat "github.com/libp2p/go-libp2p-autonat"
blankhost "github.com/libp2p/go-libp2p-blankhost"
Expand Down Expand Up @@ -201,7 +201,6 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
EnableHolePunching: cfg.EnableHolePunching,
HolePunchingOptions: cfg.HolePunchingOptions,
})

if err != nil {
swrm.Close()
return nil, err
Expand Down Expand Up @@ -251,6 +250,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {

// Note: h.AddrsFactory may be changed by AutoRelay, but non-relay version is
// used by AutoNAT below.
var autorelay *relay.AutoRelay
addrF := h.AddrsFactory
if cfg.EnableAutoRelay {
if !cfg.Relay {
Expand All @@ -259,22 +259,20 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
}

if len(cfg.StaticRelays) > 0 {
_ = relay.NewAutoRelay(ctx, h, nil, router, cfg.StaticRelays)
autorelay = relay.NewAutoRelay(h, nil, router, cfg.StaticRelays)
} else {
if router == nil {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no routing for discovery")
}

crouter, ok := router.(routing.ContentRouting)
if !ok {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery")
}

discovery := discovery.NewRoutingDiscovery(crouter)

_ = relay.NewAutoRelay(ctx, h, discovery, router, cfg.StaticRelays)
autorelay = relay.NewAutoRelay(h, discovery, router, cfg.StaticRelays)
}
}

Expand Down Expand Up @@ -341,10 +339,25 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
// start the host background tasks
h.Start()

var ho host.Host
ho = h
if router != nil {
return routed.Wrap(h, router), nil
ho = routed.Wrap(h, router)
}
if autorelay != nil {
return &autoRelayHost{Host: ho, autoRelay: autorelay}, nil
}
return h, nil
return ho, nil
}

type autoRelayHost struct {
host.Host
autoRelay *relay.AutoRelay
}

func (h *autoRelayHost) Close() error {
_ = h.autoRelay.Close()
return h.Host.Close()
}

// Option is a libp2p config option that can be given to the libp2p constructor
Expand Down
16 changes: 15 additions & 1 deletion p2p/host/relay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ type AutoRelay struct {

static []peer.AddrInfo

refCount sync.WaitGroup
ctxCancel context.CancelFunc

disconnect chan struct{}

mx sync.Mutex
Expand All @@ -59,8 +62,10 @@ type AutoRelay struct {
cachedAddrsExpiry time.Time
}

func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting, static []peer.AddrInfo) *AutoRelay {
func NewAutoRelay(bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting, static []peer.AddrInfo) *AutoRelay {
ctx, cancel := context.WithCancel(context.Background())
ar := &AutoRelay{
ctxCancel: cancel,
host: bhost,
discover: discover,
router: router,
Expand All @@ -72,6 +77,7 @@ func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discover
}
bhost.AddrsFactory = ar.hostAddrs
bhost.Network().Notify(ar)
ar.refCount.Add(1)
go ar.background(ctx)
return ar
}
Expand All @@ -81,6 +87,8 @@ func (ar *AutoRelay) hostAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
}

func (ar *AutoRelay) background(ctx context.Context) {
defer ar.refCount.Done()

subReachability, _ := ar.host.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))
defer subReachability.Close()

Expand Down Expand Up @@ -318,6 +326,12 @@ func (ar *AutoRelay) relayAddrs(addrs []ma.Multiaddr) []ma.Multiaddr {
return raddrs
}

func (ar *AutoRelay) Close() error {
ar.ctxCancel()
ar.refCount.Wait()
return nil
}

func shuffleRelays(pis []peer.AddrInfo) {
for i := range pis {
j := rand.Intn(i + 1)
Expand Down

0 comments on commit 9d3fd8b

Please sign in to comment.