gitlab.com/aquachain/aquachain@v1.17.16-rc3.0.20221018032414-e3ddf1e1c055/common/metrics/counter.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/atomic" 20 21 // Counters hold an int64 value that can be incremented and decremented. 22 type Counter interface { 23 Clear() 24 Count() int64 25 Dec(int64) 26 Inc(int64) 27 Snapshot() Counter 28 } 29 30 // GetOrRegisterCounter returns an existing Counter or constructs and registers 31 // a new StandardCounter. 32 func GetOrRegisterCounter(name string, r Registry) Counter { 33 if nil == r { 34 r = DefaultRegistry 35 } 36 return r.GetOrRegister(name, NewCounter).(Counter) 37 } 38 39 // NewCounter constructs a new StandardCounter. 40 func NewCounter() Counter { 41 if !Enabled { 42 return NilCounter{} 43 } 44 return &StandardCounter{0} 45 } 46 47 // NewRegisteredCounter constructs and registers a new StandardCounter. 48 func NewRegisteredCounter(name string, r Registry) Counter { 49 c := NewCounter() 50 if nil == r { 51 r = DefaultRegistry 52 } 53 r.Register(name, c) 54 return c 55 } 56 57 // CounterSnapshot is a read-only copy of another Counter. 58 type CounterSnapshot int64 59 60 // Clear panics. 61 func (CounterSnapshot) Clear() { 62 panic("Clear called on a CounterSnapshot") 63 } 64 65 // Count returns the count at the time the snapshot was taken. 66 func (c CounterSnapshot) Count() int64 { return int64(c) } 67 68 // Dec panics. 69 func (CounterSnapshot) Dec(int64) { 70 panic("Dec called on a CounterSnapshot") 71 } 72 73 // Inc panics. 74 func (CounterSnapshot) Inc(int64) { 75 panic("Inc called on a CounterSnapshot") 76 } 77 78 // Snapshot returns the snapshot. 79 func (c CounterSnapshot) Snapshot() Counter { return c } 80 81 // NilCounter is a no-op Counter. 82 type NilCounter struct{} 83 84 // Clear is a no-op. 85 func (NilCounter) Clear() {} 86 87 // Count is a no-op. 88 func (NilCounter) Count() int64 { return 0 } 89 90 // Dec is a no-op. 91 func (NilCounter) Dec(i int64) {} 92 93 // Inc is a no-op. 94 func (NilCounter) Inc(i int64) {} 95 96 // Snapshot is a no-op. 97 func (NilCounter) Snapshot() Counter { return NilCounter{} } 98 99 // StandardCounter is the standard implementation of a Counter and uses the 100 // sync/atomic package to manage a single int64 value. 101 type StandardCounter struct { 102 count int64 103 } 104 105 // Clear sets the counter to zero. 106 func (c *StandardCounter) Clear() { 107 atomic.StoreInt64(&c.count, 0) 108 } 109 110 // Count returns the current count. 111 func (c *StandardCounter) Count() int64 { 112 return atomic.LoadInt64(&c.count) 113 } 114 115 // Dec decrements the counter by the given amount. 116 func (c *StandardCounter) Dec(i int64) { 117 atomic.AddInt64(&c.count, -i) 118 } 119 120 // Inc increments the counter by the given amount. 121 func (c *StandardCounter) Inc(i int64) { 122 atomic.AddInt64(&c.count, i) 123 } 124 125 // Snapshot returns a read-only copy of the counter. 126 func (c *StandardCounter) Snapshot() Counter { 127 return CounterSnapshot(c.Count()) 128 }