add resource manager NetStat api

This commit is contained in:
vyzo 2022-01-18 15:38:18 +02:00
parent 8d3f98fe38
commit d05d5bcb70
4 changed files with 121 additions and 6 deletions

View File

@ -51,6 +51,9 @@ type Net interface {
NetBlockRemove(ctx context.Context, acl NetBlockList) error //perm:admin NetBlockRemove(ctx context.Context, acl NetBlockList) error //perm:admin
NetBlockList(ctx context.Context) (NetBlockList, error) //perm:read NetBlockList(ctx context.Context) (NetBlockList, error) //perm:read
// ResourceManager API
NetStat(ctx context.Context, scope string) (NetStat, error) //perm:read
// ID returns peerID of libp2p node backing this API // ID returns peerID of libp2p node backing this API
ID(context.Context) (peer.ID, error) //perm:read ID(context.Context) (peer.ID, error) //perm:read
} }

View File

@ -12,6 +12,7 @@ import (
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"github.com/ipfs/go-graphsync" "github.com/ipfs/go-graphsync"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer" "github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub" pubsub "github.com/libp2p/go-libp2p-pubsub"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
@ -129,6 +130,14 @@ type NetBlockList struct {
IPSubnets []string IPSubnets []string
} }
type NetStat struct {
System *network.ScopeStat `json:",omitempty"`
Transient *network.ScopeStat `json:",omitempty"`
Services map[string]network.ScopeStat `json:",omitempty"`
Protocols map[string]network.ScopeStat `json:",omitempty"`
Peers map[string]network.ScopeStat
}
type ExtendedPeerInfo struct { type ExtendedPeerInfo struct {
ID peer.ID ID peer.ID
Agent string Agent string

View File

@ -25,12 +25,13 @@ import (
type NetAPI struct { type NetAPI struct {
fx.In fx.In
RawHost lp2p.RawHost RawHost lp2p.RawHost
Host host.Host Host host.Host
Router lp2p.BaseIpfsRouting Router lp2p.BaseIpfsRouting
ConnGater *conngater.BasicConnectionGater ConnGater *conngater.BasicConnectionGater
Reporter metrics.Reporter ResourceManager network.ResourceManager
Sk *dtypes.ScoreKeeper Reporter metrics.Reporter
Sk *dtypes.ScoreKeeper
} }
func (a *NetAPI) ID(context.Context) (peer.ID, error) { func (a *NetAPI) ID(context.Context) (peer.ID, error) {

102
node/impl/net/rcmgr.go Normal file
View File

@ -0,0 +1,102 @@
package net
import (
"context"
"strings"
"golang.org/x/xerrors"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
rcmgr "github.com/libp2p/go-libp2p-resource-manager"
"github.com/filecoin-project/lotus/api"
)
func (a *NetAPI) NetStat(ctx context.Context, scope string) (result api.NetStat, err error) {
switch {
case scope == "all":
rapi, ok := a.ResourceManager.(rcmgr.ResourceManagerState)
if !ok {
return result, xerrors.Errorf("rexource manager does not support ResourceManagerState API")
}
stat := rapi.Stat()
result.System = &stat.System
result.Transient = &stat.Transient
if len(stat.Services) > 0 {
result.Services = stat.Services
}
if len(stat.Protocols) > 0 {
result.Protocols = make(map[string]network.ScopeStat, len(stat.Protocols))
for proto, stat := range stat.Protocols {
result.Protocols[string(proto)] = stat
}
}
if len(stat.Peers) > 0 {
result.Peers = make(map[string]network.ScopeStat, len(stat.Peers))
for p, stat := range stat.Peers {
result.Peers[p.Pretty()] = stat
}
}
return result, nil
case scope == "system":
err = a.ResourceManager.ViewSystem(func(s network.ResourceScope) error {
stat := s.Stat()
result.System = &stat
return nil
})
return result, err
case scope == "transient":
err = a.ResourceManager.ViewTransient(func(s network.ResourceScope) error {
stat := s.Stat()
result.Transient = &stat
return nil
})
return result, err
case strings.HasPrefix(scope, "svc:"):
svc := scope[4:]
err = a.ResourceManager.ViewService(svc, func(s network.ServiceScope) error {
stat := s.Stat()
result.Services = map[string]network.ScopeStat{
svc: stat,
}
return nil
})
return result, err
case strings.HasPrefix(scope, "proto:"):
proto := scope[6:]
err = a.ResourceManager.ViewProtocol(protocol.ID(proto), func(s network.ProtocolScope) error {
stat := s.Stat()
result.Protocols = map[string]network.ScopeStat{
proto: stat,
}
return nil
})
return result, err
case strings.HasPrefix(scope, "peer:"):
p := scope[5:]
pid, err := peer.IDFromString(p)
if err != nil {
return result, err
}
err = a.ResourceManager.ViewPeer(pid, func(s network.PeerScope) error {
stat := s.Stat()
result.Peers = map[string]network.ScopeStat{
p: stat,
}
return nil
})
return result, err
default:
return result, xerrors.Errorf("invalid scope %s", scope)
}
}