Commit 3b6d6112 authored by vyzo's avatar vyzo
Browse files

basic_host: integrate circuit relay as a transport with an option

parent 77af18a9
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
logging "github.com/ipfs/go-log" logging "github.com/ipfs/go-log"
goprocess "github.com/jbenet/goprocess" goprocess "github.com/jbenet/goprocess"
circuit "github.com/libp2p/go-libp2p-circuit"
connmgr "github.com/libp2p/go-libp2p-connmgr" connmgr "github.com/libp2p/go-libp2p-connmgr"
metrics "github.com/libp2p/go-libp2p-metrics" metrics "github.com/libp2p/go-libp2p-metrics"
mstream "github.com/libp2p/go-libp2p-metrics/stream" mstream "github.com/libp2p/go-libp2p-metrics/stream"
...@@ -97,6 +98,12 @@ type HostOpts struct { ...@@ -97,6 +98,12 @@ type HostOpts struct {
// ConnManager is a libp2p connection manager // ConnManager is a libp2p connection manager
ConnManager connmgr.ConnManager ConnManager connmgr.ConnManager
// Relay indicates whether the host should use circuit relay transport
Relay 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. // NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network.
...@@ -143,10 +150,25 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost { ...@@ -143,10 +150,25 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost {
h.cmgr = opts.ConnManager h.cmgr = opts.ConnManager
} }
var relayCtx context.Context
var relayCancel func()
if opts.Relay {
relayCtx, relayCancel = context.WithCancel(context.Background())
err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...)
if err != nil {
// perhaps inappropriate, but otherwise we have to change the interface
// to return an error, which will nost likely lead to a fatality anyway
panic(err)
}
}
h.proc = goprocess.WithTeardown(func() error { h.proc = goprocess.WithTeardown(func() error {
if h.natmgr != nil { if h.natmgr != nil {
h.natmgr.Close() h.natmgr.Close()
} }
if relayCancel != nil {
relayCancel()
}
return h.Network().Close() return h.Network().Close()
}) })
......
...@@ -268,6 +268,12 @@ ...@@ -268,6 +268,12 @@
"hash": "QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB", "hash": "QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB",
"name": "go-libp2p-peer", "name": "go-libp2p-peer",
"version": "2.2.0" "version": "2.2.0"
},
{
"author": "vyzo",
"hash": "QmP8i7FhM3jjciE34U9MZTnyD21BpxTinTzTcmVkvfM6tv",
"name": "go-libp2p-circuit",
"version": "1.0.0"
} }
], ],
"gxVersion": "0.4.0", "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