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

p2p: always ensure relay connection #1903

Merged
merged 2 commits into from
Mar 20, 2023
Merged
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
27 changes: 22 additions & 5 deletions p2p/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ func NewRelayReserver(tcpNode host.Host, relay *MutablePeer) lifecycle.HookFunc
// Note a single long-lived reservation (created by server-side) is mapped to
// many short-lived limited client-side connections.
// When the reservation expires, the server needs to re-reserve.
// When the connection expires (stream reset error), then client needs to reconnect.
// When the server isn't connected to the relay anymore, it needs to reconnect/re-reserve.
// When the client connection expires (stream reset error), then client needs to reconnect.

refreshDelay := time.Until(resv.Expiration.Add(-2 * time.Minute))

Expand All @@ -67,12 +68,28 @@ func NewRelayReserver(tcpNode host.Host, relay *MutablePeer) lifecycle.HookFunc

refresh := time.After(refreshDelay)

select {
case <-ctx.Done():
return nil
case <-refresh:
timer := time.NewTimer(time.Second)

for {
select {
case <-ctx.Done():
return nil
case <-timer.C:
if len(tcpNode.Network().ConnsToPeer(relayPeer.ID)) > 0 {
continue // Still connected, continue for loop
}
log.Debug(ctx, "No relay connection, reconnecting",
z.Str("relay_peer", name))
// Break out of for loop below to reconnect/re-reserve
case <-refresh:
// Break out of for loop below to reconnect/re-reserve
}

break
}

timer.Stop()

log.Debug(ctx, "Refreshing relay circuit reservation")
relayConnGauge.WithLabelValues(name).Set(0)
}
Expand Down