github.com/yandex/pandora@v0.5.32/lib/monitoring/instance.go (about) 1 package monitoring 2 3 import ( 4 "expvar" 5 "strconv" 6 "sync" 7 ) 8 9 const defaultInstCapacity = 10000 10 11 func NewInstanceTracker(name string) *InstanceTracker { 12 v := &InstanceTracker{ids: make(map[int]struct{}, defaultInstCapacity)} 13 expvar.Publish(name, v) 14 return v 15 } 16 17 type InstanceTracker struct { 18 mu sync.Mutex 19 ids map[int]struct{} 20 max int 21 } 22 23 func (u *InstanceTracker) String() string { 24 u.mu.Lock() 25 defer u.mu.Unlock() 26 return strconv.Itoa(len(u.ids)) 27 } 28 29 func (u *InstanceTracker) OnStart(id int) { 30 u.mu.Lock() 31 defer u.mu.Unlock() 32 u.ids[id] = struct{}{} 33 u.max = max(u.max, len(u.ids)) 34 } 35 36 func (u *InstanceTracker) OnFinish(id int) { 37 u.mu.Lock() 38 defer u.mu.Unlock() 39 delete(u.ids, id) 40 } 41 42 func (u *InstanceTracker) Flush() int { 43 u.mu.Lock() 44 defer u.mu.Unlock() 45 res := u.max 46 u.max = len(u.ids) 47 return res 48 }