Commit e44e538a authored by vyzo's avatar vyzo Committed by GitHub
Browse files

Merge pull request #210 from libp2p/feat/circuit-relay

Circuit Relay integration with basic_host
parents 77af18a9 6be81d34
Showing with 43 additions and 4 deletions
+43 -4
......@@ -9,6 +9,7 @@ import (
logging "github.com/ipfs/go-log"
goprocess "github.com/jbenet/goprocess"
circuit "github.com/libp2p/go-libp2p-circuit"
connmgr "github.com/libp2p/go-libp2p-connmgr"
metrics "github.com/libp2p/go-libp2p-metrics"
mstream "github.com/libp2p/go-libp2p-metrics/stream"
......@@ -97,10 +98,16 @@ type HostOpts struct {
// ConnManager is a libp2p connection manager
ConnManager connmgr.ConnManager
// Relay indicates whether the host should use circuit relay transport
EnableRelay bool
// RelayOpts are options for the relay transport; only meaningful when Relay=true
RelayOpts []circuit.RelayOpt
}
// NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
func NewHost(net inet.Network, opts *HostOpts) *BasicHost {
func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, error) {
h := &BasicHost{
network: net,
mux: msmux.NewMultistreamMuxer(),
......@@ -143,17 +150,32 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost {
h.cmgr = opts.ConnManager
}
var relayCtx context.Context
var relayCancel func()
h.proc = goprocess.WithTeardown(func() error {
if h.natmgr != nil {
h.natmgr.Close()
}
if relayCancel != nil {
relayCancel()
}
return h.Network().Close()
})
net.SetConnHandler(h.newConnHandler)
net.SetStreamHandler(h.newStreamHandler)
return h
if opts.EnableRelay {
relayCtx, relayCancel = context.WithCancel(ctx)
err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...)
if err != nil {
h.Close()
return nil, err
}
}
return h, nil
}
// New constructs and sets up a new *BasicHost with given Network and options.
......@@ -178,7 +200,14 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
}
}
return NewHost(net, hostopts)
h, err := NewHost(context.Background(), net, hostopts)
if err != nil {
// this cannot happen with legacy options
// plus we want to keep the (deprecated) legacy interface unchanged
panic(err)
}
return h
}
// newConnHandler is the remote-opened conn handler for inet.Network
......
......@@ -87,7 +87,11 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Ho
opts := &bhost.HostOpts{
NegotiationTimeout: -1,
}
h := bhost.NewHost(n, opts)
h, err := bhost.NewHost(mn.ctx, n, opts)
if err != nil {
return nil, err
}
mn.proc.AddChild(n.proc)
......
......@@ -268,6 +268,12 @@
"hash": "QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB",
"name": "go-libp2p-peer",
"version": "2.2.0"
},
{
"author": "vyzo",
"hash": "QmYv8PxtikjeL6AfsheUiJfoFRzKvx8hdwRf4odBWH7mQx",
"name": "go-libp2p-circuit",
"version": "1.1.3"
}
],
"gxVersion": "0.4.0",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment