github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/metrics/gauge.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:39</date> 10 //</624450098494967809> 11 12 package metrics 13 14 import "sync/atomic" 15 16 //仪表保持一个可以任意设置的Int64值。 17 type Gauge interface { 18 Snapshot() Gauge 19 Update(int64) 20 Value() int64 21 } 22 23 //GetOrRegisterGauge返回现有仪表或构造并注册 24 //新标准仪表。 25 func GetOrRegisterGauge(name string, r Registry) Gauge { 26 if nil == r { 27 r = DefaultRegistry 28 } 29 return r.GetOrRegister(name, NewGauge).(Gauge) 30 } 31 32 //NewGauge构造了一个新的StandardGauge。 33 func NewGauge() Gauge { 34 if !Enabled { 35 return NilGauge{} 36 } 37 return &StandardGauge{0} 38 } 39 40 //newregisteredgauge构造并注册新的标准仪表。 41 func NewRegisteredGauge(name string, r Registry) Gauge { 42 c := NewGauge() 43 if nil == r { 44 r = DefaultRegistry 45 } 46 r.Register(name, c) 47 return c 48 } 49 50 //NewFunctionalGauge构造了一个新的FunctionalGauge。 51 func NewFunctionalGauge(f func() int64) Gauge { 52 if !Enabled { 53 return NilGauge{} 54 } 55 return &FunctionalGauge{value: f} 56 } 57 58 //NewRegisteredFunctionalGauge构造并注册新的StandardGauge。 59 func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge { 60 c := NewFunctionalGauge(f) 61 if nil == r { 62 r = DefaultRegistry 63 } 64 r.Register(name, c) 65 return c 66 } 67 68 //GaugeSnapshot是另一个仪表的只读副本。 69 type GaugeSnapshot int64 70 71 //快照返回快照。 72 func (g GaugeSnapshot) Snapshot() Gauge { return g } 73 74 //更新恐慌。 75 func (GaugeSnapshot) Update(int64) { 76 panic("Update called on a GaugeSnapshot") 77 } 78 79 //值返回拍摄快照时的值。 80 func (g GaugeSnapshot) Value() int64 { return int64(g) } 81 82 //nilgauge是一个不可操作的量表。 83 type NilGauge struct{} 84 85 //快照是不可操作的。 86 func (NilGauge) Snapshot() Gauge { return NilGauge{} } 87 88 //更新是不可操作的。 89 func (NilGauge) Update(v int64) {} 90 91 //值是不可操作的。 92 func (NilGauge) Value() int64 { return 0 } 93 94 //标准仪表是仪表的标准实现,使用 95 //同步/atomic包以管理单个int64值。 96 type StandardGauge struct { 97 value int64 98 } 99 100 //快照返回仪表的只读副本。 101 func (g *StandardGauge) Snapshot() Gauge { 102 return GaugeSnapshot(g.Value()) 103 } 104 105 //更新更新更新仪表值。 106 func (g *StandardGauge) Update(v int64) { 107 atomic.StoreInt64(&g.value, v) 108 } 109 110 //值返回仪表的当前值。 111 func (g *StandardGauge) Value() int64 { 112 return atomic.LoadInt64(&g.value) 113 } 114 115 //函数仪表从给定函数返回值 116 type FunctionalGauge struct { 117 value func() int64 118 } 119 120 //值返回仪表的当前值。 121 func (g FunctionalGauge) Value() int64 { 122 return g.value() 123 } 124 125 //快照返回快照。 126 func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) } 127 128 //更新恐慌。 129 func (FunctionalGauge) Update(int64) { 130 panic("Update called on a FunctionalGauge") 131 } 132