forked from bitdabbler/fluent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
309 lines (258 loc) · 7.62 KB
/
client.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
package fluent
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"sync"
"time"
"github.com/bitdabbler/backoff"
)
type worker struct {
*ClientOptions
id int
conn net.Conn
addr string
wg *sync.WaitGroup
sendCh chan *Encoder
}
// Client represents a Fluent client. If using multiple concurrent client
// workers, then the Client could considered a Fluent client pool, as each
// worker maintains an independent connection to the server.
type Client struct {
opts *ClientOptions
host string
workers []*worker
wg *sync.WaitGroup
sendCh chan *Encoder
}
// New creates a new Fluent client and connects to the Fluent server
// immediately, returning an error if it is unable to establish the connection.
func NewClient(host string, opts *ClientOptions) (*Client, error) {
return NewClientContext(context.Background(), host, opts)
}
// NewClientContext creates a new Fluent client and connects to the Fluent
// server immediately, returning an error if it is unable to establish an
// initial connection. Context is passed to `Connect()`. This can be used to
// cancel the `Connect` operation, or set a global deadline for connecting.
func NewClientContext(ctx context.Context, host string, opts *ClientOptions) (*Client, error) {
c, err := newClient(host, opts)
if err != nil {
return nil, err
}
if opts.SkipEagerDial {
return c, nil
}
// eagerly establish server connections from each worker
for i := 0; i < opts.Concurrency; i++ {
err = errors.Join(err, c.workers[i].tryConnect(ctx, opts.MaxEagerDialTries))
if err != nil {
// will drop the client, so eagerly close open conns
for j := 0; j < i; j++ {
c.workers[j].conn.Close()
}
return nil, err
}
}
return c, nil
}
func newClient(host string, opts *ClientOptions) (*Client, error) {
if len(host) == 0 {
return nil, errors.New("valid host required")
}
if opts == nil {
opts = DefaultClientOptions()
} else {
opts.resolve()
}
c := &Client{
opts: opts,
host: host,
workers: make([]*worker, opts.Concurrency),
wg: &sync.WaitGroup{},
sendCh: make(chan *Encoder, opts.QueueDepth),
}
// compose addr to format used by dialers
addr := fmt.Sprintf("%s:%d", host, opts.Port)
// add workers and track concurrency
c.wg.Add(opts.Concurrency)
for i := 0; i < c.opts.Concurrency; i++ {
c.workers[i] = &worker{
ClientOptions: opts,
id: i + 1,
addr: addr,
wg: c.wg,
sendCh: c.sendCh,
}
go c.workers[i].run()
}
return c, nil
}
func (w *worker) tryConnect(ctx context.Context, maxAttempts int) error {
w.debug("attempting to connect to Fluent server\n")
b, err := backoff.New(
backoff.WithInitialDelay(0),
backoff.WithExponentialLimit(time.Second*20),
)
if err != nil {
return err
}
i := 0
for {
i++
err = w.connect(ctx)
if err == nil {
w.debug("successfully connected to Fluent server\n")
return nil
}
w.debug("failed to connect to Fluent server on attempt %d: %v\n", i, err)
if maxAttempts > 0 && i > maxAttempts {
break
}
b.Sleep()
}
return fmt.Errorf("failed connect to Fluent server; maxAttempts reached: %d: %w", maxAttempts, err)
}
func (w *worker) connect(ctx context.Context) error {
var d net.Dialer
ctx, cancel := context.WithTimeout(ctx, w.DialTimeout)
defer cancel()
w.debug("dialing Fluent server at %s over %s\n", w.addr, w.Network)
switch w.Network {
case "tcp":
conn, err := d.DialContext(ctx, "tcp", w.addr)
if err != nil {
return fmt.Errorf("failed to dial server: addr: %s: network: %s: %w", w.addr, w.Network, err)
}
w.conn = conn
case "tls":
tlsDialer := tls.Dialer{
NetDialer: &d,
Config: &tls.Config{InsecureSkipVerify: w.InsecureSkipVerify},
}
conn, err := tlsDialer.DialContext(ctx, "tcp", w.addr)
if err != nil {
return fmt.Errorf("failed to dial Fluent server at %s over protocol %s: %w", w.addr, w.Network, err)
}
w.conn = conn
case "udp":
conn, err := d.DialContext(ctx, "udp", w.addr)
if err != nil {
return fmt.Errorf("failed to dial Fluent server at %s over protocol %s: %w", w.addr, w.Network, err)
}
w.conn = conn
default:
return fmt.Errorf("unsupported Fluent client transport protocol: %s", w.Network)
}
return nil
}
func (w *worker) run() {
// loop until the fan-in messageCh closes
for enc := range w.sendCh {
reconnectloop:
for {
// nil when (a) using lazy conns, (b) after broken pipe tear down
if w.conn == nil {
w.debug("reconnectloop: not connected to server\n")
// ignoring this error because with 0 (infinite) retries, this
// won't return until the conn is established and err == nil
w.tryConnect(context.Background(), 0)
}
// write to the server; retry if recoverable
for i := 0; i < w.MaxWriteTries; i++ {
if w.WriteTimeout > 0 {
w.conn.SetWriteDeadline(time.Now().Add(w.WriteTimeout))
}
_, err := w.conn.Write(enc.Bytes())
if err == nil {
break reconnectloop
}
// only consider timeouts potentially recoverable
if ne, ok := err.(net.Error); !(ok && ne.Timeout()) {
w.reportError("failed to Write message: unrecoverable error: %v\n", err)
break
}
w.debug("failed to Write message: attempt %d: recoverable error: %v\n", i, err)
}
// either non-recoverable error or we exhausted maxWriteTries
w.debug("broken pipe detected; tearing down connection")
err := w.conn.Close()
if err != nil {
w.reportError("error closing broken connection: %v", err)
}
w.conn = nil
}
// successfully wrote the bytes out to the server
enc.Free()
}
w.debug("closing net.Conn and returning from worker goroutine")
// if using lazy connections and the channel is closed before any write
// requests are pushed into it, then the conn could still be nil
if w.conn != nil {
w.conn.Close()
}
w.wg.Done()
}
// Send places the log payload Encoder into the write queue.
//
// This operation is sync/blocking operation when:
// - the queueDepth is 0, or
// - the queue is full and dropIfQueueIsFull is false
//
// This operation is async/non-blocking when:
// - queueDepth > 0, and
// - the queue is not full, or dropIfQueueIsFull is true
//
// The payload should NOT include the Fluent `option` field, which the Client
// is responsible for adding if necessary.
func (c *Client) Send(enc *Encoder) {
if c.opts.DropIfQueueFull {
select {
case c.sendCh <- enc:
default:
c.debug("full buffer: dropping write request: queue depth: %d", c.opts.QueueDepth)
}
} else {
// otherwise block if the queue is full
c.sendCh <- enc
}
}
// Shutdown is used to support graceful shutdown. It closes the write queue
// channel, so any further calls to Send* methods will panic. Shutdown blocks
// until the write queue is fully drained and all worker goroutines have
// stopped, or the context expires, whichever occurs first.
func (c *Client) Shutdown(ctx context.Context) error {
close(c.sendCh)
c.debug("message send queue closed; writing out previously enqueued messages")
doneCh := make(chan error, 1)
go func() {
c.wg.Wait()
close(doneCh)
}()
select {
case <-ctx.Done():
return ctx.Err()
case <-doneCh:
c.debug("message send queue successfully drained")
return nil
}
}
// internal logging helpers:
func (c *Client) debug(format string, args ...any) {
if !c.opts.Verbose {
return
}
InternalLogger().Printf(format, args...)
}
func (w *worker) debug(format string, args ...any) {
if !w.Verbose {
return
}
args = append([]any{w.id}, args...)
InternalLogger().Printf("worker %d: "+format, args...)
}
func (w *worker) reportError(format string, args ...any) {
args = append([]any{w.id}, args...)
InternalLogger().Printf("worker %d: "+format, args...)
}