-
Notifications
You must be signed in to change notification settings - Fork 210
/
peers.go
164 lines (148 loc) · 3.5 KB
/
peers.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
package peers
import (
"strings"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/spacemeshos/go-spacemesh/p2p"
)
type data struct {
id peer.ID
success, failures int
failRate float64
averageLatency float64
}
func (d *data) latency(global float64) float64 {
if d.success+d.failures == 0 {
return 0.8 * global // to prioritize trying out new peer
}
if d.success == 0 {
return global + d.failRate*global
}
return d.averageLatency + d.failRate*global
}
func (p *data) less(other *data, global float64) bool {
peerLatency := p.latency(global)
otherLatency := other.latency(global)
if peerLatency < otherLatency {
return true
} else if peerLatency > otherLatency {
return false
}
return strings.Compare(string(p.id), string(other.id)) == -1
}
func New() *Peers {
return &Peers{
peers: map[peer.ID]*data{},
}
}
type Peers struct {
mu sync.Mutex
peers map[peer.ID]*data
// globalLatency is the average latency of all successful responses from peers.
// It is used as a reference value for new peers.
// And to adjust average peer latency based on failure rate.
globalLatency float64
}
func (p *Peers) Add(id peer.ID) {
p.mu.Lock()
defer p.mu.Unlock()
_, exist := p.peers[id]
if exist {
return
}
p.peers[id] = &data{id: id}
}
func (p *Peers) Delete(id peer.ID) {
p.mu.Lock()
defer p.mu.Unlock()
delete(p.peers, id)
}
func (p *Peers) OnFailure(id peer.ID) {
p.mu.Lock()
defer p.mu.Unlock()
peer, exist := p.peers[id]
if !exist {
return
}
peer.failures++
peer.failRate = float64(peer.failures) / float64(peer.success+peer.failures)
}
// OnLatency updates average peer and global latency.
func (p *Peers) OnLatency(id peer.ID, latency time.Duration) {
p.mu.Lock()
defer p.mu.Unlock()
peer, exist := p.peers[id]
if !exist {
return
}
peer.success++
peer.failRate = float64(peer.failures) / float64(peer.success+peer.failures)
if peer.averageLatency != 0 {
peer.averageLatency = 0.8*peer.averageLatency + 0.2*float64(latency)
} else {
peer.averageLatency = float64(latency)
}
if p.globalLatency != 0 {
p.globalLatency = 0.8*p.globalLatency + 0.2*float64(latency)
} else {
p.globalLatency = float64(latency)
}
}
// SelectBest peer with preferences.
func (p *Peers) SelectBestFrom(peers []peer.ID) peer.ID {
p.mu.Lock()
defer p.mu.Unlock()
var best *data
for _, peer := range peers {
pdata, exist := p.peers[peer]
if !exist {
continue
}
if best == nil {
best = pdata
} else if pdata.less(best, p.globalLatency) {
best = pdata
}
}
if best != nil {
return best.id
}
return p2p.NoPeer
}
// SelectBest selects at most n peers sorted by responsiveness and latency.
//
// SelectBest parametrized by N because sync protocol relies on receiving data from redundant
// connections to guarantee that it will get complete data set.
// If it doesn't get complete data set it will have to fallback into hash resolution, which is
// generally more expensive.
func (p *Peers) SelectBest(n int) []peer.ID {
p.mu.Lock()
defer p.mu.Unlock()
lth := min(len(p.peers), n)
if lth == 0 {
return nil
}
best := make([]*data, 0, lth)
for _, peer := range p.peers {
worst := peer
for i := range best {
if worst.less(best[i], p.globalLatency) {
best[i], worst = worst, best[i]
}
}
if len(best) < cap(best) {
best = append(best, worst)
}
}
rst := make([]peer.ID, len(best))
for i := range rst {
rst[i] = best[i].id
}
return rst
}
func (p *Peers) Total() int {
p.mu.Lock()
defer p.mu.Unlock()
return len(p.peers)
}