From c2356ebcde5dc864baf57f76fffe1440ce33d525 Mon Sep 17 00:00:00 2001 From: Matthias Radestock Date: Thu, 24 Dec 2015 12:27:33 +0000 Subject: [PATCH] optimisation: stop sending gossip on dead connections An error in SendProtocolMsg indicates that the connection is permanently broken. So there is no point continuing to attempt to send gossip on it. The connection will eventually be removed from LocalPeer, and any associated GossipSenders will be gc'ed, but that doesn't happen straight away, so this change is about not doing unnecessary work in the meantime. --- mesh/gossip_channel.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/mesh/gossip_channel.go b/mesh/gossip_channel.go index ff08e815..086fe6e0 100644 --- a/mesh/gossip_channel.go +++ b/mesh/gossip_channel.go @@ -155,11 +155,17 @@ func (c *GossipChannel) sendBroadcast(srcName PeerName, update GossipData) { if len(connections) == 0 { return } - for _, msg := range update.Encode() { - protocolMsg := ProtocolMsg{ProtocolGossipBroadcast, GobEncode(c.name, srcName, msg)} - // FIXME a single blocked connection can stall us - for _, conn := range connections { - conn.(ProtocolSender).SendProtocolMsg(protocolMsg) + msgs := update.Encode() + protocolMsgs := make([]ProtocolMsg, len(msgs), len(msgs)) + for i, msg := range msgs { + protocolMsgs[i] = ProtocolMsg{ProtocolGossipBroadcast, GobEncode(c.name, srcName, msg)} + } + // FIXME a single blocked connection can stall us + for _, conn := range connections { + for _, protocolMsg := range protocolMsgs { + if conn.(ProtocolSender).SendProtocolMsg(protocolMsg) != nil { + break + } } } } @@ -219,7 +225,9 @@ func (c *GossipChannel) makeSender(conn Connection) *GossipSender { return NewGossipSender(func(pending GossipData) { for _, msg := range pending.Encode() { protocolMsg := ProtocolMsg{ProtocolGossip, GobEncode(c.name, c.ourself.Name, msg)} - conn.(ProtocolSender).SendProtocolMsg(protocolMsg) + if conn.(ProtocolSender).SendProtocolMsg(protocolMsg) != nil { + break + } } }) }