From ef596aed9b9b7b27e6a1d8e15a5a5ce9abf413b5 Mon Sep 17 00:00:00 2001 From: Andrew Dunham Date: Wed, 25 Oct 2023 20:29:08 -0400 Subject: [PATCH] net/portmapper: avoid alloc in getUPnPErrorsMetric Updates #cleanup Signed-off-by: Andrew Dunham Change-Id: Iea558024c038face24cc46584421998d10f13a66 --- net/portmapper/portmapper.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/net/portmapper/portmapper.go b/net/portmapper/portmapper.go index e42dd62bf..3bee9e5f9 100644 --- a/net/portmapper/portmapper.go +++ b/net/portmapper/portmapper.go @@ -1028,19 +1028,21 @@ var ( // UPnP error metric that's keyed by code; lazily registered on first read var ( - metricUPnPErrorsByCode syncs.Map[string, *clientmetric.Metric] + metricUPnPErrorsByCode syncs.Map[int, *clientmetric.Metric] ) func getUPnPErrorsMetric(code int) *clientmetric.Metric { - // Metric names cannot contain a hyphen, so we handle negative numbers - // by prefixing the name with a "minus_". - var codeStr string - if code < 0 { - codeStr = fmt.Sprintf("portmap_upnp_errors_with_code_minus_%d", -code) - } else { - codeStr = fmt.Sprintf("portmap_upnp_errors_with_code_%d", code) - } + mm, _ := metricUPnPErrorsByCode.LoadOrInit(code, func() *clientmetric.Metric { + // Metric names cannot contain a hyphen, so we handle negative + // numbers by prefixing the name with a "minus_". + var codeStr string + if code < 0 { + codeStr = fmt.Sprintf("portmap_upnp_errors_with_code_minus_%d", -code) + } else { + codeStr = fmt.Sprintf("portmap_upnp_errors_with_code_%d", code) + } - mm, _ := metricUPnPErrorsByCode.LoadOrInit(codeStr, func() *clientmetric.Metric { return clientmetric.NewCounter(codeStr) }) + return clientmetric.NewCounter(codeStr) + }) return mm }