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

perf(p2p): Remove broadcast return channel (backport #3182) (#3480) #134

Merged
merged 1 commit into from
Aug 19, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[p2p]` Remove `Switch#Broadcast` unused return channel
([\#3182](https://github.com/cometbft/cometbft/pull/3182))
23 changes: 3 additions & 20 deletions p2p/switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package p2p
import (
"fmt"
"math"
"sync"
"time"

"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -266,33 +265,17 @@ func (sw *Switch) OnStop() {
// Peers

// Broadcast runs a go routine for each attempted send, which will block trying
// to send for defaultSendTimeoutSeconds. Returns a channel which receives
// success values for each attempted send (false if times out). Channel will be
// closed once msg bytes are sent to all peers (or time out).
// to send for defaultSendTimeoutSeconds.
//
// NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.
func (sw *Switch) Broadcast(e Envelope) chan bool {
sw.Logger.Debug("Broadcast", "channel", e.ChannelID)

func (sw *Switch) Broadcast(e Envelope) {
peers := sw.peers.List()
var wg sync.WaitGroup
wg.Add(len(peers))
successChan := make(chan bool, len(peers))

for _, peer := range peers {
go func(p Peer) {
defer wg.Done()
success := p.Send(e)
successChan <- success
_ = success
}(peer)
}

go func() {
wg.Wait()
close(successChan)
}()

return successChan
}

// NumPeers returns the count of outbound/inbound and outbound-dialing peers.
Expand Down
13 changes: 1 addition & 12 deletions p2p/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,22 +840,11 @@ func BenchmarkSwitchBroadcast(b *testing.B) {

b.ResetTimer()

numSuccess, numFailure := 0, 0

// Send random message from foo channel to another
for i := 0; i < b.N; i++ {
chID := byte(i % 4)
successChan := s1.Broadcast(Envelope{ChannelID: chID})
for s := range successChan {
if s {
numSuccess++
} else {
numFailure++
}
}
s1.Broadcast(Envelope{ChannelID: chID})
}

b.Logf("success: %v, failure: %v", numSuccess, numFailure)
}

func TestSwitchRemovalErr(t *testing.T) {
Expand Down
Loading