-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathostream.go
223 lines (200 loc) · 4.88 KB
/
ostream.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
package nq
import (
"context"
"errors"
"fmt"
"math/rand"
"net"
"sync"
"syscall"
"time"
)
// ostream is an output Pub's stream with asynchronous send loop
type ostream struct {
*PubOpts
addr net.Addr
mu sync.Mutex
e encoder
frontBuf *ringBuf // used by callers
backBuf *ringBuf // used by async send loop
// metrics
*pubMetrics
bufferedMsgs float64
bufferedBytes float64
}
// newOStream constructor
func newOStream(addr net.Addr, opts *PubOpts, p *pubMetrics) *ostream {
os := &ostream{
PubOpts: opts,
addr: addr,
frontBuf: &ringBuf{},
backBuf: &ringBuf{},
pubMetrics: p,
}
return os
}
// Post a message into the outgoing queue.
func (os *ostream) Post(b []byte) error {
os.mu.Lock()
defer os.mu.Unlock()
if err := os.e.EncodeTo(os.frontBuf, b); err != nil {
out(os.Printf, "failed to write: %s", err)
os.lostMsgs.Inc()
os.lostBytes.Add(float64(len(b)))
return err
}
os.bufferedMsgs++
os.bufferedBytes += float64(len(b))
// Do not let send buffer to grow indefinitely
for os.frontBuf.Len() > MaxBuffer {
size, err := decodeSize(os.frontBuf)
if err != nil {
panic("nanoq: failed to decode payload size from the send buffer: " + err.Error())
}
if err := os.frontBuf.Discard(size); err != nil {
panic("nanoq: failed to discard bytes from the send buffer: " + err.Error())
}
os.bufferedMsgs--
os.bufferedBytes -= float64(size)
}
return nil
}
// Work runs the background send loop.
// It blocks execution unil either ctx is done, no messages after KeepaliveTimeout published, or an unrecoverable error occurs.
func (os *ostream) Work(ctx context.Context) error {
var (
conn net.Conn
err error
gotMsgAt = time.Now()
flushTick = time.NewTicker(os.FlushFrequency)
)
defer func() {
closeConn(os.Printf, conn, "to")
flushTick.Stop()
}()
// (re-)connect loop
for {
if conn, err = os.dial(ctx); err != nil {
return fmt.Errorf("nanoq: failed to dial %q: %w", os.addr.String(), err)
}
out(os.Printf, "connected to %s", conn.RemoteAddr().String())
RETRY:
// send loop
for {
select {
case <-flushTick.C:
var (
bufferedBytes float64
bufferedMsgs float64
)
// flip front and back buffers
os.backBuf.Reset()
os.mu.Lock()
os.frontBuf, os.backBuf = os.backBuf, os.frontBuf
bufferedBytes = os.bufferedBytes
os.bufferedBytes = 0
bufferedMsgs = os.bufferedMsgs
os.bufferedMsgs = 0
os.mu.Unlock()
if os.backBuf.Len() == 0 {
// no new messages
if time.Since(gotMsgAt) > os.KeepaliveTimeout {
return nil
}
continue
}
gotMsgAt = time.Now()
// flush write buffer
if err := func() error {
if os.WriteTimeout > 0 {
if err := conn.SetWriteDeadline(time.Now().Add(os.WriteTimeout)); err != nil {
return err
}
}
_, err := os.backBuf.WriteTo(conn)
return err
}(); err != nil {
out(os.Printf, "failed to send: %s", err)
os.lostMsgs.Add(bufferedMsgs)
os.lostBytes.Add(bufferedBytes)
if temporary(err) {
closeConn(os.Printf, conn, "to")
break RETRY
}
return err
}
os.sentMsgs.Add(bufferedMsgs)
os.sentBytes.Add(bufferedBytes)
case <-ctx.Done():
return ctx.Err()
}
}
os.numReconnects.Inc()
}
}
func (os *ostream) dial(ctx context.Context) (conn net.Conn, err error) {
var (
start = time.Now()
delay time.Duration
)
for elapsed := time.Duration(0); elapsed < os.ConnectTimeout; elapsed = time.Since(start) {
netDialer := net.Dialer{Timeout: os.ConnectTimeout - elapsed}
if conn, err = netDialer.DialContext(ctx, os.addr.Network(), os.addr.String()); err != nil {
if temporary(err) {
if delay = increaseDelay(delay, os.ConnectTimeout-elapsed); delay > 0 {
// sleep before retry
select {
case <-time.After(delay):
continue
case <-ctx.Done():
return nil, ctx.Err()
}
}
}
return nil, err
}
if tcp, ok := conn.(*net.TCPConn); ok {
if err := tcp.SetNoDelay(os.NoDelay); err != nil {
return nil, err
}
}
return conn, nil
}
return nil, err
}
func (os *ostream) Reset() {
os.mu.Lock()
defer os.mu.Unlock()
os.frontBuf.Reset()
os.backBuf.Reset()
os.bufferedMsgs = 0
os.bufferedBytes = 0
}
// increaseDelay to not spam reconnects
func increaseDelay(d time.Duration, max time.Duration) time.Duration {
const (
min = time.Millisecond
minTimes = 2
maxTimes = 10
)
if d < min {
return min
}
// randomize delay to prevent accidental DDoS
d *= time.Duration((minTimes + rand.Intn(maxTimes-minTimes)))
if d > max {
d = max
}
return d
}
// temporary errors can be retried
func temporary(err error) bool {
if errors.Is(err, syscall.ECONNREFUSED) {
// we assume connection was refused because the subscriber is restarting or temporarily unavailable
return true
}
if err, ok := err.(interface{ Temporary() bool }); ok {
return err.Temporary()
}
return false
}