-
Notifications
You must be signed in to change notification settings - Fork 388
feat: gossip improvements #5520
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
sbackend123
wants to merge
11
commits into
master
Choose a base branch
from
feat/gossip-improvements
base: master
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.
+689
−13
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3c44fa7
feat: ttl cache for gossip deduplication
sbackend123 0825424
feat: send peers in batches
sbackend123 646bdb5
feat: peer distribution with gradient
sbackend123 5fa5cc0
fix: remove fanout
sbackend123 8cb8fc1
fix: remove deduplication
sbackend123 607f400
fix: testing
sbackend123 ddb758f
fix: linter issue
sbackend123 be0e1fb
fix: address gossip coalesce review feedback
sbackend123 5ec8adf
fix: clean up
sbackend123 a7a5a15
fix: clean up 2
sbackend123 ea9a08f
fix: simplify implementation
sbackend123 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 |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| // Copyright 2026 The Swarm Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package hive | ||
|
|
||
| import ( | ||
| "maps" | ||
| "slices" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/ethersphere/bee/v2/pkg/swarm" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultGossipCoalesceInterval = time.Second | ||
| // coalesceThreshold: gossips with fewer peers are buffered; larger | ||
| // (already-batched) messages are dispatched immediately. | ||
| coalesceThreshold = 2 | ||
| ) | ||
|
|
||
| // gossipBuffer accumulates single-peer outbound gossip per addressee so it can be | ||
| // flushed as one batched message. | ||
| type gossipBuffer struct { | ||
| mu sync.Mutex | ||
| pending map[string]map[string]swarm.Address // addressee key -> peer key -> peer | ||
| interval time.Duration | ||
| maxBatch int | ||
| } | ||
|
|
||
| type gossipBatch struct { | ||
| addressee swarm.Address | ||
| peers []swarm.Address | ||
| } | ||
|
|
||
| func newGossipBuffer(interval time.Duration, maxBatch int) *gossipBuffer { | ||
| if interval == 0 { | ||
| interval = defaultGossipCoalesceInterval | ||
| } | ||
| return &gossipBuffer{ | ||
| pending: make(map[string]map[string]swarm.Address), | ||
| interval: interval, | ||
| maxBatch: maxBatch, | ||
| } | ||
| } | ||
|
|
||
| // stagePeers buffers peers for the addressee. If the buffer reaches maxBatch it is | ||
| // removed and returned so the caller can flush it immediately. | ||
| func (b *gossipBuffer) stagePeers(addressee swarm.Address, peers ...swarm.Address) (flushPeers []swarm.Address, flush bool) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
|
|
||
| key := addressee.ByteString() | ||
| peerSet, ok := b.pending[key] | ||
| if !ok { | ||
| peerSet = make(map[string]swarm.Address) | ||
| b.pending[key] = peerSet | ||
| } | ||
| for _, p := range peers { | ||
| peerSet[p.ByteString()] = p | ||
| } | ||
|
|
||
| if len(peerSet) >= b.maxBatch { | ||
| delete(b.pending, key) | ||
| return slices.Collect(maps.Values(peerSet)), true | ||
| } | ||
| return nil, false | ||
| } | ||
|
|
||
| // takeAll removes and returns all buffered entries. | ||
| func (b *gossipBuffer) takeAll() []gossipBatch { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
|
|
||
| if len(b.pending) == 0 { | ||
| return nil | ||
| } | ||
|
|
||
| out := make([]gossipBatch, 0, len(b.pending)) | ||
| for key, peerSet := range b.pending { | ||
| out = append(out, gossipBatch{ | ||
| addressee: swarm.NewAddress([]byte(key)), | ||
| peers: slices.Collect(maps.Values(peerSet)), | ||
| }) | ||
| } | ||
| b.pending = make(map[string]map[string]swarm.Address) | ||
| return out | ||
| } | ||
|
|
||
| func (b *gossipBuffer) clearAddressee(addressee swarm.Address) { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| delete(b.pending, addressee.ByteString()) | ||
| } | ||
|
|
||
| func (b *gossipBuffer) pendingAddressees() int { | ||
| b.mu.Lock() | ||
| defer b.mu.Unlock() | ||
| return len(b.pending) | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright 2026 The Swarm Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package hive | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/ethersphere/bee/v2/pkg/swarm" | ||
| ) | ||
|
|
||
| func TestGossipBufferAddAndTakeAll(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| b := newGossipBuffer(time.Second, maxBatchSize) | ||
| addressee := swarm.RandAddress(t) | ||
| peer1 := swarm.RandAddress(t) | ||
| peer2 := swarm.RandAddress(t) | ||
|
|
||
| if pending := b.takeAll(); len(pending) != 0 { | ||
| t.Fatalf("want no pending entries, got %d", len(pending)) | ||
| } | ||
|
|
||
| if _, flush := b.stagePeers(addressee, peer1); flush { | ||
| t.Fatal("unexpected immediate flush") | ||
| } | ||
|
|
||
| if _, flush := b.stagePeers(addressee, peer2); flush { | ||
| t.Fatal("unexpected immediate flush") | ||
| } | ||
|
|
||
| pending := b.takeAll() | ||
| if len(pending) != 1 { | ||
| t.Fatalf("want 1 pending entry, got %d", len(pending)) | ||
| } | ||
| if got := len(pending[0].peers); got != 2 { | ||
| t.Fatalf("want 2 coalesced peers, got %d", got) | ||
| } | ||
| if !pending[0].addressee.Equal(addressee) { | ||
| t.Fatal("unexpected addressee in pending batch") | ||
| } | ||
|
|
||
| if pending := b.takeAll(); len(pending) != 0 { | ||
| t.Fatalf("want empty buffer after takeAll, got %d pending", len(pending)) | ||
| } | ||
| } | ||
|
|
||
| func TestGossipBufferMaxBatchFlush(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| b := newGossipBuffer(time.Second, 2) | ||
| addressee := swarm.RandAddress(t) | ||
|
|
||
| b.stagePeers(addressee, swarm.RandAddress(t)) | ||
| flushPeers, flush := b.stagePeers(addressee, swarm.RandAddress(t)) | ||
| if !flush { | ||
| t.Fatal("want immediate flush at maxBatch") | ||
| } | ||
| if got := len(flushPeers); got != 2 { | ||
| t.Fatalf("want 2 peers in full batch, got %d", got) | ||
| } | ||
| if pending := b.takeAll(); len(pending) != 0 { | ||
| t.Fatalf("want empty buffer after maxBatch flush, got %d pending", len(pending)) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
this is really confusing - we first add to the item on the map just to also maybe potentially return it directly? not really
add...