5
5
"math/rand"
6
6
"time"
7
7
8
+ mapset "github.com/deckarep/golang-set/v2"
8
9
dht "github.com/libp2p/go-libp2p-kad-dht"
9
10
pubsub "github.com/libp2p/go-libp2p-pubsub"
10
11
"github.com/libp2p/go-libp2p/core/discovery"
@@ -78,9 +79,15 @@ func findPeers(ctx context.Context, h host.Host, d discovery.Discoverer, ns stri
78
79
case <- ctx .Done ():
79
80
return ctx .Err ()
80
81
case <- ticker .C :
82
+ allPeerIDs := mapset .NewSet [peer.ID ]()
83
+ for _ , peerID := range h .Network ().Peers () {
84
+ allPeerIDs .Add (peerID )
85
+ }
86
+
81
87
peersBefore := len (h .Network ().Peers ())
82
88
if peersBefore >= peerTarget {
83
89
log .Debug ().Int ("peers-before" , peersBefore ).Int ("peer-target" , peerTarget ).Msg ("have enough peers" )
90
+ updatePeersMetrics (h , allPeerIDs )
84
91
continue
85
92
}
86
93
@@ -91,22 +98,19 @@ func findPeers(ctx context.Context, h host.Host, d discovery.Discoverer, ns stri
91
98
92
99
newConnections := 0
93
100
failedDials := 0
94
- ourId := h .ID ().String ()
95
101
96
- randomizedPeers := randomizePeers (peers )
102
+ for _ , p := range peers {
103
+ allPeerIDs .Add (p .ID )
104
+ }
105
+ updatePeersMetrics (h , allPeerIDs )
97
106
107
+ randomizedPeers := randomizePeers (peers )
98
108
for _ , p := range randomizedPeers {
99
109
collectPeerAddresses (p )
100
110
if p .ID == h .ID () {
101
111
continue
102
112
}
103
- connectedness := h .Network ().Connectedness (p .ID )
104
- metricsP2PPeerConnectedness .WithLabelValues (ourId , p .ID .String ()).Set (float64 (connectedness ))
105
- peerPing := h .Peerstore ().LatencyEWMA (p .ID )
106
- if peerPing != 0 {
107
- metricsP2PPeerPing .WithLabelValues (ourId , p .ID .String ()).Set (peerPing .Seconds ())
108
- }
109
- if connectedness != network .Connected {
113
+ if h .Network ().Connectedness (p .ID ) != network .Connected {
110
114
_ , err = h .Network ().DialPeer (ctx , p .ID )
111
115
if err != nil {
112
116
log .Debug ().
0 commit comments