Skip to content
Merged
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions p2p/discover/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
package discover

import (
"container/list"
"crypto/ecdsa"
crand "crypto/rand"
"encoding/binary"
"iter"
"math/rand"
"net"
"net/netip"
Expand Down Expand Up @@ -143,3 +145,16 @@ func (r *reseedingRandom) Shuffle(n int, swap func(i, j int)) {
defer r.mu.Unlock()
r.cur.Shuffle(n, swap)
}

// iterList iterates over the elements of the given list.
func iterList[T any](l *list.List) iter.Seq2[T, *list.Element] {
return func(yield func(T, *list.Element) bool) {
for el := l.Front(); el != nil; {
next := el.Next()
if !yield(el.Value.(T), el) {
return
}
el = next
}
}
}
11 changes: 4 additions & 7 deletions p2p/discover/v4_udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,8 @@ func (t *UDPv4) loop() {
}
// Start the timer so it fires when the next pending reply has expired.
now := time.Now()
for el := plist.Front(); el != nil; el = el.Next() {
nextTimeout = el.Value.(*replyMatcher)
if dist := nextTimeout.deadline.Sub(now); dist < 2*respTimeout {
for p, el := range iterList[*replyMatcher](plist) {
if dist := p.deadline.Sub(now); dist < 2*respTimeout {
timeout.Reset(dist)
return
}
Expand Down Expand Up @@ -478,8 +477,7 @@ func (t *UDPv4) loop() {

case r := <-t.gotreply:
var matched bool // whether any replyMatcher considered the reply acceptable.
for el := plist.Front(); el != nil; el = el.Next() {
p := el.Value.(*replyMatcher)
for p, el := range iterList[*replyMatcher](plist) {
if p.from == r.from && p.ptype == r.data.Kind() && p.ip == r.ip {
ok, requestDone := p.callback(r.data)
matched = matched || ok
Expand All @@ -499,8 +497,7 @@ func (t *UDPv4) loop() {
nextTimeout = nil

// Notify and remove callbacks whose deadline is in the past.
for el := plist.Front(); el != nil; el = el.Next() {
p := el.Value.(*replyMatcher)
for p, el := range iterList[*replyMatcher](plist) {
if now.After(p.deadline) || now.Equal(p.deadline) {
p.errc <- errTimeout
plist.Remove(el)
Expand Down
Loading