Skip to content

Commit

Permalink
chore: add artificial delay to peer-exchange (#1038)
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos authored Feb 21, 2024
1 parent d65a836 commit 0bdd359
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 24 deletions.
33 changes: 14 additions & 19 deletions waku/v2/discv5/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,24 +317,7 @@ func (d *DiscoveryV5) Iterate(ctx context.Context, iterator enode.Iterator, onNo
defer iterator.Close()

peerCnt := 0
for {

if !delayedHasNext(ctx, iterator) {
return
}

peerCnt++
if peerCnt == bucketSize { // Delay every bucketSize peers discovered
peerCnt = 0
t := time.NewTimer(delayBetweenDiscoveredPeerCnt)
select {
case <-ctx.Done():
return
case <-t.C:
t.Stop()
}
}

for DelayedHasNext(ctx, iterator, &peerCnt) {
_, addresses, err := wenr.Multiaddress(iterator.Node())
if err != nil {
d.metrics.RecordError(peerInfoFailure)
Expand Down Expand Up @@ -364,7 +347,7 @@ func (d *DiscoveryV5) Iterate(ctx context.Context, iterator enode.Iterator, onNo
}
}

func delayedHasNext(ctx context.Context, iterator enode.Iterator) bool {
func DelayedHasNext(ctx context.Context, iterator enode.Iterator, peerCnt *int) bool {
// Delay if .Next() is too fast
start := time.Now()
hasNext := iterator.Next()
Expand All @@ -383,6 +366,18 @@ func delayedHasNext(ctx context.Context, iterator enode.Iterator) bool {
}
}

*peerCnt++
if *peerCnt == bucketSize { // Delay every bucketSize peers discovered
*peerCnt = 0
t := time.NewTimer(delayBetweenDiscoveredPeerCnt)
select {
case <-ctx.Done():
return false
case <-t.C:
t.Stop()
}
}

return true
}

Expand Down
10 changes: 7 additions & 3 deletions waku/v2/protocol/peer_exchange/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ func (wakuPX *WakuPeerExchange) iterate(ctx context.Context) error {
// Closing iterator
defer iterator.Close()

for iterator.Next() {
peerCnt := 0
for discv5.DelayedHasNext(ctx, iterator, &peerCnt) {
_, addresses, err := enr.Multiaddress(iterator.Node())
if err != nil {
wakuPX.log.Error("extracting multiaddrs from enr", zap.Error(err))
Expand Down Expand Up @@ -183,12 +184,15 @@ func (wakuPX *WakuPeerExchange) runPeerExchangeDiscv5Loop(ctx context.Context) {
err := wakuPX.iterate(ctx)
if err != nil {
wakuPX.log.Debug("iterating peer exchange", zap.Error(err))
time.Sleep(2 * time.Second)
}

t := time.NewTimer(5 * time.Second)
select {
case <-t.C:
t.Stop()
case <-ctx.Done():
t.Stop()
return
default:
}
}
}
4 changes: 2 additions & 2 deletions waku/v2/protocol/peer_exchange/waku_peer_exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ func TestRetrieveProvidePeerExchangePeers(t *testing.T) {
err = d2.Start(context.Background())
require.NoError(t, err)

time.Sleep(3 * time.Second) // Wait some time for peers to be discovered

// mount peer exchange
pxPeerConn1 := discv5.NewTestPeerDiscoverer()
px1, err := NewWakuPeerExchange(d1, pxPeerConn1, nil, prometheus.DefaultRegisterer, utils.Logger())
Expand All @@ -80,6 +78,8 @@ func TestRetrieveProvidePeerExchangePeers(t *testing.T) {
err = host3.Peerstore().AddProtocols(host1.ID(), PeerExchangeID_v20alpha1)
require.NoError(t, err)

time.Sleep(3 * time.Second) // Wait some time for peers to be discovered

err = px3.Request(context.Background(), 1, WithPeer(host1.ID()))
require.NoError(t, err)

Expand Down

0 comments on commit 0bdd359

Please sign in to comment.