@@ -16,20 +16,22 @@ import (
16
16
)
17
17
18
18
type peerTracker struct {
19
- host host.Host
20
- connGater * conngater.BasicConnectionGater
21
- metrics * exchangeMetrics
22
19
protocolID protocol.ID
23
- peerLk sync.RWMutex
20
+
21
+ host host.Host
22
+ connGater * conngater.BasicConnectionGater
23
+
24
+ peerLk sync.RWMutex
24
25
// trackedPeers contains active peers that we can request to.
25
- // we cache the peer once they disconnect,
26
26
// so we can guarantee that peerQueue will only contain active peers
27
27
trackedPeers map [libpeer.ID ]struct {}
28
28
29
29
// an optional interface used to periodically dump
30
30
// good peers during garbage collection
31
31
pidstore PeerIDStore
32
32
33
+ metrics * exchangeMetrics
34
+
33
35
ctx context.Context
34
36
cancel context.CancelFunc
35
37
// done is used to gracefully stop the peerTracker.
@@ -103,19 +105,20 @@ func (p *peerTracker) track() {
103
105
p .done <- struct {}{}
104
106
}()
105
107
106
- connSubs , err := p .host .EventBus ().Subscribe (& event.EvtPeerConnectednessChanged {})
108
+ evtBus := p .host .EventBus ()
109
+ connSubs , err := evtBus .Subscribe (& event.EvtPeerConnectednessChanged {})
107
110
if err != nil {
108
111
log .Errorw ("subscribing to EvtPeerConnectednessChanged" , "err" , err )
109
112
return
110
113
}
111
114
112
- identifySub , err := p . host . EventBus () .Subscribe (& event.EvtPeerIdentificationCompleted {})
115
+ identifySub , err := evtBus .Subscribe (& event.EvtPeerIdentificationCompleted {})
113
116
if err != nil {
114
117
log .Errorw ("subscribing to EvtPeerIdentificationCompleted" , "err" , err )
115
118
return
116
119
}
117
120
118
- protocolSub , err := p . host . EventBus () .Subscribe (& event.EvtPeerProtocolsUpdated {})
121
+ protocolSub , err := evtBus .Subscribe (& event.EvtPeerProtocolsUpdated {})
119
122
if err != nil {
120
123
log .Errorw ("subscribing to EvtPeerProtocolsUpdated" , "err" , err )
121
124
return
@@ -124,9 +127,7 @@ func (p *peerTracker) track() {
124
127
for {
125
128
select {
126
129
case <- p .ctx .Done ():
127
- err = connSubs .Close ()
128
- errors .Join (err , identifySub .Close (), protocolSub .Close ())
129
- if err != nil {
130
+ if err := closeSubscriptions (connSubs , identifySub , protocolSub ); err != nil {
130
131
log .Errorw ("closing subscriptions" , "err" , err )
131
132
}
132
133
return
@@ -135,35 +136,23 @@ func (p *peerTracker) track() {
135
136
if network .NotConnected == ev .Connectedness {
136
137
p .disconnected (ev .Peer )
137
138
}
138
- case subscription := <- identifySub .Out ():
139
- ev := subscription .(event.EvtPeerIdentificationCompleted )
140
- p .connected (ev .Peer )
141
- case subscription := <- protocolSub .Out ():
142
- ev := subscription .(event.EvtPeerProtocolsUpdated )
139
+ case identSubscription := <- identifySub .Out ():
140
+ ev := identSubscription .(event.EvtPeerIdentificationCompleted )
141
+ if slices .Contains (ev .Protocols , p .protocolID ) {
142
+ p .connected (ev .Peer )
143
+ }
144
+ case protocolSubscription := <- protocolSub .Out ():
145
+ ev := protocolSubscription .(event.EvtPeerProtocolsUpdated )
143
146
if slices .Contains (ev .Removed , p .protocolID ) {
144
147
p .disconnected (ev .Peer )
145
- break
146
148
}
147
- p .connected (ev .Peer )
149
+ if slices .Contains (ev .Added , p .protocolID ) {
150
+ p .connected (ev .Peer )
151
+ }
148
152
}
149
153
}
150
154
}
151
155
152
- // getPeers returns the tracker's currently tracked peers up to the `max`.
153
- func (p * peerTracker ) getPeers (max int ) []libpeer.ID {
154
- p .peerLk .RLock ()
155
- defer p .peerLk .RUnlock ()
156
-
157
- peers := make ([]libpeer.ID , 0 , max )
158
- for peer := range p .trackedPeers {
159
- peers = append (peers , peer )
160
- if len (peers ) == max {
161
- break
162
- }
163
- }
164
- return peers
165
- }
166
-
167
156
func (p * peerTracker ) connected (pID libpeer.ID ) {
168
157
if err := pID .Validate (); err != nil {
169
158
return
@@ -173,15 +162,6 @@ func (p *peerTracker) connected(pID libpeer.ID) {
173
162
return
174
163
}
175
164
176
- // check that peer supports our protocol id.
177
- protocol , err := p .host .Peerstore ().SupportsProtocols (pID , p .protocolID )
178
- if err != nil {
179
- return
180
- }
181
- if ! slices .Contains (protocol , p .protocolID ) {
182
- return
183
- }
184
-
185
165
for _ , c := range p .host .Network ().ConnsToPeer (pID ) {
186
166
// check if connection is short-termed and skip this peer
187
167
if c .Stat ().Limited {
@@ -219,17 +199,21 @@ func (p *peerTracker) disconnected(pID libpeer.ID) {
219
199
p .metrics .peersDisconnected (1 )
220
200
}
221
201
222
- func (p * peerTracker ) peers () []* peerStat {
202
+ // peers returns the tracker's currently tracked peers up to the `max`.
203
+ func (p * peerTracker ) peers (max int ) []* peerStat {
223
204
p .peerLk .RLock ()
224
205
defer p .peerLk .RUnlock ()
225
206
226
- peers := make ([]* peerStat , 0 )
207
+ peers := make ([]* peerStat , 0 , max )
227
208
for peerID := range p .trackedPeers {
228
209
score := 0
229
210
if info := p .host .ConnManager ().GetTagInfo (peerID ); info != nil {
230
211
score = info .Tags [string (p .protocolID )]
231
212
}
232
213
peers = append (peers , & peerStat {peerID : peerID , peerScore : score })
214
+ if len (peers ) == max {
215
+ break
216
+ }
233
217
}
234
218
return peers
235
219
}
@@ -300,3 +284,11 @@ func (p *peerTracker) updateScore(stats *peerStat, size uint64, duration time.Du
300
284
score := stats .updateStats (size , duration )
301
285
p .host .ConnManager ().TagPeer (stats .peerID , string (p .protocolID ), score )
302
286
}
287
+
288
+ func closeSubscriptions (subs ... event.Subscription ) error {
289
+ var err error
290
+ for _ , sub := range subs {
291
+ err = errors .Join (err , sub .Close ())
292
+ }
293
+ return err
294
+ }
0 commit comments