Skip to content

Commit

Permalink
optimisation: stop GossipSender on send error
Browse files Browse the repository at this point in the history
  • Loading branch information
rade committed Dec 24, 2015
1 parent 337012a commit 5651a17
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions mesh/gossip.go
Expand Up @@ -67,13 +67,21 @@ func (s *GossipSender) run(stop <-chan struct{}, more <-chan struct{}, flush <-c
case <-stop:
return
case <-more:
sent = s.deliver(stop) || sent
sentSomething, err := s.deliver(stop)
if err != nil {
return
}
sent = sent || sentSomething
case ch := <-flush: // for testing
// send anything pending, then reply back whether we sent
// anything since previous flush
select {
case <-more:
sent = s.deliver(stop) || sent
sentSomething, err := s.deliver(stop)
if err != nil {
return
}
sent = sent || sentSomething
default:
}
ch <- sent
Expand All @@ -82,7 +90,7 @@ func (s *GossipSender) run(stop <-chan struct{}, more <-chan struct{}, flush <-c
}
}

func (s *GossipSender) deliver(stop <-chan struct{}) bool {
func (s *GossipSender) deliver(stop <-chan struct{}) (bool, error) {
sent := false
// We must not hold our lock when sending, since that would block
// the callers of Send[Gossip] while we are stuck waiting for
Expand All @@ -91,16 +99,16 @@ func (s *GossipSender) deliver(stop <-chan struct{}) bool {
for {
select {
case <-stop:
return sent
return sent, nil
default:
}
data, makeProtocolMsg := s.pick()
if data == nil {
return sent
return sent, nil
}
for _, msg := range data.Encode() {
if s.sender.SendProtocolMsg(makeProtocolMsg(msg)) != nil {
break
if err := s.sender.SendProtocolMsg(makeProtocolMsg(msg)); err != nil {
return sent, err
}
}
sent = true
Expand Down

0 comments on commit 5651a17

Please sign in to comment.