mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 04:47:18 +00:00
a901a154dc
* /healthz part1 * lint, fix * lints and todos * add p2p status test * Add test for service registry Statuses * fix my fake errors for the linter * Test healthz handler * Run gazelle
43 lines
754 B
Go
43 lines
754 B
Go
package p2p
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/libp2p/go-libp2p-host"
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
var (
|
|
peerCountMetric = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
Name: "p2p_peer_count",
|
|
Help: "The number of currently connected peers",
|
|
})
|
|
)
|
|
|
|
func init() {
|
|
prometheus.MustRegister(peerCountMetric)
|
|
}
|
|
|
|
func startPeerWatcher(ctx context.Context, h host.Host) {
|
|
|
|
go (func() {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
peerCountMetric.Set(float64(peerCount(h)))
|
|
|
|
// Wait 1 second to update again
|
|
time.Sleep(1 * time.Second)
|
|
}
|
|
}
|
|
})()
|
|
}
|
|
|
|
func peerCount(h host.Host) int {
|
|
// Return number of peers in peerstore, excluding our own host.
|
|
return len(h.Peerstore().Peers()) - 1
|
|
}
|