From 55f8c599099f478658c29850efe98e36e7bb5c4e Mon Sep 17 00:00:00 2001 From: vyzo Date: Mon, 14 Feb 2022 17:20:12 +0200 Subject: [PATCH] add Allow metrics --- metrics.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- rcmgr.go | 5 ++++ scope.go | 1 + 3 files changed, 73 insertions(+), 2 deletions(-) diff --git a/metrics.go b/metrics.go index 2342d0b..40918fc 100644 --- a/metrics.go +++ b/metrics.go @@ -8,21 +8,38 @@ import ( // MetricsReporter is an interface for collecting metrics from resource manager actions type MetricsReporter interface { + // AllowConn is invoked when opening a connection is allowed + AllowConn(dir network.Direction, usefd bool) // BlockConn is invoked when opening a connection is blocked BlockConn(dir network.Direction, usefd bool) + + // AllowStream is invoked when opening a stream is allowed + AllowStream(p peer.ID, dir network.Direction) // BlockStream is invoked when opening a stream is blocked BlockStream(p peer.ID, dir network.Direction) + + // AllowPeer is invoked when attaching ac onnection to a peer is allowed + AllowPeer(p peer.ID) // BlockPeer is invoked when attaching ac onnection to a peer is blocked BlockPeer(p peer.ID) + + // AllowProtocol is invoked when setting the protocol for a stream is allowed + AllowProtocol(proto protocol.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) + + // AllowPService is invoked when setting the protocol for a stream is allowed + AllowService(svc string) // 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 + // BlockedServicePeer is invoked when setting the service for a stream is blocked at the per service peer scope BlockServicePeer(svc string, p peer.ID) - // BlockMemory is invoked when a memory reservation fails + + // AllowMemory is invoked when a memory reservation is allowed + AllowMemory(size int) + // BlockMemory is invoked when a memory reservation is blocked BlockMemory(size int) } @@ -38,6 +55,14 @@ func WithMetrics(reporter MetricsReporter) Option { } } +func (m *metrics) AllowConn(dir network.Direction, usefd bool) { + if m == nil { + return + } + + m.reporter.AllowConn(dir, usefd) +} + func (m *metrics) BlockConn(dir network.Direction, usefd bool) { if m == nil { return @@ -46,6 +71,14 @@ func (m *metrics) BlockConn(dir network.Direction, usefd bool) { m.reporter.BlockConn(dir, usefd) } +func (m *metrics) AllowStream(p peer.ID, dir network.Direction) { + if m == nil { + return + } + + m.reporter.AllowStream(p, dir) +} + func (m *metrics) BlockStream(p peer.ID, dir network.Direction) { if m == nil { return @@ -54,6 +87,14 @@ func (m *metrics) BlockStream(p peer.ID, dir network.Direction) { m.reporter.BlockStream(p, dir) } +func (m *metrics) AllowPeer(p peer.ID) { + if m == nil { + return + } + + m.reporter.AllowPeer(p) +} + func (m *metrics) BlockPeer(p peer.ID) { if m == nil { return @@ -62,6 +103,14 @@ func (m *metrics) BlockPeer(p peer.ID) { m.reporter.BlockPeer(p) } +func (m *metrics) AllowProtocol(proto protocol.ID) { + if m == nil { + return + } + + m.reporter.AllowProtocol(proto) +} + func (m *metrics) BlockProtocol(proto protocol.ID) { if m == nil { return @@ -78,6 +127,14 @@ func (m *metrics) BlockProtocolPeer(proto protocol.ID, p peer.ID) { m.reporter.BlockProtocolPeer(proto, p) } +func (m *metrics) AllowService(svc string) { + if m == nil { + return + } + + m.reporter.AllowService(svc) +} + func (m *metrics) BlockService(svc string) { if m == nil { return @@ -94,6 +151,14 @@ func (m *metrics) BlockServicePeer(svc string, p peer.ID) { m.reporter.BlockServicePeer(svc, p) } +func (m *metrics) AllowMemory(size int) { + if m == nil { + return + } + + m.reporter.AllowMemory(size) +} + func (m *metrics) BlockMemory(size int) { if m == nil { return diff --git a/rcmgr.go b/rcmgr.go index 3a07227..bebae9b 100644 --- a/rcmgr.go +++ b/rcmgr.go @@ -264,6 +264,7 @@ func (r *resourceManager) OpenConnection(dir network.Direction, usefd bool) (net return nil, err } + r.metrics.AllowConn(dir, usefd) return conn, nil } @@ -279,6 +280,7 @@ func (r *resourceManager) OpenStream(p peer.ID, dir network.Direction) (network. return nil, err } + r.metrics.AllowStream(p, dir) return stream, nil } @@ -526,6 +528,7 @@ func (s *connectionScope) SetPeer(p peer.ID) error { } s.resourceScope.edges = edges + s.rcmgr.metrics.AllowPeer(p) return nil } @@ -583,6 +586,7 @@ func (s *streamScope) SetProtocol(proto protocol.ID) error { } s.resourceScope.edges = edges + s.rcmgr.metrics.AllowProtocol(proto) return nil } @@ -643,6 +647,7 @@ func (s *streamScope) SetService(svc string) error { } s.resourceScope.edges = edges + s.rcmgr.metrics.AllowService(svc) return nil } diff --git a/scope.go b/scope.go index 9d42207..507c3b7 100644 --- a/scope.go +++ b/scope.go @@ -254,6 +254,7 @@ func (s *resourceScope) ReserveMemory(size int, prio uint8) error { } s.trace.ReserveMemory(s.name, prio, int64(size), s.rc.memory) + s.metrics.AllowMemory(size) return nil }