diff --git a/net/portmapper/portmapper.go b/net/portmapper/portmapper.go index 3fde487cd..e42dd62bf 100644 --- a/net/portmapper/portmapper.go +++ b/net/portmapper/portmapper.go @@ -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 }