-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcounter.go
335 lines (289 loc) · 7.53 KB
/
counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Package counters provides a simple counter, max and min functionalities.
// All counters are kept in CounterBox.
// Library is thread safe.
package counters
import (
"bytes"
"io"
"math"
"net/http"
"os"
"os/signal"
"sort"
"strings"
"sync"
"sync/atomic"
"syscall"
"text/template"
"time"
)
// MaxMinValue is an interface for minima and maxima counters.
type MaxMinValue interface {
// Set allows to update value if necessary.
Set(int)
// Name returns a name of counter.
Name() string
// Value returns a current value.
Value() int64
}
// Counter is an interface for integer increase only counter.
type Counter interface {
// Increment increases counter by one.
Increment() int64
// IncrementBy increases counter by a given number.
IncrementBy(num int) int64
// Decrement decreases counter by one.
Decrement() int64
// DecrementBy decreases counter by a given number.
DecrementBy(num int) int64
// Set sets a specific value.
Set(num int)
// Name returns a name of counter.
Name() string
// Value returns a current value of counter.
Value() int64
}
type Counters interface {
Get(string) Counter
Min(string) MaxMinValue
Max(string) MaxMinValue
WithPrefix(string) Counters
GetCounter(string) Counter
GetMin(string) MaxMinValue
GetMax(string) MaxMinValue
WriteTo(w io.Writer)
Prefix() string
String() string
}
// CounterBox is a main type, it keeps references to all counters
// requested from it.
type CounterBox struct {
counters *sync.Map
min *sync.Map
max *sync.Map
}
// NewCounterBox creates a new object to keep all counters.
func NewCounterBox() *CounterBox {
return &CounterBox{
counters: &sync.Map{},
min: &sync.Map{},
max: &sync.Map{},
}
}
func New() Counters {
return NewCounterBox()
}
// CreateHttpHandler creates a simple handler printing values of all counters.
func (c *CounterBox) CreateHttpHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { c.WriteTo(w) }
}
func (c *CounterBox) Get(name string) Counter {
return c.GetCounter(name)
}
func (c *CounterBox) Min(name string) MaxMinValue {
return c.GetMin(name)
}
func (c *CounterBox) Max(name string) MaxMinValue {
return c.GetMax(name)
}
type prefixed struct {
CounterBox
base *CounterBox
prefix string
}
func (c *CounterBox) WithPrefix(name string) Counters {
return &prefixed{
CounterBox{
counters: &sync.Map{},
min: &sync.Map{},
max: &sync.Map{},
},
c,
name}
}
func (c *CounterBox) Prefix() string {
return ""
}
func (c *prefixed) GetCounter(name string) Counter {
value, _ := c.counters.LoadOrStore(name, c.base.GetCounter(c.prefix+name))
v, _ := value.(Counter)
return v
}
// GetMin returns a minima counter of given name, if doesn't exist than create.
func (c *prefixed) GetMin(name string) MaxMinValue {
value, _ := c.min.LoadOrStore(name, c.base.GetMin(c.prefix+name))
v, _ := value.(MaxMinValue)
return v
}
// GetMax returns a maxima counter of given name, if doesn't exist than create.
func (c *prefixed) GetMax(name string) MaxMinValue {
value, _ := c.max.LoadOrStore(name, c.base.GetMax(c.prefix+name))
v, _ := value.(MaxMinValue)
return v
}
func (c *prefixed) Get(name string) Counter {
return c.GetCounter(name)
}
// GetMin returns a minima counter of given name, if doesn't exist than create.
func (c *prefixed) Min(name string) MaxMinValue {
return c.GetMin(name)
}
// GetMax returns a maxima counter of given name, if doesn't exist than create.
func (c *prefixed) Max(name string) MaxMinValue {
return c.GetMax(name)
}
func (c *prefixed) Prefix() string {
return c.prefix
}
// GetCounter returns a counter of given name, if doesn't exist than create.
func (c *CounterBox) GetCounter(name string) Counter {
value, _ := c.counters.LoadOrStore(name, &counterImpl{name, 0})
v, _ := value.(Counter)
return v
}
// GetMin returns a minima counter of given name, if doesn't exist than create.
func (c *CounterBox) GetMin(name string) MaxMinValue {
value, _ := c.min.LoadOrStore(name, &minImpl{name, math.MaxInt64})
v, _ := value.(MaxMinValue)
return v
}
// GetMax returns a maxima counter of given name, if doesn't exist than create.
func (c *CounterBox) GetMax(name string) MaxMinValue {
value, _ := c.max.LoadOrStore(name, &maxImpl{name, 0})
v, _ := value.(MaxMinValue)
return v
}
var tmpl = template.Must(template.New("main").Parse(`== Counters ==
{{- range .Counters}}
{{.Name}}: {{.Value}}
{{- end}}
== Min values ==
{{- range .Min}}
{{.Name}}: {{.Value}}
{{- end}}
== Max values ==
{{- range .Max}}
{{.Name}}: {{.Value}}
{{- end -}}
`))
func (c *CounterBox) WriteTo(w io.Writer) {
data := &struct {
Counters []Counter
Min []MaxMinValue
Max []MaxMinValue
}{}
c.counters.Range(func(key interface{}, value interface{}) bool {
if value, ok := value.(Counter); ok {
data.Counters = append(data.Counters, value)
}
return true
})
c.min.Range(func(key interface{}, value interface{}) bool {
if value, ok := value.(MaxMinValue); ok {
data.Min = append(data.Min, value)
}
return true
})
c.max.Range(func(key interface{}, value interface{}) bool {
if value, ok := value.(MaxMinValue); ok {
data.Max = append(data.Max, value)
}
return true
})
sort.Slice(data.Counters, func(i, j int) bool { return strings.Compare(data.Counters[i].Name(), data.Counters[j].Name()) < 0 })
sort.Slice(data.Min, func(i, j int) bool { return strings.Compare(data.Min[i].Name(), data.Min[j].Name()) < 0 })
sort.Slice(data.Max, func(i, j int) bool { return strings.Compare(data.Max[i].Name(), data.Max[j].Name()) < 0 })
tmpl.Execute(w, data)
}
func (c *CounterBox) String() string {
buf := &bytes.Buffer{}
c.WriteTo(buf)
return buf.String()
}
type counterImpl struct {
name string
value int64
}
func (c *counterImpl) Increment() int64 {
return atomic.AddInt64(&c.value, 1)
}
func (c *counterImpl) IncrementBy(num int) int64 {
return atomic.AddInt64(&c.value, int64(num))
}
func (c *counterImpl) Decrement() int64 {
return atomic.AddInt64(&c.value, -1)
}
func (c *counterImpl) DecrementBy(num int) int64 {
return atomic.AddInt64(&c.value, -int64(num))
}
func (c *counterImpl) Set(num int) {
atomic.StoreInt64(&c.value, int64(num))
}
func (c *counterImpl) Name() string {
return c.name
}
func (c *counterImpl) Value() int64 {
return atomic.LoadInt64(&c.value)
}
type maxImpl counterImpl
func (m *maxImpl) Set(v int) {
done := false
v64 := int64(v)
for !done {
if o := atomic.LoadInt64(&m.value); v64 > o {
done = atomic.CompareAndSwapInt64(&m.value, o, v64)
} else {
done = true
}
}
}
func (m *maxImpl) Name() string {
return m.name
}
func (m *maxImpl) Value() int64 {
return atomic.LoadInt64(&m.value)
}
type minImpl counterImpl
func (m *minImpl) Set(v int) {
done := false
v64 := int64(v)
for !done {
if o := atomic.LoadInt64(&m.value); v64 < o {
done = atomic.CompareAndSwapInt64(&m.value, o, v64)
} else {
done = true
}
}
}
func (m *minImpl) Name() string {
return m.name
}
func (m *minImpl) Value() int64 {
return atomic.LoadInt64(&m.value)
}
type TrivialLogger interface {
Print(...interface{})
}
func InitCountersOnSignal(logger TrivialLogger, box Counters) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
lastInt := time.Now()
for sig := range sigs {
logger.Print(box.String())
l := time.Now()
if sig == syscall.SIGTERM || l.Sub(lastInt).Seconds() < 1. {
os.Exit(0)
}
lastInt = l
}
}()
}
func LogCountersEvery(logger TrivialLogger, box Counters, d time.Duration) {
go func() {
t := time.NewTicker(d)
for range t.C {
logger.Print(box.String())
}
}()
}