2022-02-12 00:24:53 +08:00
|
|
|
package rcmgr
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p-core/protocol"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MetricsReporter is an interface for collecting metrics from resource manager actions
|
|
|
|
type MetricsReporter interface {
|
2022-02-12 19:52:47 +08:00
|
|
|
// BlockConn is invoked when opening a connection is blocked
|
|
|
|
BlockConn(dir network.Direction, usefd bool)
|
|
|
|
// BlockStream is invoked when opening a stream is blocked
|
|
|
|
BlockStream(p peer.ID, dir network.Direction)
|
|
|
|
// BlockPeer is invoked when attaching ac onnection to a peer is blocked
|
|
|
|
BlockPeer(p peer.ID)
|
|
|
|
// BlockProtocol is invoked when setting the protocol for a stream is blocked
|
|
|
|
BlockProtocol(proto protocol.ID)
|
|
|
|
// BlockedProtocolPeer is invoekd when setting the protocol for a stream is blocked at the per protocol peer scope
|
|
|
|
BlockProtocolPeer(proto protocol.ID, p peer.ID)
|
|
|
|
// BlockPService is invoked when setting the protocol for a stream is blocked
|
|
|
|
BlockService(svc string)
|
|
|
|
// BlockedServicePeer is invoekd when setting the service for a stream is blocked at the per service peer scope
|
|
|
|
BlockServicePeer(svc string, p peer.ID)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type metrics struct {
|
|
|
|
reporter MetricsReporter
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithMetrics is a resource manager option to enable metrics collection
|
|
|
|
func WithMetrics(reporter MetricsReporter) Option {
|
|
|
|
return func(r *resourceManager) error {
|
|
|
|
r.metrics = &metrics{reporter: reporter}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockConn(dir network.Direction, usefd bool) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockConn(dir, usefd)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockStream(p peer.ID, dir network.Direction) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockStream(p, dir)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockPeer(p peer.ID) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockPeer(p)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockProtocol(proto protocol.ID) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockProtocol(proto)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockProtocolPeer(proto protocol.ID, p peer.ID) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockProtocolPeer(proto, p)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockService(svc string) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockService(svc)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
func (m *metrics) BlockServicePeer(svc string, p peer.ID) {
|
2022-02-12 00:24:53 +08:00
|
|
|
if m == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-12 19:52:47 +08:00
|
|
|
m.reporter.BlockServicePeer(svc, p)
|
2022-02-12 00:24:53 +08:00
|
|
|
}
|