refactor: simplify per-connection GossipSender gc and creation

by putting it in one place and inlining some helper functions
This commit is contained in:
Matthias Radestock committed 2015-11-16 23:21:33 +00:00
1 parent b48994b1b3
commit cab2e11ca9
1 file changed
+29 -32
+29 -32
View File
@@ -173,38 +173,31 @@ func (c *GossipChannel) Send(srcName PeerName, data GossipData) {
selectedConnections[conn] = void selectedConnections[conn] = void
} }
} }
c.sendDown(selectedConnections, data)
}
func (c *GossipChannel) SendDown(conn Connection, data GossipData) {
c.sendDown(ConnectionSet{conn: void}, data)
}
func (c *GossipChannel) sendDown(selectedConnections ConnectionSet, data GossipData) {
if len(selectedConnections) == 0 { if len(selectedConnections) == 0 {
return return
} }
connections := c.ourself.Connections() connections := c.ourself.Connections()
c.Lock() c.Lock()
defer c.Unlock() defer c.Unlock()
c.gcSenders(connections) // GC - randomly (courtesy of go's map iterator) pick some
for conn := range selectedConnections { // existing senders and stop&remove them if the associated
c.sendDown(conn, data) // connection is no longer active. We stop as soon as we
} // encounter a valid entry; the idea being that when there is
} // little or no garbage then this executes close to O(1)[1],
// whereas when there is lots of garbage we remove it quickly.
func (c *GossipChannel) SendDown(conn Connection, data GossipData) {
connections := c.ourself.Connections()
c.Lock()
defer c.Unlock()
c.gcSenders(connections)
c.sendDown(conn, data)
}
// 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 encounter a valid entry; the
// idea being that when there is little or no garbage then this
// executes close to O(1)[1], whereas when there is lots of garbage we
// remove it quickly.
// //
// [1] TODO Unfortunately, due to the desire to avoid nested locks, // [1] TODO Unfortunately, due to the desire to avoid nested
// instead of simply invoking LocalPeer.ConnectionTo(name), we pass in // locks, instead of simply invoking LocalPeer.ConnectionTo(name),
// the result of LocalPeer.Connections(). That is O(n_our_connections) // we operate on LocalPeer.Connections(). That is
// at best. // O(n_our_connections) at best.
func (c *GossipChannel) gcSenders(connections ConnectionSet) {
for conn, sender := range c.senders { for conn, sender := range c.senders {
if _, found := connections[conn]; !found { if _, found := connections[conn]; !found {
delete(c.senders, conn) delete(c.senders, conn)
@@ -213,16 +206,23 @@ func (c *GossipChannel) gcSenders(connections ConnectionSet) {
break break
} }
} }
// start senders, if necessary, and send.
for conn := range selectedConnections {
sender, found := c.senders[conn]
if !found {
sender = c.makeSender(conn)
c.senders[conn] = sender
}
sender.Send(data)
}
} }
// We have seen a couple of failures which suggest a >128GB slice was encountered. // We have seen a couple of failures which suggest a >128GB slice was encountered.
// 100MB should be enough for anyone. // 100MB should be enough for anyone.
const maxFeasibleMessageLen = 100 * 1024 * 1024 const maxFeasibleMessageLen = 100 * 1024 * 1024
func (c *GossipChannel) sendDown(conn Connection, data GossipData) { func (c *GossipChannel) makeSender(conn Connection) *GossipSender {
sender, found := c.senders[conn] return NewGossipSender(func(pending GossipData) {
if !found {
sender = NewGossipSender(func(pending GossipData) {
for _, msg := range pending.Encode() { for _, msg := range pending.Encode() {
if len(msg) > maxFeasibleMessageLen { if len(msg) > maxFeasibleMessageLen {
panic(fmt.Sprintf("Gossip message too large: len=%d bytes; on channel '%s' from %+v", len(msg), c.name, pending)) panic(fmt.Sprintf("Gossip message too large: len=%d bytes; on channel '%s' from %+v", len(msg), c.name, pending))
@@ -231,9 +231,6 @@ func (c *GossipChannel) sendDown(conn Connection, data GossipData) {
conn.(ProtocolSender).SendProtocolMsg(protocolMsg) conn.(ProtocolSender).SendProtocolMsg(protocolMsg)
} }
}) })
c.senders[conn] = sender
}
sender.Send(data)
} }
func (c *GossipChannel) log(args ...interface{}) { func (c *GossipChannel) log(args ...interface{}) {