-
Notifications
You must be signed in to change notification settings - Fork 0
/
peerset.go
139 lines (123 loc) · 2.53 KB
/
peerset.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
package core
import (
"math/rand"
"sync"
"time"
"github.com/libp2p/go-libp2p/core/peer"
bf "github.com/libp2p/go-libp2p/p2p/discovery/backoff"
)
type connCacheData struct {
nextTry time.Time
strat bf.BackoffStrategy
}
type Info struct {
process map[string]int
done bool
working bool
cache connCacheData
peerInfo peer.AddrInfo
}
type PeerSet struct {
set map[peer.ID]*Info
mux sync.Mutex
bfk bf.BackoffFactory
}
func NewPeerSet() *PeerSet {
ps := &PeerSet{}
ps.bfk = bf.NewPolynomialBackoff(time.Second, time.Minute*2, bf.NoJitter, time.Second, []float64{0.5, 2, 2.5}, rand.NewSource(0))
ps.set = make(map[peer.ID]*Info)
ps.mux = sync.Mutex{}
return ps
}
func (p *PeerSet) addRefCount(m map[string]int, proc string) {
pc, ok := m[proc]
if ok {
m[proc] = pc + 1
} else {
m[proc] = 1
}
}
func (p *PeerSet) subRefCount(m map[string]int, proc string) {
pc, ok := m[proc]
if ok {
if pc > 1 {
m[proc] = pc - 1
return
}
delete(m, proc)
}
}
func (p *PeerSet) Add(proc string, pa peer.AddrInfo) {
p.mux.Lock()
defer p.mux.Unlock()
info, ok := p.set[pa.ID]
if ok {
p.addRefCount(info.process, proc)
} else {
set := make(map[string]int)
set[proc] = 1
strat := p.bfk()
p.set[pa.ID] = &Info{process: set, cache: connCacheData{strat: strat, nextTry: time.Now()}, peerInfo: pa, done: false}
}
}
func (p *PeerSet) Remove(proc string, pid peer.ID) {
p.mux.Lock()
defer p.mux.Unlock()
info, ok := p.set[pid]
if ok {
p.subRefCount(info.process, proc)
_, ok = info.process[proc]
if !ok {
delete(p.set, pid)
}
return
}
// panic("not exist")
}
func (p *PeerSet) Turn(t time.Time) []peer.AddrInfo {
p.mux.Lock()
defer p.mux.Unlock()
res := make([]peer.AddrInfo, 0)
for key, val := range p.set {
if val.cache.nextTry.Before(t) && !val.done && !val.working {
res = append(res, val.peerInfo)
val.working = true
p.set[key] = val
}
}
return res
}
func (p *PeerSet) Done(id peer.ID) {
p.mux.Lock()
defer p.mux.Unlock()
info, ok := p.set[id]
if ok {
info.done = true
info.working = false
info.cache.strat.Reset()
}
}
func (p *PeerSet) Failed(id peer.ID) {
p.mux.Lock()
defer p.mux.Unlock()
info, ok := p.set[id]
if ok {
info.done = false
info.working = false
info.cache.nextTry = time.Now().Add(info.cache.strat.Delay())
}
}
func (p *PeerSet) Force(id peer.ID) {
p.mux.Lock()
defer p.mux.Unlock()
info, ok := p.set[id]
if ok {
info.done = false
info.working = true
}
}
func (p *PeerSet) Empty() bool {
p.mux.Lock()
defer p.mux.Unlock()
return len(p.set) == 0
}