Skip to content

Commit

Permalink
chore: add artificial delay to peer-exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Feb 20, 2024
1 parent d65a836 commit ff42f82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
36 changes: 18 additions & 18 deletions waku/v2/discv5/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,27 +313,27 @@ func (d *DiscoveryV5) PeerIterator(predicate ...Predicate) (enode.Iterator, erro
return iterator, nil
}

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

func (d *DiscoveryV5) Iterate(ctx context.Context, iterator enode.Iterator, onNode func(*enode.Node, peer.AddrInfo) error) {
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 = DelayEveryNPeers(ctx, peerCnt)

_, addresses, err := wenr.Multiaddress(iterator.Node())
if err != nil {
Expand Down Expand Up @@ -364,7 +364,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) bool {
// Delay if .Next() is too fast
start := time.Now()
hasNext := iterator.Next()
Expand Down
5 changes: 4 additions & 1 deletion waku/v2/protocol/peer_exchange/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ func (wakuPX *WakuPeerExchange) iterate(ctx context.Context) error {
// Closing iterator
defer iterator.Close()

for iterator.Next() {
peerCnt := 0
for discv5.DelayedHasNext(ctx, iterator) {
peerCnt = discv5.DelayEveryNPeers(ctx, peerCnt)

_, addresses, err := enr.Multiaddress(iterator.Node())
if err != nil {
wakuPX.log.Error("extracting multiaddrs from enr", zap.Error(err))
Expand Down

0 comments on commit ff42f82

Please sign in to comment.