gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/gauge_float64.go (about)

     1  // Copyright 2018 The aquachain Authors
     2  // This file is part of the aquachain library.
     3  //
     4  // The aquachain library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The aquachain library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the aquachain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package metrics
    18  
    19  import "sync"
    20  
    21  // GaugeFloat64s hold a float64 value that can be set arbitrarily.
    22  type GaugeFloat64 interface {
    23  	Snapshot() GaugeFloat64
    24  	Update(float64)
    25  	Value() float64
    26  }
    27  
    28  // GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a
    29  // new StandardGaugeFloat64.
    30  func GetOrRegisterGaugeFloat64(name string, r Registry) GaugeFloat64 {
    31  	if nil == r {
    32  		r = DefaultRegistry
    33  	}
    34  	return r.GetOrRegister(name, NewGaugeFloat64()).(GaugeFloat64)
    35  }
    36  
    37  // NewGaugeFloat64 constructs a new StandardGaugeFloat64.
    38  func NewGaugeFloat64() GaugeFloat64 {
    39  	if !Enabled {
    40  		return NilGaugeFloat64{}
    41  	}
    42  	return &StandardGaugeFloat64{
    43  		value: 0.0,
    44  	}
    45  }
    46  
    47  // NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64.
    48  func NewRegisteredGaugeFloat64(name string, r Registry) GaugeFloat64 {
    49  	c := NewGaugeFloat64()
    50  	if nil == r {
    51  		r = DefaultRegistry
    52  	}
    53  	r.Register(name, c)
    54  	return c
    55  }
    56  
    57  // NewFunctionalGauge constructs a new FunctionalGauge.
    58  func NewFunctionalGaugeFloat64(f func() float64) GaugeFloat64 {
    59  	if !Enabled {
    60  		return NilGaugeFloat64{}
    61  	}
    62  	return &FunctionalGaugeFloat64{value: f}
    63  }
    64  
    65  // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
    66  func NewRegisteredFunctionalGaugeFloat64(name string, r Registry, f func() float64) GaugeFloat64 {
    67  	c := NewFunctionalGaugeFloat64(f)
    68  	if nil == r {
    69  		r = DefaultRegistry
    70  	}
    71  	r.Register(name, c)
    72  	return c
    73  }
    74  
    75  // GaugeFloat64Snapshot is a read-only copy of another GaugeFloat64.
    76  type GaugeFloat64Snapshot float64
    77  
    78  // Snapshot returns the snapshot.
    79  func (g GaugeFloat64Snapshot) Snapshot() GaugeFloat64 { return g }
    80  
    81  // Update panics.
    82  func (GaugeFloat64Snapshot) Update(float64) {
    83  	panic("Update called on a GaugeFloat64Snapshot")
    84  }
    85  
    86  // Value returns the value at the time the snapshot was taken.
    87  func (g GaugeFloat64Snapshot) Value() float64 { return float64(g) }
    88  
    89  // NilGauge is a no-op Gauge.
    90  type NilGaugeFloat64 struct{}
    91  
    92  // Snapshot is a no-op.
    93  func (NilGaugeFloat64) Snapshot() GaugeFloat64 { return NilGaugeFloat64{} }
    94  
    95  // Update is a no-op.
    96  func (NilGaugeFloat64) Update(v float64) {}
    97  
    98  // Value is a no-op.
    99  func (NilGaugeFloat64) Value() float64 { return 0.0 }
   100  
   101  // StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses
   102  // sync.Mutex to manage a single float64 value.
   103  type StandardGaugeFloat64 struct {
   104  	mutex sync.Mutex
   105  	value float64
   106  }
   107  
   108  // Snapshot returns a read-only copy of the gauge.
   109  func (g *StandardGaugeFloat64) Snapshot() GaugeFloat64 {
   110  	return GaugeFloat64Snapshot(g.Value())
   111  }
   112  
   113  // Update updates the gauge's value.
   114  func (g *StandardGaugeFloat64) Update(v float64) {
   115  	g.mutex.Lock()
   116  	defer g.mutex.Unlock()
   117  	g.value = v
   118  }
   119  
   120  // Value returns the gauge's current value.
   121  func (g *StandardGaugeFloat64) Value() float64 {
   122  	g.mutex.Lock()
   123  	defer g.mutex.Unlock()
   124  	return g.value
   125  }
   126  
   127  // FunctionalGaugeFloat64 returns value from given function
   128  type FunctionalGaugeFloat64 struct {
   129  	value func() float64
   130  }
   131  
   132  // Value returns the gauge's current value.
   133  func (g FunctionalGaugeFloat64) Value() float64 {
   134  	return g.value()
   135  }
   136  
   137  // Snapshot returns the snapshot.
   138  func (g FunctionalGaugeFloat64) Snapshot() GaugeFloat64 { return GaugeFloat64Snapshot(g.Value()) }
   139  
   140  // Update panics.
   141  func (FunctionalGaugeFloat64) Update(float64) {
   142  	panic("Update called on a FunctionalGaugeFloat64")
   143  }