Skip to content

Commit

Permalink
config: use Fx lifecycle hooks to start AutoRelay
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Feb 26, 2023
1 parent 69283f9 commit c0dbc61
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
37 changes: 18 additions & 19 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ func (cfg *Config) newBasicHost(swrm *swarm.Swarm, eventBus event.Bus) (*bhost.B
//
// This function consumes the config. Do not reuse it (really!).
func (cfg *Config) NewNode() (host.Host, error) {
if cfg.EnableAutoRelay && !cfg.Relay {
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
}
fxopts := []fx.Option{
fx.Provide(func() event.Bus {
return eventbus.NewBus(eventbus.WithMetricsTracer(eventbus.NewMetricsTracer(eventbus.WithRegisterer(cfg.PrometheusRegisterer))))
Expand Down Expand Up @@ -337,10 +340,6 @@ func (cfg *Config) NewNode() (host.Host, error) {

var h *bhost.BasicHost
fxopts = append(fxopts, fx.Invoke(func(ho *bhost.BasicHost) { h = ho }))
app := fx.New(fxopts...)
if err := app.Start(context.Background()); err != nil {
return nil, err
}

// Configure routing and autorelay
var router routing.PeerRouting
Expand All @@ -354,17 +353,22 @@ func (cfg *Config) NewNode() (host.Host, error) {

// Note: h.AddrsFactory may be changed by relayFinder, but non-relay version is
// used by AutoNAT below.
var ar *autorelay.AutoRelay
if cfg.EnableAutoRelay {
if !cfg.Relay {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
}
fxopts = append(fxopts,
fx.Invoke(func(h *bhost.BasicHost, lifecycle fx.Lifecycle) (*autorelay.AutoRelay, error) {
ar, err := autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
if err != nil {
return nil, err
}
lifecycle.Append(fx.StartStopHook(ar.Start, ar.Close))
return ar, nil
}),
)
}

ar, err = autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
if err != nil {
return nil, err
}
app := fx.New(fxopts...)
if err := app.Start(context.Background()); err != nil {
return nil, err
}

if err := cfg.addAutoNAT(h); err != nil {
Expand All @@ -377,12 +381,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
if router != nil {
ho = routed.Wrap(h, router)
}
if ar != nil {
arh := autorelay.NewAutoRelayHost(ho, ar)
arh.Start()
return arh, nil
}
return ho, nil
return &closableHost{App: app, Host: ho}, nil
}

func (cfg *Config) addAutoNAT(h *bhost.BasicHost) error {
Expand Down
19 changes: 19 additions & 0 deletions config/host.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package config

import (
"context"

"github.com/libp2p/go-libp2p/core/host"

"go.uber.org/fx"
)

type closableHost struct {
*fx.App
host.Host
}

func (h *closableHost) Close() error {
_ = h.App.Stop(context.Background())
return h.Host.Close()
}
3 changes: 1 addition & 2 deletions p2p/host/autorelay/autorelay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,8 @@ func TestMinInterval(t *testing.T) {
)
defer h.Close()

cl.Add(500 * time.Millisecond)
// The second call to peerSource should happen after 1 second
require.Never(t, func() bool { return numRelays(h) > 0 }, 500*time.Millisecond, 100*time.Millisecond)
cl.Add(500 * time.Millisecond)
cl.Add(time.Second)
require.Eventually(t, func() bool { return numRelays(h) > 0 }, 10*time.Second, 100*time.Millisecond)
}
23 changes: 0 additions & 23 deletions p2p/host/autorelay/host.go

This file was deleted.

3 changes: 3 additions & 0 deletions p2p/protocol/holepunch/holepunch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func TestNoHolePunchIfDirectConnExists(t *testing.T) {
}

func TestDirectDialWorks(t *testing.T) {
if race.WithRace() {
t.Skip("modifying manet.Private4 is racy")
}
// mark all addresses as public
cpy := manet.Private4
manet.Private4 = []*net.IPNet{}
Expand Down

0 comments on commit c0dbc61

Please sign in to comment.