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
Adds exponential backoff to re-spawing new streams for supposedly dead peers #483
Merged
vyzo
merged 20 commits into
libp2p:master
from
yhassanzadeh13:yahya/backoff-handle-new-peer
May 30, 2022
Merged
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
c3a6760
updates gitignore
yhassanzadeh13 5e8ec29
implements draft solution
yhassanzadeh13 7c58f7a
consolidates update and get
yhassanzadeh13 42e310b
extends test
yhassanzadeh13 90cdd55
adds cleaner logic
yhassanzadeh13 b3f58bc
removes a redundant else case
yhassanzadeh13 a9f4edf
refactors cleanup in a goroutine
yhassanzadeh13 a77d435
adds a jitter to backoff
yhassanzadeh13 6e4b2f8
stretches the sleep for cleanup
yhassanzadeh13 2761b98
reduces jitter time
yhassanzadeh13 6401d8b
fixes a test
yhassanzadeh13 4c94e5f
adds maximum backoff attempts
yhassanzadeh13 e260291
returns error for closing channel
yhassanzadeh13 c74ae78
refactors peer status exceed backoff threshold
yhassanzadeh13 7f815f0
converts if-else to switch
yhassanzadeh13 6ebc292
nit
yhassanzadeh13 8b64966
consolidates update and maximum backoff check
yhassanzadeh13 c00510e
bug fix
yhassanzadeh13 e9d42fa
nit
yhassanzadeh13 eede9ba
refactors cleanup with a ticker object
yhassanzadeh13 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
cover.out | ||
prof.out | ||
go-floodsub.test | ||
|
||
.idea/ |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package pubsub | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
const ( | ||
MinBackoffDelay = 100 * time.Millisecond | ||
MaxBackoffDelay = 10 * time.Second | ||
BackoffMultiplier = 2 | ||
) | ||
|
||
type backoff struct { | ||
mu sync.Mutex | ||
info map[peer.ID]time.Duration | ||
} | ||
|
||
func newBackoff() *backoff{ | ||
return &backoff{ | ||
mu: sync.Mutex{}, | ||
info: make(map[peer.ID]time.Duration), | ||
} | ||
} | ||
|
||
func (b *backoff) updateAndGet(id peer.ID) time.Duration{ | ||
b.mu.Lock() | ||
defer b.mu.Unlock() | ||
|
||
h, ok := b.info[id] | ||
if !ok { | ||
// first request goes immediately. | ||
h = time.Duration(0) | ||
} else if h < MinBackoffDelay { | ||
h = MinBackoffDelay | ||
} else if h < MaxBackoffDelay { | ||
h = time.Duration(BackoffMultiplier * h) | ||
if h > MaxBackoffDelay || h < 0 { | ||
h = MaxBackoffDelay | ||
} | ||
} | ||
|
||
b.info[id] = h | ||
|
||
return h | ||
} |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package pubsub | ||
|
||
import ( | ||
"math" | ||
"testing" | ||
"time" | ||
|
||
"github.com/libp2p/go-libp2p-core/peer" | ||
) | ||
|
||
func TestBackoff(t *testing.T){ | ||
id1 := peer.ID("peer-1") | ||
id2 := peer.ID("peer-2") | ||
b := newBackoff() | ||
|
||
if len(b.info) > 0 { | ||
t.Fatal("non-empty info map for backoff") | ||
} | ||
|
||
if d := b.updateAndGet(id1); d != time.Duration(0) { | ||
t.Fatalf("invalid initialization: %v", d) | ||
} | ||
if d := b.updateAndGet(id2); d != time.Duration(0) { | ||
t.Fatalf("invalid initialization: %v", d) | ||
} | ||
|
||
for i := 0; i < 10; i++{ | ||
got := b.updateAndGet(id1) | ||
|
||
expected := time.Duration(math.Pow(BackoffMultiplier, float64(i)) * float64(MinBackoffDelay)) | ||
if expected > MaxBackoffDelay { | ||
expected = MaxBackoffDelay | ||
} | ||
|
||
if expected != got { | ||
t.Fatalf("invalid backoff result, expected: %v, got: %v", expected, got) | ||
} | ||
} | ||
|
||
got := b.updateAndGet(id2) | ||
if got != MinBackoffDelay { | ||
t.Fatalf("invalid backoff result, expected: %v, got: %v", MinBackoffDelay, got) | ||
} | ||
|
||
if len(b.info) != 2 { | ||
t.Fatalf("info map size mismatch, expected: %d, got: %d", 2, len(b.info)) | ||
} | ||
} |
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.
I think we need to add a failure more if we have backed off too much and simply give up; say we try up to 10 times and then
updateAndGet
returns an error and we close the channel and forget the peer.How does that sound?
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.
maybe 10 is even too much, 3-4 attempts should be enough.
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.
Done