github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/metrics/counter.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  //</624450097995845632>
    11  
    12  package metrics
    13  
    14  import (
    15  	"sync/atomic"
    16  )
    17  
    18  //计数器保存一个可以递增和递减的Int64值。
    19  type Counter interface {
    20  	Clear()
    21  	Count() int64
    22  	Dec(int64)
    23  	Inc(int64)
    24  	Snapshot() Counter
    25  }
    26  
    27  //GetOrRegisterCounter返回现有计数器或构造和注册
    28  //一个新的标准计数器。
    29  func GetOrRegisterCounter(name string, r Registry) Counter {
    30  	if nil == r {
    31  		r = DefaultRegistry
    32  	}
    33  	return r.GetOrRegister(name, NewCounter).(Counter)
    34  }
    35  
    36  //GetOrRegisterCounterForced返回现有计数器或构造并注册
    37  //新计数器,无论全局开关是否启用。
    38  //一旦计数器对
    39  //允许垃圾收集。
    40  func GetOrRegisterCounterForced(name string, r Registry) Counter {
    41  	if nil == r {
    42  		r = DefaultRegistry
    43  	}
    44  	return r.GetOrRegister(name, NewCounterForced).(Counter)
    45  }
    46  
    47  //NewCounter构造一个新的标准计数器。
    48  func NewCounter() Counter {
    49  	if !Enabled {
    50  		return NilCounter{}
    51  	}
    52  	return &StandardCounter{0}
    53  }
    54  
    55  //NewCounterForced构造新的StandardCounter并返回它,无论
    56  //全局开关是否启用。
    57  func NewCounterForced() Counter {
    58  	return &StandardCounter{0}
    59  }
    60  
    61  //NewRegisteredCounter构造并注册新的标准计数器。
    62  func NewRegisteredCounter(name string, r Registry) Counter {
    63  	c := NewCounter()
    64  	if nil == r {
    65  		r = DefaultRegistry
    66  	}
    67  	r.Register(name, c)
    68  	return c
    69  }
    70  
    71  //newregisteredcounterforce构造并注册新的标准计数器
    72  //并启动Goroutine,无论全局开关是否启用。
    73  //一旦计数器对
    74  //允许垃圾收集。
    75  func NewRegisteredCounterForced(name string, r Registry) Counter {
    76  	c := NewCounterForced()
    77  	if nil == r {
    78  		r = DefaultRegistry
    79  	}
    80  	r.Register(name, c)
    81  	return c
    82  }
    83  
    84  //CounterSnapshot是另一个计数器的只读副本。
    85  type CounterSnapshot int64
    86  
    87  //清晰的恐慌。
    88  func (CounterSnapshot) Clear() {
    89  	panic("Clear called on a CounterSnapshot")
    90  }
    91  
    92  //count返回快照拍摄时的计数。
    93  func (c CounterSnapshot) Count() int64 { return int64(c) }
    94  
    95  //十足恐慌。
    96  func (CounterSnapshot) Dec(int64) {
    97  	panic("Dec called on a CounterSnapshot")
    98  }
    99  
   100  //公司恐慌。
   101  func (CounterSnapshot) Inc(int64) {
   102  	panic("Inc called on a CounterSnapshot")
   103  }
   104  
   105  //快照返回快照。
   106  func (c CounterSnapshot) Snapshot() Counter { return c }
   107  
   108  //nilcounter是一个禁止操作的计数器。
   109  type NilCounter struct{}
   110  
   111  //清除是不可操作的。
   112  func (NilCounter) Clear() {}
   113  
   114  //计数是不允许的。
   115  func (NilCounter) Count() int64 { return 0 }
   116  
   117  //DEC是NO-OP。
   118  func (NilCounter) Dec(i int64) {}
   119  
   120  //公司是一个NO-OP。
   121  func (NilCounter) Inc(i int64) {}
   122  
   123  //快照是不可操作的。
   124  func (NilCounter) Snapshot() Counter { return NilCounter{} }
   125  
   126  //StandardCounter是计数器的标准实现,它使用
   127  //同步/atomic包以管理单个int64值。
   128  type StandardCounter struct {
   129  	count int64
   130  }
   131  
   132  //清除将计数器设置为零。
   133  func (c *StandardCounter) Clear() {
   134  	atomic.StoreInt64(&c.count, 0)
   135  }
   136  
   137  //count返回当前计数。
   138  func (c *StandardCounter) Count() int64 {
   139  	return atomic.LoadInt64(&c.count)
   140  }
   141  
   142  //DEC按给定的数量递减计数器。
   143  func (c *StandardCounter) Dec(i int64) {
   144  	atomic.AddInt64(&c.count, -i)
   145  }
   146  
   147  //inc将计数器递增给定的数量。
   148  func (c *StandardCounter) Inc(i int64) {
   149  	atomic.AddInt64(&c.count, i)
   150  }
   151  
   152  //快照返回计数器的只读副本。
   153  func (c *StandardCounter) Snapshot() Counter {
   154  	return CounterSnapshot(c.Count())
   155  }
   156