package rcmgr
import (
"bytes"
"sort"
"strings"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
)
type ResourceScopeLimiter interface {
Limit () Limit
SetLimit (Limit )
}
var _ ResourceScopeLimiter = (*resourceScope )(nil )
type ResourceManagerState interface {
ListServices () []string
ListProtocols () []protocol .ID
ListPeers () []peer .ID
Stat () ResourceManagerStat
}
type ResourceManagerStat struct {
System network .ScopeStat
Transient network .ScopeStat
Services map [string ]network .ScopeStat
Protocols map [protocol .ID ]network .ScopeStat
Peers map [peer .ID ]network .ScopeStat
}
var _ ResourceManagerState = (*resourceManager )(nil )
func (s *resourceScope ) Limit () Limit {
s .Lock ()
defer s .Unlock ()
return s .rc .limit
}
func (s *resourceScope ) SetLimit (limit Limit ) {
s .Lock ()
defer s .Unlock ()
s .rc .limit = limit
}
func (s *protocolScope ) SetLimit (limit Limit ) {
s .rcmgr .setStickyProtocol (s .proto )
s .resourceScope .SetLimit (limit )
}
func (s *peerScope ) SetLimit (limit Limit ) {
s .rcmgr .setStickyPeer (s .peer )
s .resourceScope .SetLimit (limit )
}
func (r *resourceManager ) ListServices () []string {
r .mx .Lock ()
defer r .mx .Unlock ()
result := make ([]string , 0 , len (r .svc ))
for svc := range r .svc {
result = append (result , svc )
}
sort .Slice (result , func (i , j int ) bool {
return strings .Compare (result [i ], result [j ]) < 0
})
return result
}
func (r *resourceManager ) ListProtocols () []protocol .ID {
r .mx .Lock ()
defer r .mx .Unlock ()
result := make ([]protocol .ID , 0 , len (r .proto ))
for p := range r .proto {
result = append (result , p )
}
sort .Slice (result , func (i , j int ) bool {
return result [i ] < result [j ]
})
return result
}
func (r *resourceManager ) ListPeers () []peer .ID {
r .mx .Lock ()
defer r .mx .Unlock ()
result := make ([]peer .ID , 0 , len (r .peer ))
for p := range r .peer {
result = append (result , p )
}
sort .Slice (result , func (i , j int ) bool {
return bytes .Compare ([]byte (result [i ]), []byte (result [j ])) < 0
})
return result
}
func (r *resourceManager ) Stat () (result ResourceManagerStat ) {
r .mx .Lock ()
svcs := make ([]*serviceScope , 0 , len (r .svc ))
for _ , svc := range r .svc {
svcs = append (svcs , svc )
}
protos := make ([]*protocolScope , 0 , len (r .proto ))
for _ , proto := range r .proto {
protos = append (protos , proto )
}
peers := make ([]*peerScope , 0 , len (r .peer ))
for _ , peer := range r .peer {
peers = append (peers , peer )
}
r .mx .Unlock ()
result .Peers = make (map [peer .ID ]network .ScopeStat , len (peers ))
for _ , peer := range peers {
result .Peers [peer .peer ] = peer .Stat ()
}
result .Protocols = make (map [protocol .ID ]network .ScopeStat , len (protos ))
for _ , proto := range protos {
result .Protocols [proto .proto ] = proto .Stat ()
}
result .Services = make (map [string ]network .ScopeStat , len (svcs ))
for _ , svc := range svcs {
result .Services [svc .service ] = svc .Stat ()
}
result .Transient = r .transient .Stat ()
result .System = r .system .Stat ()
return result
}
func (r *resourceManager ) GetConnLimit () int {
return r .limits .GetSystemLimits ().GetConnTotalLimit ()
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .