forked from weaveworks/mesh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gossip_channel.go
158 lines (138 loc) · 4.6 KB
/
gossip_channel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package mesh
import (
"bytes"
"encoding/gob"
"fmt"
)
// gossipChannel is a logical communication channel within a physical mesh.
type gossipChannel struct {
name string
ourself *localPeer
routes *routes
gossiper Gossiper
logger Logger
}
// newGossipChannel returns a named, usable channel.
// It delegates receiving duties to the passed Gossiper.
func newGossipChannel(channelName string, ourself *localPeer, r *routes, g Gossiper, logger Logger) *gossipChannel {
return &gossipChannel{
name: channelName,
ourself: ourself,
routes: r,
gossiper: g,
logger: logger,
}
}
func (c *gossipChannel) deliverUnicast(srcName PeerName, origPayload []byte, dec *gob.Decoder) error {
var destName PeerName
if err := dec.Decode(&destName); err != nil {
return err
}
if c.ourself.Name == destName {
var payload []byte
if err := dec.Decode(&payload); err != nil {
return err
}
return c.gossiper.OnGossipUnicast(srcName, payload)
}
if err := c.relayUnicast(destName, origPayload); err != nil {
c.logf("%v", err)
}
return nil
}
func (c *gossipChannel) deliverBroadcast(srcName PeerName, _ []byte, dec *gob.Decoder) error {
var payload []byte
if err := dec.Decode(&payload); err != nil {
return err
}
data, err := c.gossiper.OnGossipBroadcast(srcName, payload)
if err != nil || data == nil {
return err
}
c.relayBroadcast(srcName, data)
return nil
}
func (c *gossipChannel) deliver(srcName PeerName, _ []byte, dec *gob.Decoder) error {
var payload []byte
if err := dec.Decode(&payload); err != nil {
return err
}
update, err := c.gossiper.OnGossip(payload)
if err != nil || update == nil {
return err
}
c.relay(srcName, update)
return nil
}
// GossipUnicast implements Gossip, relaying msg to dst, which must be a
// member of the channel.
func (c *gossipChannel) GossipUnicast(dstPeerName PeerName, msg []byte) error {
return c.relayUnicast(dstPeerName, gobEncode(c.name, c.ourself.Name, dstPeerName, msg))
}
// GossipBroadcast implements Gossip, relaying update to all members of the
// channel.
func (c *gossipChannel) GossipBroadcast(update GossipData) {
c.relayBroadcast(c.ourself.Name, update)
}
// GossipNeighbourSubset implements Gossip, relaying update to subset of members of the
// channel.
func (c *gossipChannel) GossipNeighbourSubset(update GossipData) {
c.relay(c.ourself.Name, update)
}
// Send relays data into the channel topology via random neighbours.
func (c *gossipChannel) Send(data GossipData) {
c.relay(c.ourself.Name, data)
}
// SendDown relays data into the channel topology via conn.
func (c *gossipChannel) SendDown(conn Connection, data GossipData) {
c.senderFor(conn).Send(data)
}
func (c *gossipChannel) relayUnicast(dstPeerName PeerName, buf []byte) (err error) {
if relayPeerName, found := c.routes.UnicastAll(dstPeerName); !found {
err = fmt.Errorf("unknown relay destination: %s", dstPeerName)
} else if conn, found := c.ourself.ConnectionTo(relayPeerName); !found {
err = fmt.Errorf("unable to find connection to relay peer %s", relayPeerName)
} else {
err = conn.(protocolSender).SendProtocolMsg(protocolMsg{ProtocolGossipUnicast, buf})
}
return err
}
func (c *gossipChannel) relayBroadcast(srcName PeerName, update GossipData) {
c.routes.ensureRecalculated()
for _, conn := range c.ourself.ConnectionsTo(c.routes.BroadcastAll(srcName)) {
c.senderFor(conn).Broadcast(srcName, update)
}
}
func (c *gossipChannel) relay(srcName PeerName, data GossipData) {
c.routes.ensureRecalculated()
for _, conn := range c.ourself.ConnectionsTo(c.routes.randomNeighbours(srcName)) {
c.senderFor(conn).Send(data)
}
}
func (c *gossipChannel) senderFor(conn Connection) *gossipSender {
return conn.(gossipConnection).gossipSenders().Sender(c.name, c.makeGossipSender)
}
func (c *gossipChannel) makeGossipSender(sender protocolSender, stop <-chan struct{}) *gossipSender {
return newGossipSender(c.makeMsg, c.makeBroadcastMsg, sender, stop)
}
func (c *gossipChannel) makeMsg(msg []byte) protocolMsg {
return protocolMsg{ProtocolGossip, gobEncode(c.name, c.ourself.Name, msg)}
}
func (c *gossipChannel) makeBroadcastMsg(srcName PeerName, msg []byte) protocolMsg {
return protocolMsg{ProtocolGossipBroadcast, gobEncode(c.name, srcName, msg)}
}
func (c *gossipChannel) logf(format string, args ...interface{}) {
format = "[gossip " + c.name + "]: " + format
c.logger.Printf(format, args...)
}
// GobEncode gob-encodes each item and returns the resulting byte slice.
func gobEncode(items ...interface{}) []byte {
buf := new(bytes.Buffer)
enc := gob.NewEncoder(buf)
for _, i := range items {
if err := enc.Encode(i); err != nil {
panic(err)
}
}
return buf.Bytes()
}