counter.go 501 B

12345678910111213141516171819202122232425262728293031
  1. package log
  2. import "sync"
  3. type counter struct {
  4. mu sync.RWMutex
  5. seen map[string]int
  6. }
  7. func newCounter() *counter {
  8. return &counter{seen: map[string]int{}}
  9. }
  10. func (ctr *counter) count(key string) int {
  11. ctr.mu.RLock()
  12. defer ctr.mu.RUnlock()
  13. return ctr.seen[key]
  14. }
  15. func (ctr *counter) delete(key string) {
  16. ctr.mu.Lock()
  17. delete(ctr.seen, key)
  18. ctr.mu.Unlock()
  19. }
  20. func (ctr *counter) increment(key string) int {
  21. ctr.mu.Lock()
  22. defer ctr.mu.Unlock()
  23. ctr.seen[key]++
  24. return ctr.seen[key]
  25. }