From d14d4a252767c227bc57381d30bb7824bf18ac6e Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 11 Jan 2018 17:15:04 -0600 Subject: [PATCH] remove TryBroadcast --- p2p/switch.go | 31 ++++--------------------------- p2p/switch_test.go | 10 ++-------- 2 files changed, 6 insertions(+), 35 deletions(-) diff --git a/p2p/switch.go b/p2p/switch.go index 4a2c3480f..7c09212b3 100644 --- a/p2p/switch.go +++ b/p2p/switch.go @@ -202,44 +202,21 @@ func (sw *Switch) OnStop() { // Broadcast runs a go routine for each attempted send, which will block // trying to send for defaultSendTimeoutSeconds. Returns a channel -// which receives broadcast result for each attempted send (success=false if times out). +// which receives success values for each attempted send (false if times out). // NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved. // TODO: Something more intelligent. - -type BroadcastResult struct { - PeerKey string - Success bool -} - -func (sw *Switch) Broadcast(chID byte, msg interface{}) chan BroadcastResult { - successChan := make(chan BroadcastResult, len(sw.peers.List())) +func (sw *Switch) Broadcast(chID byte, msg interface{}) chan bool { + successChan := make(chan bool, len(sw.peers.List())) sw.Logger.Debug("Broadcast", "channel", chID, "msg", msg) for _, peer := range sw.peers.List() { go func(peer Peer) { success := peer.Send(chID, msg) - successChan <- BroadcastResult{peer.Key(), success} + successChan <- success }(peer) } return successChan } -func (sw *Switch) TryBroadcast(chID byte, msg interface{}) chan BroadcastResult { - successChan := make(chan BroadcastResult, len(sw.peers.List())) - sw.Logger.Debug("TryBroadcast", "channel", chID, "msg", msg) - for _, peer := range sw.peers.List() { - success := peer.TrySend(chID, msg) - if success { - successChan <- BroadcastResult{peer.Key(), success} - } else { - go func(peer Peer) { - success := peer.Send(chID, msg) - successChan <- BroadcastResult{peer.Key(), success} - }(peer) - } - } - return successChan -} - // NumPeers returns the count of outbound/inbound and outbound-dialing peers. func (sw *Switch) NumPeers() (outbound, inbound, dialing int) { peers := sw.peers.List() diff --git a/p2p/switch_test.go b/p2p/switch_test.go index 9c8b763ec..75f9640b1 100644 --- a/p2p/switch_test.go +++ b/p2p/switch_test.go @@ -128,17 +128,14 @@ func TestSwitches(t *testing.T) { ch0Msg := "channel zero" ch1Msg := "channel foo" ch2Msg := "channel bar" - ch3Msg := "channel baz" s1.Broadcast(byte(0x00), ch0Msg) s1.Broadcast(byte(0x01), ch1Msg) s1.Broadcast(byte(0x02), ch2Msg) - s1.TryBroadcast(byte(0x03), ch3Msg) assertMsgReceivedWithTimeout(t, ch0Msg, byte(0x00), s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second) assertMsgReceivedWithTimeout(t, ch1Msg, byte(0x01), s2.Reactor("foo").(*TestReactor), 10*time.Millisecond, 5*time.Second) assertMsgReceivedWithTimeout(t, ch2Msg, byte(0x02), s2.Reactor("bar").(*TestReactor), 10*time.Millisecond, 5*time.Second) - assertMsgReceivedWithTimeout(t, ch3Msg, byte(0x03), s2.Reactor("bar").(*TestReactor), 10*time.Millisecond, 5*time.Second) } func assertMsgReceivedWithTimeout(t *testing.T, msg string, channel byte, reactor *TestReactor, checkPeriod, timeout time.Duration) { @@ -331,11 +328,8 @@ func BenchmarkSwitches(b *testing.B) { for i := 0; i < b.N; i++ { chID := byte(i % 4) successChan := s1.Broadcast(chID, "test data") - for res := range successChan { - if !s1.peers.Has(res.PeerKey) { - b.Error("Unexpected peerKey: " + res.PeerKey) - } - if res.Success { + for s := range successChan { + if s { numSuccess++ } else { numFailure++