cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
h2mux/bytes_counter.go
27lines · modecode
| 1 | package h2mux |
| 2 | |
| 3 | import ( |
| 4 | "sync/atomic" |
| 5 | ) |
| 6 | |
| 7 | type AtomicCounter struct { |
| 8 | count uint64 |
| 9 | } |
| 10 | |
| 11 | func NewAtomicCounter(initCount uint64) *AtomicCounter { |
| 12 | return &AtomicCounter{count: initCount} |
| 13 | } |
| 14 | |
| 15 | func (c *AtomicCounter) IncrementBy(number uint64) { |
| 16 | atomic.AddUint64(&c.count, number) |
| 17 | } |
| 18 | |
| 19 | // Count returns the current value of counter and reset it to 0 |
| 20 | func (c *AtomicCounter) Count() uint64 { |
| 21 | return atomic.SwapUint64(&c.count, 0) |
| 22 | } |
| 23 | |
| 24 | // Value returns the current value of counter |
| 25 | func (c *AtomicCounter) Value() uint64 { |
| 26 | return atomic.LoadUint64(&c.count) |
| 27 | } |