github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/metrics/gauge.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 package metrics 10 11 import "sync/atomic" 12 13 //仪表保持一个可以任意设置的Int64值。 14 type Gauge interface { 15 Snapshot() Gauge 16 Update(int64) 17 Value() int64 18 } 19 20 //GetOrRegisterGauge返回现有仪表或构造并注册 21 //新标准仪表。 22 func GetOrRegisterGauge(name string, r Registry) Gauge { 23 if nil == r { 24 r = DefaultRegistry 25 } 26 return r.GetOrRegister(name, NewGauge).(Gauge) 27 } 28 29 //NewGauge构造了一个新的StandardGauge。 30 func NewGauge() Gauge { 31 if !Enabled { 32 return NilGauge{} 33 } 34 return &StandardGauge{0} 35 } 36 37 //newregisteredgauge构造并注册新的标准仪表。 38 func NewRegisteredGauge(name string, r Registry) Gauge { 39 c := NewGauge() 40 if nil == r { 41 r = DefaultRegistry 42 } 43 r.Register(name, c) 44 return c 45 } 46 47 //NewFunctionalGauge构造了一个新的FunctionalGauge。 48 func NewFunctionalGauge(f func() int64) Gauge { 49 if !Enabled { 50 return NilGauge{} 51 } 52 return &FunctionalGauge{value: f} 53 } 54 55 //NewRegisteredFunctionalGauge构造并注册新的StandardGauge。 56 func NewRegisteredFunctionalGauge(name string, r Registry, f func() int64) Gauge { 57 c := NewFunctionalGauge(f) 58 if nil == r { 59 r = DefaultRegistry 60 } 61 r.Register(name, c) 62 return c 63 } 64 65 //GaugeSnapshot是另一个仪表的只读副本。 66 type GaugeSnapshot int64 67 68 //快照返回快照。 69 func (g GaugeSnapshot) Snapshot() Gauge { return g } 70 71 //更新恐慌。 72 func (GaugeSnapshot) Update(int64) { 73 panic("Update called on a GaugeSnapshot") 74 } 75 76 //值返回拍摄快照时的值。 77 func (g GaugeSnapshot) Value() int64 { return int64(g) } 78 79 //nilgauge是一个不可操作的量表。 80 type NilGauge struct{} 81 82 //快照是不可操作的。 83 func (NilGauge) Snapshot() Gauge { return NilGauge{} } 84 85 //更新是不可操作的。 86 func (NilGauge) Update(v int64) {} 87 88 //值是不可操作的。 89 func (NilGauge) Value() int64 { return 0 } 90 91 //标准仪表是仪表的标准实现,使用 92 //同步/atomic包以管理单个int64值。 93 type StandardGauge struct { 94 value int64 95 } 96 97 //快照返回仪表的只读副本。 98 func (g *StandardGauge) Snapshot() Gauge { 99 return GaugeSnapshot(g.Value()) 100 } 101 102 //更新更新更新仪表值。 103 func (g *StandardGauge) Update(v int64) { 104 atomic.StoreInt64(&g.value, v) 105 } 106 107 //值返回仪表的当前值。 108 func (g *StandardGauge) Value() int64 { 109 return atomic.LoadInt64(&g.value) 110 } 111 112 //函数仪表从给定函数返回值 113 type FunctionalGauge struct { 114 value func() int64 115 } 116 117 //值返回仪表的当前值。 118 func (g FunctionalGauge) Value() int64 { 119 return g.value() 120 } 121 122 //快照返回快照。 123 func (g FunctionalGauge) Snapshot() Gauge { return GaugeSnapshot(g.Value()) } 124 125 //更新恐慌。 126 func (FunctionalGauge) Update(int64) { 127 panic("Update called on a FunctionalGauge") 128 }