-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_client.go
More file actions
132 lines (115 loc) · 2.68 KB
/
batch_client.go
File metadata and controls
132 lines (115 loc) · 2.68 KB
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
package components
import (
"context"
"errors"
"log"
"time"
)
func EnqueueTimeout(timeout time.Duration) func(*BatchClientConfig) {
return func(config *BatchClientConfig) {
config.EnqueueTimeout = timeout
}
}
func FlushInterval(interval time.Duration) func(*BatchClientConfig) {
return func(config *BatchClientConfig) {
config.FlushInterval = interval
}
}
func BatchSize(size int) func(*BatchClientConfig) {
return func(config *BatchClientConfig) {
config.BatchSize = size
}
}
type BatchClientConfig struct {
EnqueueTimeout time.Duration
FlushInterval time.Duration
BatchSize int
}
var _ APIClient = (*BatchClient)(nil)
type BatchClient struct {
buffer chan Entry
client APIClient
cfg BatchClientConfig
}
func NewBatchClient(client APIClient, opts ...func(*BatchClientConfig)) *BatchClient {
cfg := BatchClientConfig{
EnqueueTimeout: 5 * time.Second,
FlushInterval: 5 * time.Second,
BatchSize: 100,
}
for _, opt := range opts {
opt(&cfg)
}
b := &BatchClient{
buffer: make(chan Entry, cfg.BatchSize*2),
client: client,
cfg: cfg,
}
return b
}
func (b *BatchClient) IngestLogs(ctx context.Context, entries []Entry) error {
enqTimeout := time.After(b.cfg.EnqueueTimeout)
for _, entry := range entries {
select {
case b.buffer <- entry:
// Successfully enqueued.
case <-ctx.Done():
return ctx.Err()
case <-enqTimeout:
return errors.New("timeout: buffer is full, cannot enqueue log entry")
}
}
return nil
}
func (b *BatchClient) Run(ctx context.Context) error {
return b.run(ctx)
}
func (b *BatchClient) run(ctx context.Context) error {
ticker := time.NewTicker(b.cfg.FlushInterval)
defer ticker.Stop()
entries := make([]Entry, 0, b.cfg.BatchSize)
for {
select {
case entry := <-b.buffer:
if len(entry.Message) == 0 {
continue
}
entries = append(entries, entry)
if len(entries) >= b.cfg.BatchSize {
b.flush(ctx, entries)
entries = entries[:0]
}
case <-ticker.C:
b.flush(ctx, entries)
entries = entries[:0]
case <-ctx.Done():
b.drainBuffer(&entries)
// Use a new context with timeout for graceful shutdown.
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
b.flush(shutdownCtx, entries)
return ctx.Err()
}
}
}
func (b *BatchClient) drainBuffer(entries *[]Entry) {
for {
select {
case entry := <-b.buffer:
if len(entry.Message) > 0 {
*entries = append(*entries, entry)
}
default:
// Buffer is empty.
return
}
}
}
func (b *BatchClient) flush(ctx context.Context, e []Entry) {
if len(e) == 0 {
return
}
if err := b.client.IngestLogs(ctx, e); err != nil {
log.Printf("failed to publish logs: %v", err)
}
}