From 486eb740b55313853c587274c07d767f964e6443 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Thu, 24 Dec 2015 14:10:59 +0000 Subject: [PATCH] optimisation: abort GossipSender.deliver when stopped --- mesh/gossip.go | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mesh/gossip.go b/mesh/gossip.go index f4b94fec..1c73ac78 100644 --- a/mesh/gossip.go +++ b/mesh/gossip.go @@ -67,13 +67,13 @@ func (s *GossipSender) run(stop <-chan struct{}, more <-chan struct{}, flush <-c case <-stop: return case <-more: - sent = s.deliver() || sent + sent = s.deliver(stop) || sent case ch := <-flush: // for testing // send anything pending, then reply back whether we sent // anything since previous flush select { case <-more: - sent = s.deliver() || sent + sent = s.deliver(stop) || sent default: } ch <- sent @@ -82,13 +82,18 @@ func (s *GossipSender) run(stop <-chan struct{}, more <-chan struct{}, flush <-c } } -func (s *GossipSender) deliver() bool { +func (s *GossipSender) deliver(stop <-chan struct{}) bool { sent := false // We must not hold our lock when sending, since that would block // the callers of Send/Broadcast while we are stuck waiting for // network congestion to clear. So we pick and send one piece of // data at a time, only holding the lock during the picking. for { + select { + case <-stop: + return sent + default: + } data, makeProtocolMsg := s.pick() if data == nil { return sent