-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgossip.go
197 lines (169 loc) · 4.4 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
package gogossip
import (
"errors"
"log"
"math/rand"
"os"
"sync/atomic"
"time"
)
const (
//
pullInterval = 200 * time.Millisecond
// Packet fragmentation is not considered.
maxPacketSize = 65535
// actualPayloadSize is the result of calculating the overhead
// in the process of marshaling the PullResponse.
//
// TODO(dbadoy): Figure out another serialization method(e.g.
// gob). The overhead of serializing two dimensional array via
// json is too much.
//
// type t struct {
// Data [][]byte
// }
//
// Appending []byte (len: 5000) 1000 times.
// 1. json - 6671010
// 2. gob - 5003061
actualPayloadSize = maxPacketSize - 61440
)
var (
// If call 'Push' and it returns this error, you are making
// too many requests. cache is emptied after a certain amount
// of time, so if you try again, it will be processed normally.
ErrNoSpaceCache = errors.New("too many requests")
)
type Gossiper struct {
run uint32
cfg *Config
logger *log.Logger
registry Registry
transport Transport
messages *propagator
pipe chan []byte
}
func New(reg Registry, transport Transport, cfg *Config) (*Gossiper, error) {
logger := log.New(os.Stdout, "[Gossip] ", log.LstdFlags)
if cfg == nil {
cfg = DefaultConfig()
logger.Println("config is nil. use default config")
}
if cfg.GossipNumber < 2 {
logger.Println("mininum size of GossipNumber is 2. set default value [2]")
cfg.GossipNumber = 2
}
if err := cfg.validate(); err != nil {
return nil, err
}
filter, err := newFilter(cfg.FilterWithStorage)
if err != nil {
return nil, err
}
logger.Printf("configured, Filter: %s, GossipNumber: %d, EncryptType: %s\n", filter.Kind(), cfg.GossipNumber, cfg.EncType.String())
propagator, err := newPropagator(filter)
if err != nil {
return nil, err
}
gossiper := &Gossiper{
cfg: cfg,
logger: logger,
registry: reg,
transport: transport,
messages: propagator,
pipe: make(chan []byte, 4096),
}
return gossiper, nil
}
func (g *Gossiper) Start() {
if atomic.LoadUint32(&g.run) == 1 {
return
}
go g.readLoop()
go g.pullLoop()
atomic.StoreUint32(&g.run, 1)
}
// Push is a method for surface to application for starts
// gossip. It's limits requests to prevent abnormal propagation
// when more requests than cacheSize are received.
func (g *Gossiper) Push(buf []byte) error {
if len(buf) > actualPayloadSize {
return errors.New("too big")
}
if g.messages.size() > cacheSize {
return ErrNoSpaceCache
}
g.push(idGenerator(), buf, false)
return nil
}
// MessagePipe is a method for surface to application for send
// newly messages.
func (g *Gossiper) MessagePipe() chan []byte {
return g.pipe
}
func (g *Gossiper) Pending() int {
return g.messages.size()
}
func (g *Gossiper) readLoop() {
for {
buf := make([]byte, maxPacketSize)
n, sender, err := g.transport.ReadFromUDP(buf)
if err != nil {
g.logger.Printf("readLoop: read UDP packet failure, %v\n", err)
continue
}
r := buf[:n]
g.handler(r, sender)
}
}
func (g *Gossiper) pullLoop() {
ticker := time.NewTicker(pullInterval)
defer ticker.Stop()
for {
<-ticker.C
// Request PullRequest to random peers.
//
// Since it is the starting point of the gossip protocol.
// So it follows the encType of this peer.
p, err := marshalWithEncryption(&pullRequest{}, g.cfg.EncType, g.cfg.Passphrase)
if err != nil {
g.logger.Printf("pullLoop: marshalPacketWithEncryption failure, %v\n", err)
continue
}
// Choose random peers and send.
multicastWithRawAddress(g.transport, p, g.selectRandomPeers(g.cfg.GossipNumber))
}
}
func (g *Gossiper) push(key [8]byte, value []byte, remote bool) {
if g.messages.add(key, value) && remote {
g.pipe <- value
}
}
// selectRandomPeers selects random peers.
func (g *Gossiper) selectRandomPeers(n int) []string {
peers := g.registry.Gossipiers()
if len(peers) <= n {
return peers
}
var (
random = rand.New(rand.NewSource(time.Now().UnixNano()))
result = make([]string, 0, n)
selected = make(map[string]struct{})
)
for r := random.Intn(n); len(result) != n; r = random.Intn(n) {
key := peers[r]
if _, ok := selected[key]; ok {
continue
}
selected[key] = struct{}{}
result = append(result, key)
}
return result
}
func (g *Gossiper) send(p packet, encType EncryptType) (int, error) {
b, err := marshalWithEncryption(p, encType, g.cfg.Passphrase)
if err != nil {
return 0, err
}
return g.transport.WriteToUDP(b, p.To())
}