Skip to content

Commit

Permalink
p2p: always ensure relay connection (#1903)
Browse files Browse the repository at this point in the history
Always ensure active connections to relay. This is required since peers can only connect to you via a relay if you are connected to the relay. This reverts the change introduced in #1816. It also mitigates increase in DKG failures and general "not connected to peer" issues.

category: bug
ticket: #1898
  • Loading branch information
corverroos authored Mar 20, 2023
1 parent 6bba714 commit b333310
Showing 1 changed file with 22 additions and 5 deletions.
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

0 comments on commit b333310

Please sign in to comment.