github.com/yandex/pandora@v0.5.32/examples/grpc/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  	hello         atomic.Uint64
    28  	auth200       map[int64]uint64
    29  	auth200Mutex  sync.Mutex
    30  	auth400       atomic.Uint64
    31  	auth500       atomic.Uint64
    32  	list200       map[int64]uint64
    33  	list200Mutex  sync.Mutex
    34  	list400       atomic.Uint64
    35  	list500       atomic.Uint64
    36  	order200      map[int64]uint64
    37  	order200Mutex sync.Mutex
    38  	order400      atomic.Uint64
    39  	order500      atomic.Uint64
    40  }
    41  
    42  func (s *Stats) IncHello() {
    43  	s.hello.Add(1)
    44  }
    45  
    46  func (s *Stats) IncAuth400() {
    47  	s.auth400.Add(1)
    48  }
    49  
    50  func (s *Stats) IncAuth500() {
    51  	s.auth500.Add(1)
    52  }
    53  
    54  func (s *Stats) IncAuth200(userID int64) {
    55  	s.auth200Mutex.Lock()
    56  	s.auth200[userID]++
    57  	s.auth200Mutex.Unlock()
    58  }
    59  
    60  func (s *Stats) IncList400() {
    61  	s.list400.Add(1)
    62  }
    63  
    64  func (s *Stats) IncList500() {
    65  	s.list500.Add(1)
    66  }
    67  
    68  func (s *Stats) IncList200(userID int64) {
    69  	s.list200Mutex.Lock()
    70  	s.list200[userID]++
    71  	s.list200Mutex.Unlock()
    72  }
    73  
    74  func (s *Stats) IncOrder400() {
    75  	s.order400.Add(1)
    76  }
    77  
    78  func (s *Stats) IncOrder500() {
    79  	s.order500.Add(1)
    80  }
    81  
    82  func (s *Stats) IncOrder200(userID int64) {
    83  	s.order200Mutex.Lock()
    84  	s.order200[userID]++
    85  	s.order200Mutex.Unlock()
    86  }
    87  
    88  func (s *Stats) Reset() {
    89  	s.auth200Mutex.Lock()
    90  	s.auth200 = map[int64]uint64{}
    91  	s.auth200Mutex.Unlock()
    92  	s.auth400.Store(0)
    93  	s.auth500.Store(0)
    94  
    95  	s.list200Mutex.Lock()
    96  	s.list200 = map[int64]uint64{}
    97  	s.list200Mutex.Unlock()
    98  	s.list400.Store(0)
    99  	s.list500.Store(0)
   100  
   101  	s.order200Mutex.Lock()
   102  	s.order200 = map[int64]uint64{}
   103  	s.order200Mutex.Unlock()
   104  	s.order400.Store(0)
   105  	s.order500.Store(0)
   106  }