Skip to content

Commit

Permalink
refactor: introduce a helper
Browse files Browse the repository at this point in the history
which has a very narrow purpose, and can use 'defer' to release the
lock, which is nice.
  • Loading branch information
rade committed Dec 24, 2015
1 parent 138897a commit 9702faf
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mesh/gossip_channel.go
Expand Up @@ -185,11 +185,18 @@ func (c *GossipChannel) SendDown(conn Connection, 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
Expand Down Expand Up @@ -219,11 +226,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 {
Expand Down

0 comments on commit 9702faf

Please sign in to comment.