bytes_counter.go 552 B

12345678910111213141516171819202122232425262728
  1. package h2mux
  2. import (
  3. "sync/atomic"
  4. )
  5. type AtomicCounter struct {
  6. count uint64
  7. }
  8. func NewAtomicCounter(initCount uint64) *AtomicCounter {
  9. return &AtomicCounter{count: initCount}
  10. }
  11. func (c *AtomicCounter) IncrementBy(number uint64) {
  12. atomic.AddUint64(&c.count, number)
  13. }
  14. // Count returns the current value of counter and reset it to 0
  15. func (c *AtomicCounter) Count() uint64 {
  16. return atomic.SwapUint64(&c.count, 0)
  17. }
  18. // Value returns the current value of counter
  19. func (c *AtomicCounter) Value() uint64 {
  20. return atomic.LoadUint64(&c.count)
  21. }