-
Notifications
You must be signed in to change notification settings - Fork 18
/
loops.go
167 lines (141 loc) · 3.86 KB
/
loops.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
package statsd
/*
Copyright (c) 2017 Andrey Smirnov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import (
"context"
"net"
"sync/atomic"
"time"
)
// flushLoop makes sure metrics are flushed every flushInterval
func (t *transport) flushLoop(flushInterval time.Duration) {
var flushC <-chan time.Time
if flushInterval > 0 {
flushTicker := time.NewTicker(flushInterval)
defer flushTicker.Stop()
flushC = flushTicker.C
}
for {
select {
case <-t.shutdown:
t.bufLock.Lock()
if len(t.buf) > 0 {
t.flushBuf(len(t.buf))
}
t.bufLock.Unlock()
close(t.sendQueue)
return
case <-flushC:
t.bufLock.Lock()
if len(t.buf) > 0 {
t.flushBuf(len(t.buf))
}
t.bufLock.Unlock()
}
}
}
// sendLoop handles packet delivery over UDP and periodic reconnects
func (t *transport) sendLoop(addr string, network string, reconnectInterval, retryTimeout time.Duration, log SomeLogger) {
var (
sock net.Conn
err error
reconnectC <-chan time.Time
)
defer t.shutdownWg.Done()
if reconnectInterval > 0 {
reconnectTicker := time.NewTicker(reconnectInterval)
defer reconnectTicker.Stop()
reconnectC = reconnectTicker.C
}
RECONNECT:
// Attempt to connect
sock, err = func() (net.Conn, error) {
// Dial with context which is aborted when client is shut down
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
go func() {
select {
case <-t.shutdown:
ctxCancel()
case <-ctx.Done():
}
}()
var d net.Dialer
return d.DialContext(ctx, network, addr)
}()
if err != nil {
log.Printf("[STATSD] Error connecting to server: %s", err)
goto WAIT
}
for {
select {
case buf, ok := <-t.sendQueue:
// Get a buffer from the queue
if !ok {
_ = sock.Close() // nolint: gosec
return
}
if len(buf) > 0 {
// cut off \n in the end
_, err := sock.Write(buf[0 : len(buf)-1])
if err != nil {
log.Printf("[STATSD] Error writing to socket: %s", err)
_ = sock.Close() // nolint: gosec
goto WAIT
}
}
// return buffer to the pool
select {
case t.bufPool <- buf:
default:
// pool is full, let GC handle the buf
}
case <-reconnectC:
_ = sock.Close() // nolint: gosec
goto RECONNECT
}
}
WAIT:
// Wait for a while
select {
case <-time.After(retryTimeout):
goto RECONNECT
case <-t.shutdown:
}
// drain send queue waiting for flush loops to terminate
for range t.sendQueue { //nolint:revive
}
}
// reportLoop reports periodically number of packets lost
func (t *transport) reportLoop(reportInterval time.Duration, log SomeLogger) {
defer t.shutdownWg.Done()
reportTicker := time.NewTicker(reportInterval)
defer reportTicker.Stop()
for {
select {
case <-t.shutdown:
return
case <-reportTicker.C:
lostPeriod := atomic.SwapInt64(&t.lostPacketsPeriod, 0)
if lostPeriod > 0 {
log.Printf("[STATSD] %d packets lost (overflow)", lostPeriod)
}
}
}
}