hyprspace/p2p/metrics.go
Roland Urbano 2e36bcdce6
Enhanced Logging with ipfs/go-log (#90)
* Added ipfs/go-log and updated up.go

Use ipfs/go-log over fmt.Println

Fixed more log usages with go-log

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

log: refactor log usage to provide module global logger with configurable log level

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

Refactored logger initialization for modules

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

Removed object member loggers

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

* Implemented logging with ipfs/go-log

logging: fix logger level config

Co-authored-by: Max <max@privatevoid.net>

Update svc/network.go

Co-authored-by: Max <max@privatevoid.net>

removed static info log level constant

Co-authored-by: Max <max@privatevoid.net>

Update dns/server.go

Co-authored-by: Max <max@privatevoid.net>

Update dns/server.go

Co-authored-by: Max <max@privatevoid.net>

Update rpc/server.go

Co-authored-by: Max <max@privatevoid.net>

Applied MR suggestions

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

Enhanced logging

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>

* update vendorHash

* go fmt

---------

Signed-off-by: Roland Urbano <urbano.roland@gmail.com>
Co-authored-by: Max <max@privatevoid.net>
2025-12-07 19:31:49 +00:00

57 lines
1.4 KiB
Go

package p2p
import (
"context"
"sync"
"time"
"github.com/hyprspace/hyprspace/config"
"github.com/libp2p/go-libp2p/core/event"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
)
func RouteMetricsService(ctx context.Context, wg *sync.WaitGroup, host host.Host, cfg *config.Config) {
subCon, err := host.EventBus().Subscribe(new(event.EvtPeerConnectednessChanged))
if err != nil {
logger.With(err).Fatal("Failed to subscribe eventbus")
}
logger.Debug("Route metrics service ready")
wg.Add(1)
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case ev := <-subCon.Out():
evt := ev.(event.EvtPeerConnectednessChanged)
_, found := config.FindPeer(cfg.Peers, evt.Peer)
if found {
if evt.Connectedness == network.Connected {
ctx2, cancel := context.WithDeadline(ctx, time.Now().Add(30*time.Second))
ch := ping.Ping(ctx2, host, evt.Peer)
go func() {
t := time.NewTimer(15 * time.Second)
for {
select {
case <-t.C:
cancel()
case <-ctx2.Done():
return
case res := <-ch:
if res.Error == nil {
host.Peerstore().RecordLatency(evt.Peer, res.RTT)
}
time.Sleep(5 * time.Second)
}
}
}()
// wait a little before spawning another ping goroutine
time.Sleep(1 * time.Second)
}
}
}
}
}