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

Reduce autorelay boot delay and correctly handle private->public transition #570

Merged
merged 5 commits into from
Apr 4, 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 go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/jbenet/goprocess v0.0.0-20160826012719-b497e2f366b8
github.com/libp2p/go-conn-security v0.0.1
github.com/libp2p/go-conn-security-multistream v0.0.1
github.com/libp2p/go-libp2p-autonat v0.0.2
github.com/libp2p/go-libp2p-autonat v0.0.3
github.com/libp2p/go-libp2p-blankhost v0.0.1
github.com/libp2p/go-libp2p-circuit v0.0.1
github.com/libp2p/go-libp2p-crypto v0.0.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ github.com/libp2p/go-libp2p-autonat v0.0.1 h1:d5eskFxeJ4ag1ekhMC3yLTK+z+6RTw9W1Y
github.com/libp2p/go-libp2p-autonat v0.0.1/go.mod h1:fs71q5Xk+pdnKU014o2iq1RhMs9/PMaG5zXRFNnIIT4=
github.com/libp2p/go-libp2p-autonat v0.0.2 h1:ilo9QPzNPf1hMkqaPG55yzvhILf5ZtijstJhcii+l3s=
github.com/libp2p/go-libp2p-autonat v0.0.2/go.mod h1:fs71q5Xk+pdnKU014o2iq1RhMs9/PMaG5zXRFNnIIT4=
github.com/libp2p/go-libp2p-autonat v0.0.3 h1:PUD+pAx8Qs9hh+Bowzxq8RCkg/Vwrz5oCFC4peixXQk=
github.com/libp2p/go-libp2p-autonat v0.0.3/go.mod h1:fs71q5Xk+pdnKU014o2iq1RhMs9/PMaG5zXRFNnIIT4=
github.com/libp2p/go-libp2p-blankhost v0.0.1 h1:/mZuuiwntNR8RywnCFlGHLKrKLYne+qciBpQXWqp5fk=
github.com/libp2p/go-libp2p-blankhost v0.0.1/go.mod h1:Ibpbw/7cPPYwFb7PACIWdvxxv0t0XCCI10t7czjAjTc=
github.com/libp2p/go-libp2p-circuit v0.0.1 h1:DYbjyQ5ZY3QVAVYZWG4uzBQ6Wmcd1C82Bk8Q/pJlM1I=
Expand Down
28 changes: 25 additions & 3 deletions p2p/host/relay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const (
var (
DesiredRelays = 3

BootDelay = 60 * time.Second
BootDelay = 20 * time.Second

unspecificRelay = ma.StringCast("/p2p-circuit")
)
Expand Down Expand Up @@ -85,21 +85,43 @@ func (h *AutoRelayHost) background(ctx context.Context) {
return
}

// when true, we need to identify push
push := false

for {
wait := autonat.AutoNATRefreshInterval
switch h.autonat.Status() {
case autonat.NATStatusUnknown:
wait = autonat.AutoNATRetryInterval

case autonat.NATStatusPublic:
// invalidate addrs
h.mx.Lock()
if h.addrs != nil {
h.addrs = nil
push = true
}
h.mx.Unlock()

// if we had previously announced relay addrs, push our public addrs
if push {
push = false
h.PushIdentify()
}

case autonat.NATStatusPrivate:
push = false // clear, findRelays pushes as needed
h.findRelays(ctx)
}

select {
case <-h.disconnect:
// invalidate addrs
h.mx.Lock()
h.addrs = nil
if h.addrs != nil {
h.addrs = nil
push = true
}
h.mx.Unlock()
case <-time.After(wait):
case <-ctx.Done():
Expand All @@ -122,7 +144,7 @@ func (h *AutoRelayHost) findRelays(ctx context.Context) {
limit = 2 * need
}

dctx, cancel := context.WithTimeout(ctx, 60*time.Second)
dctx, cancel := context.WithTimeout(ctx, 30*time.Second)
pis, err := discovery.FindPeers(dctx, h.discover, RelayRendezvous, limit)
cancel()
if err != nil {
Expand Down