From ba50a25f0dcf4195af00d1abef89944ebd98a5e0 Mon Sep 17 00:00:00 2001 From: rene <41963722+renaynay@users.noreply.github.com> Date: Fri, 5 Apr 2024 16:44:20 +0200 Subject: [PATCH] fix(p2p/discovery): Unregister metrics callback on close (#3280) Self explanatory --- share/p2p/discovery/discovery.go | 2 +- share/p2p/discovery/metrics.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/share/p2p/discovery/discovery.go b/share/p2p/discovery/discovery.go index c40c979d16..65925819d7 100644 --- a/share/p2p/discovery/discovery.go +++ b/share/p2p/discovery/discovery.go @@ -114,7 +114,7 @@ func (d *Discovery) Start(context.Context) error { func (d *Discovery) Stop(context.Context) error { d.cancel() - return nil + return d.metrics.close() } // Peers provides a list of discovered peers in the given topic. diff --git a/share/p2p/discovery/metrics.go b/share/p2p/discovery/metrics.go index c91e68491d..5847fbdd90 100644 --- a/share/p2p/discovery/metrics.go +++ b/share/p2p/discovery/metrics.go @@ -37,6 +37,8 @@ type metrics struct { advertise metric.Int64Counter // attributes: failed[bool] peerAdded metric.Int64Counter peerRemoved metric.Int64Counter + + clientReg metric.Registration } // WithMetrics turns on metric collection in discoery. @@ -107,13 +109,22 @@ func initMetrics(d *Discovery) (*metrics, error) { observer.ObserveInt64(backOffSize, int64(d.connector.Size())) return nil } - _, err = meter.RegisterCallback(callback, peersAmount, backOffSize) + + metrics.clientReg, err = meter.RegisterCallback(callback, peersAmount, backOffSize) if err != nil { return nil, fmt.Errorf("registering metrics callback: %w", err) } + return metrics, nil } +func (m *metrics) close() error { + if m == nil { + return nil + } + return m.clientReg.Unregister() +} + func (m *metrics) observeFindPeers(ctx context.Context, isEnoughPeers bool) { if m == nil { return