8784: Fix test for latest firefox.
[arvados.git] / services / keepstore / stats_ticker.go
1 package main
2
3 import (
4         "sync"
5         "sync/atomic"
6 )
7
8 type statsTicker struct {
9         Errors   uint64
10         InBytes  uint64
11         OutBytes uint64
12
13         ErrorCodes map[string]uint64 `json:",omitempty"`
14         lock       sync.Mutex
15 }
16
17 // Tick increments each of the given counters by 1 using
18 // atomic.AddUint64.
19 func (s *statsTicker) Tick(counters ...*uint64) {
20         for _, counter := range counters {
21                 atomic.AddUint64(counter, 1)
22         }
23 }
24
25 // TickErr increments the overall error counter, as well as the
26 // ErrorCodes entry for the given errType. If err is nil, TickErr is a
27 // no-op.
28 func (s *statsTicker) TickErr(err error, errType string) {
29         if err == nil {
30                 return
31         }
32         s.Tick(&s.Errors)
33
34         s.lock.Lock()
35         if s.ErrorCodes == nil {
36                 s.ErrorCodes = make(map[string]uint64)
37         }
38         s.ErrorCodes[errType]++
39         s.lock.Unlock()
40 }
41
42 // TickInBytes increments the incoming byte counter by n.
43 func (s *statsTicker) TickInBytes(n uint64) {
44         atomic.AddUint64(&s.InBytes, n)
45 }
46
47 // TickOutBytes increments the outgoing byte counter by n.
48 func (s *statsTicker) TickOutBytes(n uint64) {
49         atomic.AddUint64(&s.OutBytes, n)
50 }