net/portmapper: convert UPnP metrics to new syncs.Map.LoadOrInit method

Simplify UPnP error metrics by using the new syncs.Map.LoadOrInit method.

Updates #cleanup

Signed-off-by: Val <valerie@tailscale.com>
This commit is contained in:
Val 2023-10-07 09:14:24 +02:00 committed by Andrew Dunham
parent 8326fdd60f
commit 19e5f242e0

View File

@ -25,10 +25,10 @@
"tailscale.com/net/netmon"
"tailscale.com/net/netns"
"tailscale.com/net/sockstats"
"tailscale.com/syncs"
"tailscale.com/types/logger"
"tailscale.com/types/nettype"
"tailscale.com/util/clientmetric"
"tailscale.com/util/mak"
)
// DebugKnobs contains debug configuration that can be provided when creating a
@ -1028,18 +1028,10 @@ func (c *Client) Probe(ctx context.Context) (res ProbeResult, err error) {
// UPnP error metric that's keyed by code; lazily registered on first read
var (
metricUPnPErrorsByCodeMu sync.Mutex
metricUPnPErrorsByCode map[int]*clientmetric.Metric
metricUPnPErrorsByCode syncs.Map[string, *clientmetric.Metric]
)
func getUPnPErrorsMetric(code int) *clientmetric.Metric {
metricUPnPErrorsByCodeMu.Lock()
defer metricUPnPErrorsByCodeMu.Unlock()
mm := metricUPnPErrorsByCode[code]
if mm != nil {
return mm
}
// Metric names cannot contain a hyphen, so we handle negative numbers
// by prefixing the name with a "minus_".
var codeStr string
@ -1049,7 +1041,6 @@ func getUPnPErrorsMetric(code int) *clientmetric.Metric {
codeStr = fmt.Sprintf("portmap_upnp_errors_with_code_%d", code)
}
mm = clientmetric.NewCounter(codeStr)
mak.Set(&metricUPnPErrorsByCode, code, mm)
mm, _ := metricUPnPErrorsByCode.LoadOrInit(codeStr, func() *clientmetric.Metric { return clientmetric.NewCounter(codeStr) })
return mm
}