github.com/pf-qiu/concourse/v6@v6.7.3-0.20201207032516-1f455d73275f/atc/metric/gauge.go (about) 1 package metric 2 3 import "sync/atomic" 4 5 type Gauge struct { 6 cur int64 7 max int64 8 } 9 10 func (c *Gauge) Inc() { 11 cur := atomic.AddInt64(&c.cur, 1) 12 13 for { 14 max := atomic.LoadInt64(&c.max) 15 if cur > max { 16 if atomic.CompareAndSwapInt64(&c.max, max, cur) { 17 break 18 } 19 } else { 20 break 21 } 22 } 23 } 24 25 func (c *Gauge) Set(val int64) { 26 for { 27 max := atomic.LoadInt64(&c.max) 28 if val > max { 29 if atomic.CompareAndSwapInt64(&c.max, max, val) { 30 break 31 } 32 } else { 33 break 34 } 35 } 36 } 37 38 func (c *Gauge) Dec() { 39 atomic.AddInt64(&c.cur, -1) 40 } 41 42 func (c *Gauge) Max() float64 { 43 cur := atomic.LoadInt64(&c.cur) 44 max := atomic.SwapInt64(&c.max, -1) 45 46 if max == -1 { 47 // no call to .Inc has occurred since last call to .Max; 48 // highest value must be the current value 49 return float64(cur) 50 } 51 52 return float64(max) 53 }