Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

p2p/enode: implement per-source timeout in FairMix #25962

Merged
merged 5 commits into from
Nov 8, 2022
Merged
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
21 changes: 14 additions & 7 deletions p2p/enode/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,27 +203,34 @@ func (m *FairMix) Close() {
func (m *FairMix) Next() bool {
m.cur = nil

var timeout <-chan time.Time
if m.timeout >= 0 {
timer := time.NewTimer(m.timeout)
timeout = timer.C
defer timer.Stop()
}
for {
source := m.pickSource()
if source == nil {
return m.nextFromAny()
}

var timeout <-chan time.Time
if source.timeout >= 0 {
timer := time.NewTimer(source.timeout)
timeout = timer.C
defer timer.Stop()
}

select {
case n, ok := <-source.next:
if ok {
m.cur = n
// Here, the timeout is reset to the configured value
// because the source delivered a node.
source.timeout = m.timeout
m.cur = n
return true
}
// This source has ended.
m.deleteSource(source)
case <-timeout:
// The selected source did not deliver a node within the timeout, so the
// timeout duration is halved for next time. This is supposed to improve
// latency with stuck sources.
source.timeout /= 2
return m.nextFromAny()
}
Expand Down