Skip to content

Commit

Permalink
add resource manager NetStat api
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Jan 20, 2022
1 parent 8d3f98f commit d05d5bc
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 6 deletions.
3 changes: 3 additions & 0 deletions api/api_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ type Net interface {
NetBlockRemove(ctx context.Context, acl NetBlockList) error //perm:admin
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(context.Context) (peer.ID, error) //perm:read
}
Expand Down
9 changes: 9 additions & 0 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-graphsync"

"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
pubsub "github.com/libp2p/go-libp2p-pubsub"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -129,6 +130,14 @@ type NetBlockList struct {
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 {
ID peer.ID
Agent string
Expand Down
13 changes: 7 additions & 6 deletions node/impl/net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
type NetAPI struct {
fx.In

RawHost lp2p.RawHost
Host host.Host
Router lp2p.BaseIpfsRouting
ConnGater *conngater.BasicConnectionGater
Reporter metrics.Reporter
Sk *dtypes.ScoreKeeper
RawHost lp2p.RawHost
Host host.Host
Router lp2p.BaseIpfsRouting
ConnGater *conngater.BasicConnectionGater
ResourceManager network.ResourceManager
Reporter metrics.Reporter
Sk *dtypes.ScoreKeeper
}

func (a *NetAPI) ID(context.Context) (peer.ID, error) {
Expand Down
102 changes: 102 additions & 0 deletions node/impl/net/rcmgr.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit d05d5bc

Please sign in to comment.