From 08a8b069ca7255c59c394596b3bbbf4e186ccb69 Mon Sep 17 00:00:00 2001 From: zoushengfu Date: Tue, 5 Apr 2022 23:15:36 +0800 Subject: [PATCH] fix: prevent metrics leak when authenticate fails Broker register metrics name to `registeredMetrics` and then authenticate to broker by socket, when authenticate failed many times, the `registeredMetrics` is slice and will append a metrics name many times --- broker.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/broker.go b/broker.go index e481ad711..d7294512d 100644 --- a/broker.go +++ b/broker.go @@ -32,7 +32,7 @@ type Broker struct { responses chan *responsePromise done chan bool - registeredMetrics []string + registeredMetrics map[string]struct{} incomingByteRate metrics.Meter requestRate metrics.Meter @@ -1683,7 +1683,7 @@ func (b *Broker) registerMetrics() { } func (b *Broker) unregisterMetrics() { - for _, name := range b.registeredMetrics { + for name := range b.registeredMetrics { b.conf.MetricRegistry.Unregister(name) } b.registeredMetrics = nil @@ -1691,19 +1691,28 @@ func (b *Broker) unregisterMetrics() { func (b *Broker) registerMeter(name string) metrics.Meter { nameForBroker := getMetricNameForBroker(name, b) - b.registeredMetrics = append(b.registeredMetrics, nameForBroker) + if b.registeredMetrics == nil { + b.registeredMetrics = map[string]struct{}{} + } + b.registeredMetrics[nameForBroker] = struct{}{} return metrics.GetOrRegisterMeter(nameForBroker, b.conf.MetricRegistry) } func (b *Broker) registerHistogram(name string) metrics.Histogram { nameForBroker := getMetricNameForBroker(name, b) - b.registeredMetrics = append(b.registeredMetrics, nameForBroker) + if b.registeredMetrics == nil { + b.registeredMetrics = map[string]struct{}{} + } + b.registeredMetrics[nameForBroker] = struct{}{} return getOrRegisterHistogram(nameForBroker, b.conf.MetricRegistry) } func (b *Broker) registerCounter(name string) metrics.Counter { nameForBroker := getMetricNameForBroker(name, b) - b.registeredMetrics = append(b.registeredMetrics, nameForBroker) + if b.registeredMetrics == nil { + b.registeredMetrics = map[string]struct{}{} + } + b.registeredMetrics[nameForBroker] = struct{}{} return metrics.GetOrRegisterCounter(nameForBroker, b.conf.MetricRegistry) }