counter.go 433 B

12345678910111213141516171819202122232425262728293031323334
  1. package metrics
  2. import "sync/atomic"
  3. type Counter interface {
  4. Clear()
  5. Count() int64
  6. Dec(int64)
  7. Inc(int64)
  8. }
  9. type counter struct {
  10. count int64
  11. }
  12. func NewCounter() Counter {
  13. return &counter{0}
  14. }
  15. func (c *counter) Clear() {
  16. c.count = 0
  17. }
  18. func (c *counter) Count() int64 {
  19. return c.count
  20. }
  21. func (c *counter) Dec(i int64) {
  22. atomic.AddInt64(&c.count, -i)
  23. }
  24. func (c *counter) Inc(i int64) {
  25. atomic.AddInt64(&c.count, i)
  26. }