-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
165 lines (143 loc) · 3.54 KB
/
worker.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
package statsd
import (
"errors"
"time"
"github.com/cactus/go-statsd-client/v5/statsd"
)
// worker
type worker struct {
statsdClient statsd.StatSender
input chan workerJob
stop chan struct{}
}
func newWorker(input chan workerJob, statsdClient statsd.StatSender) *worker {
return &worker{
statsdClient: statsdClient,
input: input,
stop: make(chan struct{}),
}
}
func (w *worker) startReceivingMetric() {
go w.pullMetric()
}
func (w *worker) stopReceivingMetric() {
w.stop <- struct{}{}
}
func (w *worker) pullMetric() {
for {
select {
case m := <-w.input:
_ = m(w.statsdClient)
case <-w.stop:
return
}
}
}
// workerStatSender
type workerStatSender struct {
statsdClient statsd.StatSender
workers []*worker
input chan workerJob
}
func newWorkerStatSender(workers int, bufferSize int, statsdClient statsd.StatSender) *workerStatSender {
if bufferSize <= 0 {
bufferSize = workers * 10
}
ret := &workerStatSender{
statsdClient: statsdClient,
input: make(chan workerJob, bufferSize),
}
for i := 0; i < workers; i++ {
w := newWorker(ret.input, statsdClient)
ret.workers = append(ret.workers, w)
}
return ret
}
func (w *workerStatSender) Start() error {
if len(w.workers) == 0 {
return errors.New("no workers")
}
for _, w := range w.workers {
w.startReceivingMetric()
}
return nil
}
func (w *workerStatSender) Stop() error {
for _, w := range w.workers {
w.stopReceivingMetric()
}
w.workers = nil
w.flush()
return nil
}
func (w *workerStatSender) flush() {
for {
select {
case m, ok := <-w.input:
if ok {
_ = m(w.statsdClient)
} else {
// channel closed
break
}
default:
return
}
}
}
func (w *workerStatSender) Inc(s string, i int64, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.Inc(s, i, f, tag...)
}
return nil
}
func (w *workerStatSender) Dec(s string, i int64, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.Dec(s, i, f, tag...)
}
return nil
}
func (w *workerStatSender) Gauge(s string, i int64, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.Gauge(s, i, f, tag...)
}
return nil
}
func (w *workerStatSender) GaugeDelta(s string, i int64, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.GaugeDelta(s, i, f, tag...)
}
return nil
}
func (w *workerStatSender) Timing(s string, i int64, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.Timing(s, i, f, tag...)
}
return nil
}
func (w *workerStatSender) TimingDuration(s string, duration time.Duration, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.TimingDuration(s, duration, f, tag...)
}
return nil
}
func (w *workerStatSender) Set(s string, s2 string, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.Set(s, s2, f, tag...)
}
return nil
}
func (w *workerStatSender) SetInt(s string, i int64, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.SetInt(s, i, f, tag...)
}
return nil
}
func (w *workerStatSender) Raw(s string, s2 string, f float32, tag ...statsd.Tag) error {
w.input <- func(sender statsd.StatSender) error {
return sender.Raw(s, s2, f, tag...)
}
return nil
}
// workerJob
type workerJob func(statsd.StatSender) error