Skip to content

Commit 6572945

Browse files
fix: relay candidate added into backoff list even if reservation on that relay candidate success (#3482)
1 parent 062200b commit 6572945

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

p2p/host/autorelay/autorelay.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/libp2p/go-libp2p/core/event"
1010
"github.com/libp2p/go-libp2p/core/host"
1111
"github.com/libp2p/go-libp2p/core/network"
12+
"github.com/libp2p/go-libp2p/core/peer"
1213
"github.com/libp2p/go-libp2p/p2p/host/eventbus"
1314

1415
logging "github.com/libp2p/go-libp2p/gologshim"
@@ -53,6 +54,15 @@ func NewAutoRelay(host host.Host, opts ...Option) (*AutoRelay, error) {
5354
return r, nil
5455
}
5556

57+
// IsPeerInBackoff return true if the peer in backoff list currently
58+
func (r *AutoRelay) IsPeerInBackoff(peerID peer.ID) bool {
59+
r.relayFinder.candidateMx.Lock()
60+
defer r.relayFinder.candidateMx.Unlock()
61+
62+
_, ok := r.relayFinder.backoff[peerID]
63+
return ok
64+
}
65+
5666
func (r *AutoRelay) Start() {
5767
r.refCount.Add(1)
5868
go func() {

p2p/host/autorelay/autorelay_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,48 @@ func TestBackoff(t *testing.T) {
267267
require.Equal(t, 2, int(reservations.Load()))
268268
}
269269

270+
func TestRemovePeerFromBackoffAfterSuccess(t *testing.T) {
271+
const backoff = 20 * time.Second
272+
cl := newMockClock()
273+
274+
rh := newRelay(t)
275+
276+
var counter atomic.Int32
277+
h, err := libp2p.New(
278+
libp2p.ForceReachabilityPrivate(),
279+
)
280+
require.NoError(t, err)
281+
defer h.Close()
282+
283+
ar, err := autorelay.NewAutoRelay(h,
284+
autorelay.WithPeerSource(
285+
func(context.Context, int) <-chan peer.AddrInfo {
286+
// always return the same node, and make sure we don't try to connect to it too frequently
287+
counter.Add(1)
288+
peerChan := make(chan peer.AddrInfo, 1)
289+
peerChan <- peer.AddrInfo{ID: rh.ID(), Addrs: rh.Addrs()}
290+
close(peerChan)
291+
return peerChan
292+
}),
293+
autorelay.WithNumRelays(1),
294+
autorelay.WithBootDelay(0),
295+
autorelay.WithBackoff(backoff),
296+
autorelay.WithMinCandidates(1),
297+
autorelay.WithMaxCandidateAge(1),
298+
autorelay.WithClock(cl),
299+
autorelay.WithMinInterval(0),
300+
)
301+
require.NoError(t, err)
302+
ar.Start()
303+
defer ar.Close()
304+
305+
require.Eventually(t, func() bool {
306+
return numRelays(h) > 0
307+
}, 5*time.Second, 100*time.Millisecond, "should successfully reserve relay")
308+
309+
require.False(t, ar.IsPeerInBackoff(rh.ID()), "successfully added relay should not be in backoff list")
310+
}
311+
270312
func TestStaticRelays(t *testing.T) {
271313
const numStaticRelays = 3
272314
staticRelays := make([]peer.AddrInfo, 0, numStaticRelays)

p2p/host/autorelay/relay_finder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,13 @@ func (rf *relayFinder) connectToRelay(ctx context.Context, cand *candidate) (*ci
663663
}
664664
}
665665

666-
rf.candidateMx.Lock()
667-
rf.backoff[id] = rf.conf.clock.Now()
668-
rf.candidateMx.Unlock()
669666
var err error
670667
if cand.supportsRelayV2 {
671668
rsvp, err = circuitv2.Reserve(ctx, rf.host, cand.ai)
672669
if err != nil {
670+
rf.candidateMx.Lock()
671+
rf.backoff[id] = rf.conf.clock.Now()
672+
rf.candidateMx.Unlock()
673673
err = fmt.Errorf("failed to reserve slot: %w", err)
674674
}
675675
}

0 commit comments

Comments
 (0)