From e9520215a09de9b2b8bf0e3fdb6d9f4cbd2c1d6b Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Wed, 23 Dec 2015 20:10:52 +0000 Subject: [PATCH] refactor: introduce a helper which has a very narrow purpose, and can use 'defer' to release the lock, which is nice. --- mesh/gossip_channel.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mesh/gossip_channel.go b/mesh/gossip_channel.go index 478f6880..17e570c7 100644 --- a/mesh/gossip_channel.go +++ b/mesh/gossip_channel.go @@ -184,11 +184,18 @@ func (c *GossipChannel) relay(srcName PeerName, data GossipData) { } func (c *GossipChannel) sendDown(conns []Connection, data GossipData) { + for _, sender := range c.sendersFor(conns) { + sender.Send(data) + } +} + +func (c *GossipChannel) sendersFor(conns []Connection) []*GossipSender { if len(conns) == 0 { - return + return nil } ourConnections := c.ourself.Connections() c.Lock() + defer c.Unlock() // GC - randomly (courtesy of go's map iterator) pick some // existing senders and stop&remove them if the associated // connection is no longer active. We stop as soon as we @@ -218,11 +225,7 @@ func (c *GossipChannel) sendDown(conns []Connection, data GossipData) { } senders[i] = sender } - c.Unlock() - // send - for _, sender := range senders { - sender.Send(data) - } + return senders } func (c *GossipChannel) makeSender(conn Connection) *GossipSender {