packages/hyprspace: implement route metrics service
This commit is contained in:
parent
c860fab44d
commit
7520d134b1
2 changed files with 58 additions and 0 deletions
|
@ -124,6 +124,9 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
|
||||||
// PeX
|
// PeX
|
||||||
go p2p.PeXService(ctx, host, cfg)
|
go p2p.PeXService(ctx, host, cfg)
|
||||||
|
|
||||||
|
// Route metrics and latency
|
||||||
|
go p2p.RouteMetricsService(ctx, host, cfg)
|
||||||
|
|
||||||
// Register the application to listen for signals
|
// Register the application to listen for signals
|
||||||
go signalHandler(ctx, host, lockPath, dht)
|
go signalHandler(ctx, host, lockPath, dht)
|
||||||
|
|
||||||
|
|
55
packages/networking/hyprspace/p2p/metrics.go
Normal file
55
packages/networking/hyprspace/p2p/metrics.go
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
package p2p
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"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, host host.Host, cfg *config.Config) {
|
||||||
|
subCon, err := host.EventBus().Subscribe(new(event.EvtPeerConnectednessChanged))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
fmt.Println("[-] Route metrics service ready")
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue