github.com/yandex/pandora@v0.5.32/examples/http/server/stats.go (about) 1 package server 2 3 import ( 4 "sync" 5 "sync/atomic" 6 ) 7 8 func newStats(capacity int) *Stats { 9 stats := Stats{ 10 Auth200: make(map[int64]uint64, capacity), 11 auth200Mutex: sync.Mutex{}, 12 auth400: atomic.Uint64{}, 13 auth500: atomic.Uint64{}, 14 List200: make(map[int64]uint64, capacity), 15 list200Mutex: sync.Mutex{}, 16 list400: atomic.Uint64{}, 17 list500: atomic.Uint64{}, 18 Order200: make(map[int64]uint64, capacity), 19 order200Mutex: sync.Mutex{}, 20 order400: atomic.Uint64{}, 21 order500: atomic.Uint64{}, 22 } 23 return &stats 24 } 25 26 type Stats struct { 27 Auth200 map[int64]uint64 28 auth200Mutex sync.Mutex 29 auth400 atomic.Uint64 30 auth500 atomic.Uint64 31 List200 map[int64]uint64 32 list200Mutex sync.Mutex 33 list400 atomic.Uint64 34 list500 atomic.Uint64 35 Order200 map[int64]uint64 36 order200Mutex sync.Mutex 37 order400 atomic.Uint64 38 order500 atomic.Uint64 39 } 40 41 func (s *Stats) IncAuth400() { 42 s.auth400.Add(1) 43 } 44 45 func (s *Stats) IncAuth500() { 46 s.auth500.Add(1) 47 } 48 49 func (s *Stats) IncAuth200(userID int64) { 50 s.auth200Mutex.Lock() 51 s.Auth200[userID]++ 52 s.auth200Mutex.Unlock() 53 } 54 55 func (s *Stats) IncList400() { 56 s.list400.Add(1) 57 } 58 59 func (s *Stats) IncList500() { 60 s.list500.Add(1) 61 } 62 63 func (s *Stats) IncList200(userID int64) { 64 s.list200Mutex.Lock() 65 s.List200[userID]++ 66 s.list200Mutex.Unlock() 67 } 68 69 func (s *Stats) IncOrder400() { 70 s.order400.Add(1) 71 } 72 73 func (s *Stats) IncOrder500() { 74 s.order500.Add(1) 75 } 76 77 func (s *Stats) IncOrder200(userID int64) { 78 s.order200Mutex.Lock() 79 s.Order200[userID]++ 80 s.order200Mutex.Unlock() 81 } 82 83 func (s *Stats) Reset() { 84 s.auth200Mutex.Lock() 85 s.Auth200 = map[int64]uint64{} 86 s.auth200Mutex.Unlock() 87 s.auth400.Store(0) 88 s.auth500.Store(0) 89 90 s.list200Mutex.Lock() 91 s.List200 = map[int64]uint64{} 92 s.list200Mutex.Unlock() 93 s.list400.Store(0) 94 s.list500.Store(0) 95 96 s.order200Mutex.Lock() 97 s.Order200 = map[int64]uint64{} 98 s.order200Mutex.Unlock() 99 s.order400.Store(0) 100 s.order500.Store(0) 101 }