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

Add random delay to periodic peer update thread #1277

Merged
merged 3 commits into from
Sep 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion engine/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,12 @@ func (u *Unit) LaunchAfter(delay time.Duration, f func()) {
// If f is executed, the unit will not shut down until after f returns.
func (u *Unit) LaunchPeriodically(f func(), interval time.Duration, delay time.Duration) {
u.Launch(func() {
<-time.After(delay)
select {
case <-u.ctx.Done():
return
case <-time.After(delay):
}

ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
Expand Down
17 changes: 15 additions & 2 deletions engine/unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/require"

"github.com/onflow/flow-go/engine"
"github.com/onflow/flow-go/utils/unittest"
)

func TestReadyDone(t *testing.T) {
Expand All @@ -19,7 +20,7 @@ func TestReadyDone(t *testing.T) {
// takes longer than the interval, the next call will be blocked
func TestLaunchPeriod(t *testing.T) {
u := engine.NewUnit()
<-u.Ready()
unittest.RequireCloseBefore(t, u.Ready(), time.Second, "ready did not close")
logs := make([]string, 0)
u.LaunchPeriodically(func() {
logs = append(logs, "running")
Expand All @@ -38,11 +39,23 @@ func TestLaunchPeriod(t *testing.T) {
"running", "finish",
"running",
}, logs)
<-u.Done()
unittest.RequireCloseBefore(t, u.Done(), time.Second, "done did not close")

require.Equal(t, []string{
"running", "finish",
"running", "finish",
"running", "finish",
}, logs)
}

func TestLaunchPeriod_Delay(t *testing.T) {
u := engine.NewUnit()
unittest.RequireCloseBefore(t, u.Ready(), time.Second, "ready did not close")

// launch f with a large initial delay (30s)
// the function f should never be invoked, so we use t.Fail
u.LaunchPeriodically(t.Fail, time.Millisecond, time.Second*30)

// ensure we can stop the unit quickly (we should not need to wait for initial delay)
unittest.RequireCloseBefore(t, u.Done(), time.Second, "done did not close")
}
10 changes: 7 additions & 3 deletions network/p2p/peerManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package p2p
import (
"context"
"fmt"
mrand "math/rand"
"time"

"github.com/libp2p/go-libp2p-core/host"
Expand Down Expand Up @@ -83,12 +84,15 @@ func PeerManagerFactory(peerManagerOptions []Option, connectorOptions ...Connect

// Ready kicks off the ambient periodic connection updates.
func (pm *PeerManager) Ready() <-chan struct{} {
// makes sure that peer update request is invoked
// once before returning
// makes sure that peer update request is invoked once before returning
pm.RequestPeerUpdate()

// also starts running it periodically
pm.unit.LaunchPeriodically(pm.RequestPeerUpdate, pm.peerUpdateInterval, time.Duration(0))
//
// add a random delay to initial launch to avoid synchronizing this
// potentially expensive operation across the network
delay := time.Duration(mrand.Int63n(pm.peerUpdateInterval.Nanoseconds()))
pm.unit.LaunchPeriodically(pm.RequestPeerUpdate, pm.peerUpdateInterval, delay)
Comment on lines +94 to +95
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delay becomes a static configuration parameter of the update loop of the node when inserted here.

Would it be possible to instead choose the delay as part of each run through the loop? That way a node isn't "stuck" with a long delay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The delay only happens once (here), before the static-period ticker starts. The time between each peer update is still 10m, it's just the time before the first peer update that can have a longer delay.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😲 got it!


pm.unit.Launch(pm.updateLoop)

Expand Down