-
Notifications
You must be signed in to change notification settings - Fork 0
/
gossip.go
230 lines (184 loc) · 4.75 KB
/
gossip.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//go:build !solution
package gossip
import (
"context"
"io"
"log"
"time"
"github.com/mmvpm/gossip/service"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type PeerConfig struct {
SelfAddr string
PingPeriod time.Duration
}
type Peer struct {
service.UnimplementedGossipServiceServer
config PeerConfig
ctx context.Context
cancel context.CancelFunc
connections *Connections
}
func NewPeer(config PeerConfig) *Peer {
ctx, cancel := context.WithCancel(context.Background())
return &Peer{
config: config,
ctx: ctx,
cancel: cancel,
connections: NewConnections(config.SelfAddr),
}
}
func (p *Peer) Addr() string {
return p.config.SelfAddr
}
func (p *Peer) GetMembers() map[string]PeerInfo {
allData := p.connections.GetAllData()
members := make(map[string]PeerInfo, len(allData))
for addr, data := range allData {
members[addr] = peerDataToInfo(data)
}
return members
}
func (p *Peer) UpdateSelfInfo(info *PeerInfo) {
p.connections.UpdateSelfInfo(info)
log.Println(p.Addr(), "update info:", info)
}
func (p *Peer) AddSeed(seed string) {
conn, err := p.openConn(seed)
if err != nil {
log.Println(p.Addr(), "cannot open connection to", seed, "because of", err)
return
}
p.connections.AddSeed(seed, conn)
log.Println(p.Addr(), "added a new seed", seed)
}
func (p *Peer) Run() {
log.Println(p.Addr(), "started")
ticker := time.NewTicker(p.config.PingPeriod)
for {
select {
case <-p.ctx.Done():
log.Println(p.Addr(), "client stopped because of", p.ctx.Err())
return
case <-ticker.C:
p.sendInfoToSomeone()
}
}
}
func (p *Peer) Stop() {
p.cancel()
log.Println(p.Addr(), "cancelled context")
p.connections.Close()
log.Println(p.Addr(), "closed all connections")
}
// gRPC server implementation
func (p *Peer) Ping(context.Context, *service.PingMessage) (*service.PongMessage, error) {
return &service.PongMessage{}, nil
}
func (p *Peer) ShareData(stream service.GossipService_ShareDataServer) error {
otherData, err := p.recvAllInfo(stream.Recv)
if err != nil {
return err
}
p.updateWithOtherData(otherData)
log.Println(p.Addr(), "received other data")
if err = p.sendAllInfo(stream.Send); err != nil {
return err
}
log.Println(p.Addr(), "sent self data")
return nil
}
// gRPC client implementation
func (p *Peer) openConn(seed string) (*grpc.ClientConn, error) {
conn, err := grpc.Dial(seed, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
if err = p.pingConn(conn); err != nil {
log.Println(p.Addr(), "failed to ping", seed, "because of", err)
_ = conn.Close()
return nil, err
}
return conn, nil
}
func (p *Peer) pingConn(conn *grpc.ClientConn) error {
client := service.NewGossipServiceClient(conn)
_, err := client.Ping(p.ctx, &service.PingMessage{})
if err != nil {
return err
}
return nil
}
func (p *Peer) sendInfoToSomeone() {
seed, conn, err := p.connections.GetRandomConn()
if err != nil {
return
}
log.Println(p.Addr(), "chose", seed, "to send info")
if err = p.sendDataToConn(conn); err != nil {
p.connections.DeleteSeed(seed)
log.Println(p.Addr(), "deleted", seed, "because of", err)
}
}
func (p *Peer) sendDataToConn(conn *grpc.ClientConn) error {
client := service.NewGossipServiceClient(conn)
stream, err := client.ShareData(p.ctx)
if err != nil {
log.Println(p.Addr(), "cannot obtain share-stream because of", err)
return err
}
if err = p.sendAllInfo(stream.Send); err != nil {
return err
}
_ = stream.CloseSend()
log.Println(p.Addr(), "sent self data")
otherData, err := p.recvAllInfo(stream.Recv)
if err != nil {
return err
}
p.updateWithOtherData(otherData)
log.Println(p.Addr(), "received other data")
return nil
}
// internal
func (p *Peer) sendAllInfo(send func(*service.PeerData) error) error {
for _, info := range p.connections.GetAllData() {
if p.ctx.Err() != nil {
log.Println(p.Addr(), "context cancelled during sendAllInfo")
return nil
}
if err := send(info); err != nil {
log.Println(p.Addr(), "got", err, "during sendAllInfo")
return err
}
}
return nil
}
func (p *Peer) recvAllInfo(recv func() (*service.PeerData, error)) (map[string]*service.PeerData, error) {
result := make(map[string]*service.PeerData)
for {
if p.ctx.Err() != nil {
log.Println(p.Addr(), "context cancelled during recvAllInfo")
return nil, nil
}
data, err := recv()
if err == io.EOF {
break
}
if err != nil {
log.Println(p.Addr(), "got", err, "during recvAllInfo")
return nil, err
}
result[data.GetAddr()] = data
}
return result, nil
}
func (p *Peer) updateWithOtherData(otherData map[string]*service.PeerData) {
for seed, data := range otherData {
exists := p.connections.UpsertData(seed, data)
if !exists {
p.AddSeed(seed)
}
}
}