-
Notifications
You must be signed in to change notification settings - Fork 377
/
conn_manager.go
147 lines (119 loc) · 3.13 KB
/
conn_manager.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
package ipfsutil
import (
"fmt"
"sync"
"github.com/libp2p/go-libp2p/core/connmgr"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/peer"
"go.uber.org/zap"
)
type TypeTagAction int
const (
TypeTagActionTag TypeTagAction = iota
TypeTagActionUntag
TypeTagActionUpsert
)
type EvtPeerTag struct {
Kind TypeTagAction
Peer peer.ID
Tag string
Diff int
Total int
}
var _ connmgr.ConnManager = (*BertyConnManager)(nil)
// keep track of peer of interest
type BertyConnManager struct {
connmgr.ConnManager
logger *zap.Logger
tagEmitter event.Emitter
muMarked sync.RWMutex
marked map[peer.ID]int
}
func NewBertyConnManager(logger *zap.Logger, cm connmgr.ConnManager) *BertyConnManager {
return &BertyConnManager{
ConnManager: cm,
logger: logger,
marked: make(map[peer.ID]int),
}
}
func (c *BertyConnManager) RegisterEventBus(bus event.Bus) (err error) {
if c.tagEmitter == nil {
c.tagEmitter, err = bus.Emitter(new(EvtPeerTag))
} else {
err = fmt.Errorf("event emitter already registered")
}
return err
}
func (c *BertyConnManager) GetPeerScore(p peer.ID) (score int, exist bool) {
c.muMarked.RLock()
score, exist = c.marked[p]
c.muMarked.RUnlock()
return
}
// TagPeer tags a peer with a string, associating a weight with the tag.
func (c *BertyConnManager) TagPeer(p peer.ID, tag string, score int) {
c.ConnManager.TagPeer(p, tag, score)
old, total := c.computePeerScore(p)
if old != total && c.tagEmitter != nil {
evt := EvtPeerTag{
Kind: TypeTagActionTag,
Peer: p,
Tag: tag,
Diff: total - old,
Total: total,
}
if err := c.tagEmitter.Emit(evt); err != nil {
c.logger.Error("unable to emit tag event", zap.Error(err))
}
}
}
// Untag removes the tagged value from the peer.
func (c *BertyConnManager) UntagPeer(p peer.ID, tag string) {
c.ConnManager.UntagPeer(p, tag)
old, total := c.computePeerScore(p)
if old != total && c.tagEmitter != nil {
evt := EvtPeerTag{
Kind: TypeTagActionUntag,
Peer: p,
Tag: tag,
Diff: total - old,
Total: total,
}
if err := c.tagEmitter.Emit(evt); err != nil {
c.logger.Error("unable to emit tag event", zap.Error(err))
}
}
}
// UpsertTag updates an existing tag or inserts a new one.
// The connection manager calls the upsert function supplying the current
// value of the tag (or zero if inexistent). The return value is used as
// the new value of the tag.
func (c *BertyConnManager) UpsertTag(p peer.ID, tag string, upsert func(int) int) {
c.ConnManager.UpsertTag(p, tag, upsert)
old, total := c.computePeerScore(p)
if old != total && c.tagEmitter != nil {
evt := EvtPeerTag{
Kind: TypeTagActionUpsert,
Peer: p,
Tag: tag,
Diff: total - old,
Total: total,
}
if err := c.tagEmitter.Emit(evt); err != nil {
c.logger.Error("unable to emit tag event", zap.Error(err))
}
}
}
func (c *BertyConnManager) computePeerScore(p peer.ID) (old, new int) {
c.muMarked.Lock()
old = c.marked[p]
if info := c.ConnManager.GetTagInfo(p); info != nil {
if new = info.Value; new > 0 {
c.marked[p] = new
} else {
delete(c.marked, p)
}
}
c.muMarked.Unlock()
return
}