github.com/dominant-strategies/go-quai@v0.28.2/metrics/gauge.go (about)

     1  package metrics
     2  
     3  import "sync/atomic"
     4  
     5  // Gauges hold an int64 value that can be set arbitrarily.
     6  type Gauge interface {
     7  	Snapshot() Gauge
     8  	Update(int64)
     9  	Dec(int64)
    10  	Inc(int64)
    11  	Value() int64
    12  }
    13  
    14  // GetOrRegisterGauge returns an existing Gauge or constructs and registers a
    15  // new StandardGauge.
    16  func GetOrRegisterGauge(name string, r Registry) Gauge {
    17  	if nil == r {
    18  		r = DefaultRegistry
    19  	}
    20  	return r.GetOrRegister(name, NewGauge).(Gauge)
    21  }
    22  
    23  // NewGauge constructs a new StandardGauge.
    24  func NewGauge() Gauge {
    25  	if !Enabled {
    26  		return NilGauge{}
    27  	}
    28  	return &StandardGauge{0}
    29  }
    30  
    31  // NewRegisteredGauge constructs and registers a new StandardGauge.
    32  func NewRegisteredGauge(name string, r Registry) Gauge {
    33  	c := NewGauge()
    34  	if nil == r {
    35  		r = DefaultRegistry
    36  	}
    37  	r.Register(name, c)
    38  	return c
    39  }
    40  
    41  // NewFunctionalGauge constructs a new FunctionalGauge.
    42  func NewFunctionalGauge(f func() int64) Gauge {
    43  	if !Enabled {
    44  		return NilGauge{}
    45  	}
    46  	return &FunctionalGauge{value: f}
    47  }
    48  
    49  // NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
    50  func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge {
    51  	c := NewFunctionalGauge(f)
    52  	if nil == r {
    53  		r = DefaultRegistry
    54  	}
    55  	r.Register(name, c)
    56  	return c
    57  }
    58  
    59  // GaugeSnapshot is a read-only copy of another Gauge.
    60  type GaugeSnapshot int64
    61  
    62  // Snapshot returns the snapshot.
    63  func (g GaugeSnapshot) Snapshot() Gauge { return g }
    64  
    65  // Update panics.
    66  func (GaugeSnapshot) Update(int64) {
    67  	panic("Update called on a GaugeSnapshot")
    68  }
    69  
    70  // Dec panics.
    71  func (GaugeSnapshot) Dec(int64) {
    72  	panic("Dec called on a GaugeSnapshot")
    73  }
    74  
    75  // Inc panics.
    76  func (GaugeSnapshot) Inc(int64) {
    77  	panic("Inc called on a GaugeSnapshot")
    78  }
    79  
    80  // Value returns the value at the time the snapshot was taken.
    81  func (g GaugeSnapshot) Value() int64 { return int64(g) }
    82  
    83  // NilGauge is a no-op Gauge.
    84  type NilGauge struct{}
    85  
    86  // Snapshot is a no-op.
    87  func (NilGauge) Snapshot() Gauge { return NilGauge{} }
    88  
    89  // Update is a no-op.
    90  func (NilGauge) Update(v int64) {}
    91  
    92  // Dec is a no-op.
    93  func (NilGauge) Dec(i int64) {}
    94  
    95  // Inc is a no-op.
    96  func (NilGauge) Inc(i int64) {}
    97  
    98  // Value is a no-op.
    99  func (NilGauge) Value() int64 { return 0 }
   100  
   101  // StandardGauge is the standard implementation of a Gauge and uses the
   102  // sync/atomic package to manage a single int64 value.
   103  type StandardGauge struct {
   104  	value int64
   105  }
   106  
   107  // Snapshot returns a read-only copy of the gauge.
   108  func (g *StandardGauge) Snapshot() Gauge {
   109  	return GaugeSnapshot(g.Value())
   110  }
   111  
   112  // Update updates the gauge's value.
   113  func (g *StandardGauge) Update(v int64) {
   114  	atomic.StoreInt64(&g.value, v)
   115  }
   116  
   117  // Value returns the gauge's current value.
   118  func (g *StandardGauge) Value() int64 {
   119  	return atomic.LoadInt64(&g.value)
   120  }
   121  
   122  // Dec decrements the gauge's current value by the given amount.
   123  func (g *StandardGauge) Dec(i int64) {
   124  	atomic.AddInt64(&g.value, -i)
   125  }
   126  
   127  // Inc increments the gauge's current value by the given amount.
   128  func (g *StandardGauge) Inc(i int64) {
   129  	atomic.AddInt64(&g.value, i)
   130  }
   131  
   132  // FunctionalGauge returns value from given function
   133  type FunctionalGauge struct {
   134  	value func() int64
   135  }
   136  
   137  // Value returns the gauge's current value.
   138  func (g FunctionalGauge) Value() int64 {
   139  	return g.value()
   140  }
   141  
   142  // Snapshot returns the snapshot.
   143  func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) }
   144  
   145  // Update panics.
   146  func (FunctionalGauge) Update(int64) {
   147  	panic("Update called on a FunctionalGauge")
   148  }
   149  
   150  // Dec panics.
   151  func (FunctionalGauge) Dec(int64) {
   152  	panic("Dec called on a FunctionalGauge")
   153  }
   154  
   155  // Inc panics.
   156  func (FunctionalGauge) Inc(int64) {
   157  	panic("Inc called on a FunctionalGauge")
   158  }