-
Notifications
You must be signed in to change notification settings - Fork 187
/
peer_gater.go
453 lines (378 loc) · 11.5 KB
/
peer_gater.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package pubsub
import (
"context"
"fmt"
"math/rand"
"sort"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
manet "github.com/multiformats/go-multiaddr/net"
)
var (
DefaultPeerGaterRetainStats = 6 * time.Hour
DefaultPeerGaterQuiet = time.Minute
DefaultPeerGaterDuplicateWeight = 0.125
DefaultPeerGaterIgnoreWeight = 1.0
DefaultPeerGaterRejectWeight = 16.0
DefaultPeerGaterThreshold = 0.33
DefaultPeerGaterGlobalDecay = ScoreParameterDecay(2 * time.Minute)
DefaultPeerGaterSourceDecay = ScoreParameterDecay(time.Hour)
)
// PeerGaterParams groups together parameters that control the operation of the peer gater
type PeerGaterParams struct {
// when the ratio of throttled/validated messages exceeds this threshold, the gater turns on
Threshold float64
// (linear) decay parameter for gater counters
GlobalDecay float64 // global counter decay
SourceDecay float64 // per IP counter decay
// decay interval
DecayInterval time.Duration
// counter zeroing threshold
DecayToZero float64
// how long to retain stats
RetainStats time.Duration
// quiet interval before turning off the gater; if there are no validation throttle events
// for this interval, the gater turns off
Quiet time.Duration
// weight of duplicate message deliveries
DuplicateWeight float64
// weight of ignored messages
IgnoreWeight float64
// weight of rejected messages
RejectWeight float64
// priority topic delivery weights
TopicDeliveryWeights map[string]float64
}
func (p *PeerGaterParams) validate() error {
if p.Threshold <= 0 {
return fmt.Errorf("invalid Threshold; must be > 0")
}
if p.GlobalDecay <= 0 || p.GlobalDecay >= 1 {
return fmt.Errorf("invalid GlobalDecay; must be between 0 and 1")
}
if p.SourceDecay <= 0 || p.SourceDecay >= 1 {
return fmt.Errorf("invalid SourceDecay; must be between 0 and 1")
}
if p.DecayInterval < time.Second {
return fmt.Errorf("invalid DecayInterval; must be at least 1s")
}
if p.DecayToZero <= 0 || p.DecayToZero >= 1 {
return fmt.Errorf("invalid DecayToZero; must be between 0 and 1")
}
// no need to check stats retention; a value of 0 means we don't retain stats
if p.Quiet < time.Second {
return fmt.Errorf("invalud Quiet interval; must be at least 1s")
}
if p.DuplicateWeight <= 0 {
return fmt.Errorf("invalid DuplicateWeight; must be > 0")
}
if p.IgnoreWeight < 1 {
return fmt.Errorf("invalid IgnoreWeight; must be >= 1")
}
if p.RejectWeight < 1 {
return fmt.Errorf("invalud RejectWeight; must be >= 1")
}
return nil
}
// WithTopicDeliveryWeights is a fluid setter for the priority topic delivery weights
func (p *PeerGaterParams) WithTopicDeliveryWeights(w map[string]float64) *PeerGaterParams {
p.TopicDeliveryWeights = w
return p
}
// NewPeerGaterParams creates a new PeerGaterParams struct, using the specified threshold and decay
// parameters and default values for all other parameters.
func NewPeerGaterParams(threshold, globalDecay, sourceDecay float64) *PeerGaterParams {
return &PeerGaterParams{
Threshold: threshold,
GlobalDecay: globalDecay,
SourceDecay: sourceDecay,
DecayToZero: DefaultDecayToZero,
DecayInterval: DefaultDecayInterval,
RetainStats: DefaultPeerGaterRetainStats,
Quiet: DefaultPeerGaterQuiet,
DuplicateWeight: DefaultPeerGaterDuplicateWeight,
IgnoreWeight: DefaultPeerGaterIgnoreWeight,
RejectWeight: DefaultPeerGaterRejectWeight,
}
}
// DefaultPeerGaterParams creates a new PeerGaterParams struct using default values
func DefaultPeerGaterParams() *PeerGaterParams {
return NewPeerGaterParams(DefaultPeerGaterThreshold, DefaultPeerGaterGlobalDecay, DefaultPeerGaterSourceDecay)
}
// the gater object.
type peerGater struct {
sync.Mutex
host host.Host
// gater parameters
params *PeerGaterParams
// counters
validate, throttle float64
// time of last validation throttle
lastThrottle time.Time
// stats per peer.ID -- multiple peer IDs may share the same stats object if they are
// colocated in the same IP
peerStats map[peer.ID]*peerGaterStats
// stats per IP
ipStats map[string]*peerGaterStats
// for unit tests
getIP func(peer.ID) string
}
type peerGaterStats struct {
// number of connected peer IDs mapped to this stat object
connected int
// stats expiration time -- only valid if connected = 0
expire time.Time
// counters
deliver, duplicate, ignore, reject float64
}
// WithPeerGater is a gossipsub router option that enables reactive validation queue
// management.
// The Gater is activated if the ratio of throttled/validated messages exceeds the specified
// threshold.
// Once active, the Gater probabilistically throttles peers _before_ they enter the validation
// queue, performing Random Early Drop.
// The throttle decision is randomized, with the probability of allowing messages to enter the
// validation queue controlled by the statistical observations of the performance of all peers
// in the IP address of the gated peer.
// The Gater deactivates if there is no validation throttlinc occurring for the specified quiet
// interval.
func WithPeerGater(params *PeerGaterParams) Option {
return func(ps *PubSub) error {
gs, ok := ps.rt.(*GossipSubRouter)
if !ok {
return fmt.Errorf("pubsub router is not gossipsub")
}
err := params.validate()
if err != nil {
return err
}
gs.gate = newPeerGater(ps.ctx, ps.host, params)
// hook the tracer
if ps.tracer != nil {
ps.tracer.raw = append(ps.tracer.raw, gs.gate)
} else {
ps.tracer = &pubsubTracer{
raw: []RawTracer{gs.gate},
pid: ps.host.ID(),
idGen: ps.idGen,
}
}
return nil
}
}
func newPeerGater(ctx context.Context, host host.Host, params *PeerGaterParams) *peerGater {
pg := &peerGater{
params: params,
peerStats: make(map[peer.ID]*peerGaterStats),
ipStats: make(map[string]*peerGaterStats),
host: host,
}
go pg.background(ctx)
return pg
}
func (pg *peerGater) background(ctx context.Context) {
tick := time.NewTicker(pg.params.DecayInterval)
defer tick.Stop()
for {
select {
case <-tick.C:
pg.decayStats()
case <-ctx.Done():
return
}
}
}
func (pg *peerGater) decayStats() {
pg.Lock()
defer pg.Unlock()
pg.validate *= pg.params.GlobalDecay
if pg.validate < pg.params.DecayToZero {
pg.validate = 0
}
pg.throttle *= pg.params.GlobalDecay
if pg.throttle < pg.params.DecayToZero {
pg.throttle = 0
}
now := time.Now()
for ip, st := range pg.ipStats {
if st.connected > 0 {
st.deliver *= pg.params.SourceDecay
if st.deliver < pg.params.DecayToZero {
st.deliver = 0
}
st.duplicate *= pg.params.SourceDecay
if st.duplicate < pg.params.DecayToZero {
st.duplicate = 0
}
st.ignore *= pg.params.SourceDecay
if st.ignore < pg.params.DecayToZero {
st.ignore = 0
}
st.reject *= pg.params.SourceDecay
if st.reject < pg.params.DecayToZero {
st.reject = 0
}
} else if st.expire.Before(now) {
delete(pg.ipStats, ip)
}
}
}
func (pg *peerGater) getPeerStats(p peer.ID) *peerGaterStats {
st, ok := pg.peerStats[p]
if !ok {
st = pg.getIPStats(p)
pg.peerStats[p] = st
}
return st
}
func (pg *peerGater) getIPStats(p peer.ID) *peerGaterStats {
ip := pg.getPeerIP(p)
st, ok := pg.ipStats[ip]
if !ok {
st = &peerGaterStats{}
pg.ipStats[ip] = st
}
return st
}
func (pg *peerGater) getPeerIP(p peer.ID) string {
if pg.getIP != nil {
return pg.getIP(p)
}
connToIP := func(c network.Conn) string {
remote := c.RemoteMultiaddr()
ip, err := manet.ToIP(remote)
if err != nil {
log.Warnf("error determining IP for remote peer in %s: %s", remote, err)
return "<unknown>"
}
return ip.String()
}
conns := pg.host.Network().ConnsToPeer(p)
switch len(conns) {
case 0:
return "<unknown>"
case 1:
return connToIP(conns[0])
default:
// we have multiple connections -- order by number of streams and use the one with the
// most streams; it's a nightmare to track multiple IPs per peer, so pick the best one.
streams := make(map[string]int)
for _, c := range conns {
if c.Stat().Limited {
// ignore transient
continue
}
streams[c.ID()] = len(c.GetStreams())
}
sort.Slice(conns, func(i, j int) bool {
return streams[conns[i].ID()] > streams[conns[j].ID()]
})
return connToIP(conns[0])
}
}
// router interface
func (pg *peerGater) AcceptFrom(p peer.ID) AcceptStatus {
if pg == nil {
return AcceptAll
}
pg.Lock()
defer pg.Unlock()
// check the quiet period; if the validation queue has not throttled for more than the Quiet
// interval, we turn off the circuit breaker and accept.
if time.Since(pg.lastThrottle) > pg.params.Quiet {
return AcceptAll
}
// no throttle events -- or they have decayed; accept.
if pg.throttle == 0 {
return AcceptAll
}
// check the throttle/validate ration; if it is below threshold we accept.
if pg.validate != 0 && pg.throttle/pg.validate < pg.params.Threshold {
return AcceptAll
}
st := pg.getPeerStats(p)
// compute the goodput of the peer; the denominator is the weighted mix of message counters
total := st.deliver + pg.params.DuplicateWeight*st.duplicate + pg.params.IgnoreWeight*st.ignore + pg.params.RejectWeight*st.reject
if total == 0 {
return AcceptAll
}
// we make a randomized decision based on the goodput of the peer.
// the probabiity is biased by adding 1 to the delivery counter so that we don't unconditionally
// throttle in the first negative event; it also ensures that a peer always has a chance of being
// accepted; this is not a sinkhole/blacklist.
threshold := (1 + st.deliver) / (1 + total)
if rand.Float64() < threshold {
return AcceptAll
}
log.Debugf("throttling peer %s with threshold %f", p, threshold)
return AcceptControl
}
// -- RawTracer interface methods
var _ RawTracer = (*peerGater)(nil)
// tracer interface
func (pg *peerGater) AddPeer(p peer.ID, proto protocol.ID) {
pg.Lock()
defer pg.Unlock()
st := pg.getPeerStats(p)
st.connected++
}
func (pg *peerGater) RemovePeer(p peer.ID) {
pg.Lock()
defer pg.Unlock()
st := pg.getPeerStats(p)
st.connected--
st.expire = time.Now().Add(pg.params.RetainStats)
delete(pg.peerStats, p)
}
func (pg *peerGater) Join(topic string) {}
func (pg *peerGater) Leave(topic string) {}
func (pg *peerGater) Graft(p peer.ID, topic string) {}
func (pg *peerGater) Prune(p peer.ID, topic string) {}
func (pg *peerGater) ValidateMessage(msg *Message) {
pg.Lock()
defer pg.Unlock()
pg.validate++
}
func (pg *peerGater) DeliverMessage(msg *Message) {
pg.Lock()
defer pg.Unlock()
st := pg.getPeerStats(msg.ReceivedFrom)
topic := msg.GetTopic()
weight := pg.params.TopicDeliveryWeights[topic]
if weight == 0 {
weight = 1
}
st.deliver += weight
}
func (pg *peerGater) RejectMessage(msg *Message, reason string) {
pg.Lock()
defer pg.Unlock()
switch reason {
case RejectValidationQueueFull:
fallthrough
case RejectValidationThrottled:
pg.lastThrottle = time.Now()
pg.throttle++
case RejectValidationIgnored:
st := pg.getPeerStats(msg.ReceivedFrom)
st.ignore++
default:
st := pg.getPeerStats(msg.ReceivedFrom)
st.reject++
}
}
func (pg *peerGater) DuplicateMessage(msg *Message) {
pg.Lock()
defer pg.Unlock()
st := pg.getPeerStats(msg.ReceivedFrom)
st.duplicate++
}
func (pg *peerGater) ThrottlePeer(p peer.ID) {}
func (pg *peerGater) RecvRPC(rpc *RPC) {}
func (pg *peerGater) SendRPC(rpc *RPC, p peer.ID) {}
func (pg *peerGater) DropRPC(rpc *RPC, p peer.ID) {}
func (pg *peerGater) UndeliverableMessage(msg *Message) {}