Debian Codegraph @

memmetrics/counter.go

raw
1 package memmetrics
2
3 import (
4 "fmt"
5 "time"
6
7 "github.com/vulcand/oxy/v2/internal/holsterv4/clock"
8 )
9
10 type rcOption func(*RollingCounter) error
11
12 // RollingCounter Calculates in memory failure rate of an endpoint using rolling window of a predefined size.
13 type RollingCounter struct {
14 resolution time.Duration
15 values []int
16 countedBuckets int // how many samples in different buckets have we collected so far
17 lastBucket int // last recorded bucket
18 lastUpdated clock.Time
19 }
20
21 // NewCounter creates a counter with fixed amount of buckets that are rotated every resolution period.
22 // E.g. 10 buckets with 1 second means that every new second the bucket is refreshed, so it maintains 10 seconds rolling window.
23 // By default, creates a bucket with 10 buckets and 1 second resolution.
24 func NewCounter(buckets int, resolution time.Duration, options ...rcOption) (*RollingCounter, error) {
25 if buckets <= 0 {
26 return nil, fmt.Errorf("buckets should be >= 0")
27 }
28 if resolution < clock.Second {
29 return nil, fmt.Errorf("resolution should be larger than a second")
30 }
31
32 rc := &RollingCounter{
33 lastBucket: -1,
34 resolution: resolution,
35
36 values: make([]int, buckets),
37 }
38
39 for _, o := range options {
40 if err := o(rc); err != nil {
41 return nil, err
42 }
43 }
44
45 return rc, nil
46 }
47
48 // Append appends a counter.
49 func (c *RollingCounter) Append(o *RollingCounter) error {
50 c.Inc(int(o.Count()))
51 return nil
52 }
53
54 // Clone clones a counter.
55 func (c *RollingCounter) Clone() *RollingCounter {
56 c.cleanup()
57 other := &RollingCounter{
58 resolution: c.resolution,
59 values: make([]int, len(c.values)),
60 lastBucket: c.lastBucket,
61 lastUpdated: c.lastUpdated,
62 }
63 copy(other.values, c.values)
64 return other
65 }
66
67 // Reset resets a counter.
68 func (c *RollingCounter) Reset() {
69 c.lastBucket = -1
70 c.countedBuckets = 0
71 c.lastUpdated = clock.Time{}
72 for i := range c.values {
73 c.values[i] = 0
74 }
75 }
76
77 // CountedBuckets gets counted buckets.
78 func (c *RollingCounter) CountedBuckets() int {
79 return c.countedBuckets
80 }
81
82 // Count counts.
83 func (c *RollingCounter) Count() int64 {
84 c.cleanup()
85 return c.sum()
86 }
87
88 // Resolution gets resolution.
89 func (c *RollingCounter) Resolution() time.Duration {
90 return c.resolution
91 }
92
93 // Buckets gets buckets.
94 func (c *RollingCounter) Buckets() int {
95 return len(c.values)
96 }
97
98 // WindowSize gets windows size.
99 func (c *RollingCounter) WindowSize() time.Duration {
100 return time.Duration(len(c.values)) * c.resolution
101 }
102
103 // Inc increments counter.
104 func (c *RollingCounter) Inc(v int) {
105 c.cleanup()
106 c.incBucketValue(v)
107 }
108
109 func (c *RollingCounter) incBucketValue(v int) {
110 now := clock.Now().UTC()
111 bucket := c.getBucket(now)
112 c.values[bucket] += v
113 c.lastUpdated = now
114 // Update usage stats if we haven't collected enough data
115 if c.countedBuckets < len(c.values) {
116 // Only update if we have advanced to the next bucket and not incremented the value
117 // in the current bucket.
118 if c.lastBucket != bucket {
119 c.lastBucket = bucket
120 c.countedBuckets++
121 }
122 }
123 }
124
125 // Returns the number in the moving window bucket that this slot occupies.
126 func (c *RollingCounter) getBucket(t time.Time) int {
127 return int(t.Truncate(c.resolution).Unix() % int64(len(c.values)))
128 }
129
130 // Reset buckets that were not updated.
131 func (c *RollingCounter) cleanup() {
132 now := clock.Now().UTC()
133 for i := 0; i < len(c.values); i++ {
134 now = now.Add(time.Duration(-1*i) * c.resolution)
135 if now.Truncate(c.resolution).After(c.lastUpdated.Truncate(c.resolution)) {
136 c.values[c.getBucket(now)] = 0
137 } else {
138 break
139 }
140 }
141 }
142
143 func (c *RollingCounter) sum() int64 {
144 out := int64(0)
145 for _, v := range c.values {
146 out += int64(v)
147 }
148 return out
149 }