Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PeerRouting in autorelay to find relay peer addresses #531

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
if hop {
h = relay.NewRelayHost(swrm.Context(), h.(*bhost.BasicHost), discovery)
} else {
h = relay.NewAutoRelayHost(swrm.Context(), h.(*bhost.BasicHost), discovery)
h = relay.NewAutoRelayHost(swrm.Context(), h.(*bhost.BasicHost), discovery, router)
}
}

Expand Down
15 changes: 14 additions & 1 deletion p2p/host/relay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
inet "github.com/libp2p/go-libp2p-net"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
routing "github.com/libp2p/go-libp2p-routing"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)
Expand Down Expand Up @@ -44,6 +45,7 @@ func init() {
type AutoRelayHost struct {
*basic.BasicHost
discover discovery.Discoverer
router routing.PeerRouting
autonat autonat.AutoNAT
addrsF basic.AddrsFactory

Expand All @@ -54,10 +56,11 @@ type AutoRelayHost struct {
addrs []ma.Multiaddr
}

func NewAutoRelayHost(ctx context.Context, bhost *basic.BasicHost, discover discovery.Discoverer) *AutoRelayHost {
func NewAutoRelayHost(ctx context.Context, bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting) *AutoRelayHost {
h := &AutoRelayHost{
BasicHost: bhost,
discover: discover,
router: router,
addrsF: bhost.AddrsFactory,
relays: make(map[peer.ID]pstore.PeerInfo),
disconnect: make(chan struct{}, 1),
Expand Down Expand Up @@ -148,6 +151,16 @@ func (h *AutoRelayHost) findRelays(ctx context.Context) {
h.mx.Unlock()

cctx, cancel := context.WithTimeout(ctx, 60*time.Second)

if len(pi.Addrs) == 0 {
pi, err = h.router.FindPeer(cctx, pi.ID)
if err != nil {
log.Debugf("error finding relay peer %s: %s", pi.ID, err.Error())
cancel()
continue
}
}

err = h.Connect(cctx, pi)
cancel()
if err != nil {
Expand Down