-
Notifications
You must be signed in to change notification settings - Fork 993
feat(discovery): add jitter and fix backoff #2967
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
Open
nodersteam
wants to merge
14
commits into
celestiaorg:main
Choose a base branch
from
nodersteam:discovery-peer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
25df849
fix backoffDuration logic place, linear grows, skip backoff, time.Tim…
AlexMoskaleu c6a5845
fix unused var, increase backoffInitialDuration to 5 sec
AlexMoskaleu 432c96c
return var discoveryRetryTimeout
AlexMoskaleu 0f55d6a
del var discoveryRetryTimeout
AlexMoskaleu 4d86779
Merge branch 'celestiaorg:main' into discovery-peer
nodersteam 3c9a2ba
del unused drainChannel
nodersteam e4f605b
Merge branch 'main' into discovery-peer
nodersteam d7ec89b
Merge branch 'main' into discovery-peer
nodersteam d8223cd
Merge branch 'main' into discovery-peer
nodersteam 1f837b5
Merge branch 'main' into discovery-peer
nodersteam 767c424
Merge branch 'celestiaorg:main' into discovery-peer
nodersteam 2832f1c
Merge branch 'celestiaorg:main' into discovery-peer
nodersteam 5ac2076
Merge branch 'main' into discovery-peer
nodersteam 937e702
Merge branch 'celestiaorg:main' into discovery-peer
nodersteam 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 hidden or 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 |
---|---|---|
|
@@ -4,16 +4,18 @@ | |
"context" | ||
"errors" | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
|
||
"golang.org/x/sync/errgroup" | ||
|
||
logging "github.com/ipfs/go-log/v2" | ||
"github.com/libp2p/go-libp2p/core/discovery" | ||
"github.com/libp2p/go-libp2p/core/event" | ||
"github.com/libp2p/go-libp2p/core/host" | ||
"github.com/libp2p/go-libp2p/core/network" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
"github.com/libp2p/go-libp2p/p2p/host/eventbus" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
var log = logging.Logger("share/discovery") | ||
|
@@ -33,12 +35,12 @@ | |
|
||
// logInterval defines the time interval at which a warning message will be logged | ||
// if the desired number of nodes is not detected. | ||
logInterval = 5 * time.Minute | ||
logInterval = 5 * time.Minute | ||
maxBackoffDuration = 10 * time.Minute | ||
backoffInitialDuration = 5 * time.Second | ||
jitterFactor = 0.1 | ||
) | ||
|
||
// discoveryRetryTimeout defines time interval between discovery attempts, needed for tests | ||
var discoveryRetryTimeout = retryTimeout | ||
|
||
// Discovery combines advertise and discover services and allows to store discovered nodes. | ||
// TODO: The code here gets horribly hairy, so we should refactor this at some point | ||
type Discovery struct { | ||
|
@@ -57,7 +59,8 @@ | |
|
||
cancel context.CancelFunc | ||
|
||
params *Parameters | ||
params *Parameters | ||
backoffDuration time.Duration | ||
} | ||
|
||
type OnUpdatedPeers func(peerID peer.ID, isAdded bool) | ||
|
@@ -86,14 +89,15 @@ | |
} | ||
o := newOptions(opts...) | ||
return &Discovery{ | ||
tag: tag, | ||
set: newLimitedSet(params.PeersLimit), | ||
host: h, | ||
disc: d, | ||
connector: newBackoffConnector(h, defaultBackoffFactory), | ||
onUpdatedPeers: o.onUpdatedPeers, | ||
params: params, | ||
triggerDisc: make(chan struct{}), | ||
tag: tag, | ||
set: newLimitedSet(params.PeersLimit), | ||
host: h, | ||
disc: d, | ||
connector: newBackoffConnector(h, defaultBackoffFactory), | ||
onUpdatedPeers: o.onUpdatedPeers, | ||
params: params, | ||
triggerDisc: make(chan struct{}), | ||
backoffDuration: backoffInitialDuration, | ||
}, nil | ||
} | ||
|
||
|
@@ -199,21 +203,25 @@ | |
// It initiates peer discovery upon request and restarts the process until the soft limit is | ||
// reached. | ||
func (d *Discovery) discoveryLoop(ctx context.Context) { | ||
t := time.NewTicker(discoveryRetryTimeout) | ||
defer t.Stop() | ||
backoffTimer := time.NewTimer(0) | ||
defer backoffTimer.Stop() | ||
|
||
warnTicker := time.NewTicker(logInterval) | ||
defer warnTicker.Stop() | ||
|
||
for { | ||
// drain all previous ticks from the channel | ||
drainChannel(t.C) | ||
select { | ||
case <-t.C: | ||
case <-backoffTimer.C: | ||
if !d.discover(ctx) { | ||
// rerun discovery if the number of peers hasn't reached the limit | ||
d.backoffDuration += backoffInitialDuration | ||
if d.backoffDuration > maxBackoffDuration { | ||
d.backoffDuration = maxBackoffDuration | ||
} | ||
jitter := time.Duration(float64(d.backoffDuration) * jitterFactor * (rand.Float64()*2 - 1)) | ||
backoffTimer.Reset(d.backoffDuration + jitter) | ||
continue | ||
} | ||
d.backoffDuration = backoffInitialDuration | ||
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. Should we also reset |
||
case <-warnTicker.C: | ||
if d.set.Size() < d.set.Limit() { | ||
log.Warnf( | ||
|
@@ -222,8 +230,6 @@ | |
d.tag, logInterval, d.set.Size(), d.set.Limit(), | ||
) | ||
} | ||
// Do not break the loop; just continue | ||
continue | ||
case <-ctx.Done(): | ||
return | ||
} | ||
|
@@ -375,13 +381,3 @@ | |
d.host.ConnManager().Protect(peer.ID, d.tag) | ||
return true | ||
} | ||
|
||
func drainChannel(c <-chan time.Time) { | ||
for { | ||
select { | ||
case <-c: | ||
default: | ||
return | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -26,9 +26,7 @@ const ( | |
func TestDiscovery(t *testing.T) { | ||
const nodes = 10 // higher number brings higher coverage | ||
|
||
discoveryRetryTimeout = time.Millisecond * 100 // defined in discovery.go | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10) | ||
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. Why so big? 30x more than before 👀 |
||
t.Cleanup(cancel) | ||
|
||
tn := newTestnet(ctx, t) | ||
|
Oops, something went wrong.
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.
Better to keep as it was.