github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/metrics/gauge_float64.go (about)

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