-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
backoff: fix flaky tests in backoff cache #1516
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,8 @@ type BackoffDiscovery struct { | |
|
||
parallelBufSz int | ||
returnedBufSz int | ||
|
||
clock clock | ||
} | ||
|
||
type BackoffDiscoveryOption func(*BackoffDiscovery) error | ||
|
@@ -33,6 +35,8 @@ func NewBackoffDiscovery(disc discovery.Discovery, stratFactory BackoffFactory, | |
|
||
parallelBufSz: 32, | ||
returnedBufSz: 32, | ||
|
||
clock: realClock{}, | ||
} | ||
|
||
for _, opt := range opts { | ||
|
@@ -68,6 +72,24 @@ func WithBackoffDiscoveryReturnedChannelSize(size int) BackoffDiscoveryOption { | |
} | ||
} | ||
|
||
type clock interface { | ||
Now() time.Time | ||
} | ||
|
||
type realClock struct{} | ||
|
||
func (c realClock) Now() time.Time { | ||
return time.Now() | ||
} | ||
|
||
// withClock lets you override the default time.Now() call. Useful for tests. | ||
func withClock(c clock) BackoffDiscoveryOption { | ||
return func(b *BackoffDiscovery) error { | ||
b.clock = c | ||
return nil | ||
} | ||
} | ||
|
||
type backoffCache struct { | ||
// strat is assigned on creation and not written to | ||
strat BackoffStrategy | ||
|
@@ -78,6 +100,8 @@ type backoffCache struct { | |
peers map[peer.ID]peer.AddrInfo | ||
sendingChs map[chan peer.AddrInfo]int | ||
ongoing bool | ||
|
||
clock clock | ||
} | ||
|
||
func (d *BackoffDiscovery) Advertise(ctx context.Context, ns string, opts ...discovery.Option) (time.Duration, error) { | ||
|
@@ -112,6 +136,7 @@ func (d *BackoffDiscovery) FindPeers(ctx context.Context, ns string, opts ...dis | |
peers: make(map[peer.ID]peer.AddrInfo), | ||
sendingChs: make(map[chan peer.AddrInfo]int), | ||
strat: d.stratFactory(), | ||
clock: d.clock, | ||
} | ||
|
||
d.peerCacheMux.Lock() | ||
|
@@ -128,7 +153,7 @@ func (d *BackoffDiscovery) FindPeers(ctx context.Context, ns string, opts ...dis | |
c.mux.Lock() | ||
defer c.mux.Unlock() | ||
|
||
timeExpired := time.Now().After(c.nextDiscover) | ||
timeExpired := d.clock.Now().After(c.nextDiscover) | ||
|
||
// If it's not yet time to search again and no searches are in progress then return cached peers | ||
if !(timeExpired || c.ongoing) { | ||
|
@@ -180,19 +205,19 @@ func findPeerDispatcher(ctx context.Context, c *backoffCache, pch <-chan peer.Ad | |
defer func() { | ||
c.mux.Lock() | ||
|
||
for ch := range c.sendingChs { | ||
close(ch) | ||
} | ||
|
||
// If the peer addresses have changed reset the backoff | ||
if checkUpdates(c.prevPeers, c.peers) { | ||
c.strat.Reset() | ||
c.prevPeers = c.peers | ||
} | ||
c.nextDiscover = time.Now().Add(c.strat.Delay()) | ||
c.nextDiscover = c.clock.Now().Add(c.strat.Delay()) | ||
|
||
c.ongoing = false | ||
c.peers = make(map[peer.ID]peer.AddrInfo) | ||
|
||
for ch := range c.sendingChs { | ||
close(ch) | ||
} | ||
c.sendingChs = make(map[chan peer.AddrInfo]int) | ||
c.mux.Unlock() | ||
}() | ||
|
@@ -221,13 +246,9 @@ func findPeerDispatcher(ctx context.Context, c *backoffCache, pch <-chan peer.Ad | |
c.peers[ai.ID] = sendAi | ||
|
||
for ch, rem := range c.sendingChs { | ||
ch <- sendAi | ||
if rem == 1 { | ||
close(ch) | ||
delete(c.sendingChs, ch) | ||
break | ||
} else if rem > 0 { | ||
rem-- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is broken, right? This is a copy of an int not a reference so it wouldn't affect the rem value in the map at all. |
||
if rem > 0 { | ||
ch <- sendAi | ||
c.sendingChs[ch] = rem - 1 | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no point doing this here, we can clean everything up in one go at the end.