vitess.io/vitess@v0.16.2/go/stats/counter_map.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package stats
    18  
    19  import (
    20  	"sync"
    21  )
    22  
    23  var (
    24  	countersMap = map[string]interface{}{}
    25  	countersMu  sync.RWMutex
    26  )
    27  
    28  // GetOrNewCounter returns a Counter with given name; the functiona either creates the counter
    29  // if it does not exist, or returns a pre-existing one. The function is thread safe.
    30  func GetOrNewCounter(name string, help string) *Counter {
    31  	// first, attempt read lock only
    32  	countersMu.RLock()
    33  	c, ok := countersMap[name]
    34  	countersMu.RUnlock()
    35  	if ok {
    36  		return c.(*Counter)
    37  	}
    38  
    39  	// escalate into write lock
    40  	countersMu.Lock()
    41  	defer countersMu.Unlock()
    42  	// double check because we have released the lock in the interim
    43  	if c, ok := countersMap[name]; ok {
    44  		return c.(*Counter)
    45  	}
    46  	n := NewCounter(name, help)
    47  	countersMap[name] = n
    48  	return n
    49  }
    50  
    51  // GetOrNewGauge returns a Gauge with given name; the functiona either creates the gauge
    52  // if it does not exist, or returns a pre-existing one. The function is thread safe.
    53  func GetOrNewGauge(name string, help string) *Gauge {
    54  	// first, attempt read lock only
    55  	countersMu.RLock()
    56  	c, ok := countersMap[name]
    57  	countersMu.RUnlock()
    58  	if ok {
    59  		return c.(*Gauge)
    60  	}
    61  
    62  	// escalate into write lock
    63  	countersMu.Lock()
    64  	defer countersMu.Unlock()
    65  	// double check because we have released the lock in the interim
    66  	if c, ok := countersMap[name]; ok {
    67  		return c.(*Gauge)
    68  	}
    69  	n := NewGauge(name, help)
    70  	countersMap[name] = n
    71  	return n
    72  }
    73  
    74  // GetOrNewGaugeFloat64 returns a Gauge (float64) with given name; the functiona either creates the gauge
    75  // if it does not exist, or returns a pre-existing one. The function is thread safe.
    76  func GetOrNewGaugeFloat64(name string, help string) *GaugeFloat64 {
    77  	// first, attempt read lock only
    78  	countersMu.RLock()
    79  	c, ok := countersMap[name]
    80  	countersMu.RUnlock()
    81  	if ok {
    82  		return c.(*GaugeFloat64)
    83  	}
    84  
    85  	// escalate into write lock
    86  	countersMu.Lock()
    87  	defer countersMu.Unlock()
    88  	// double check because we have released the lock in the interim
    89  	if c, ok := countersMap[name]; ok {
    90  		return c.(*GaugeFloat64)
    91  	}
    92  	n := NewGaugeFloat64(name, help)
    93  	countersMap[name] = n
    94  	return n
    95  }