-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
207 lines (169 loc) · 4.88 KB
/
buffer.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
package client
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
)
type BufferedClientConfig struct {
Endpoint string
AccessKeyID string
AccessKeySecret string
ClientId string
Encoding string // json or msgpack, default is json
NoCompression bool // set to true to turn off compression
CompressionAlgo string // default is gzip
RetryTimeIntervalInitial time.Duration // retry interval initial, default is 100ms
RetryTimeIntervalMax time.Duration // retry interval max, default is 5m
MaxMessagesPerBatch int
MaxDurationPerBatch time.Duration
MaxConcurrency int
Logger Logger
}
type BufferedClient struct {
conf BufferedClientConfig
client *Client
inMsgs chan *Message
outBatches chan *Messages
nextBatchID int64
closed int64
closeCh chan interface{}
batchingLoopDie chan interface{}
sendingLoopDie chan interface{}
}
func NewBufferedClient(config BufferedClientConfig) (*BufferedClient, error) {
clientConfig := Config{
Endpoint: config.Endpoint,
AccessKeyID: config.AccessKeyID,
AccessKeySecret: config.AccessKeySecret,
ClientId: config.ClientId,
Encoding: config.Encoding,
NoCompression: config.NoCompression,
CompressionAlgo: config.CompressionAlgo,
RetryTimeIntervalInitial: config.RetryTimeIntervalInitial,
RetryTimeIntervalMax: config.RetryTimeIntervalMax,
Logger: config.Logger,
}
client, err := NewClient(clientConfig)
if err != nil {
return nil, err
}
if config.MaxMessagesPerBatch == 0 {
config.MaxMessagesPerBatch = 2000
}
if config.MaxDurationPerBatch == 0 {
config.MaxDurationPerBatch = 50 * time.Millisecond
}
if config.MaxConcurrency == 0 {
config.MaxConcurrency = 10
}
bc := &BufferedClient{
conf: config,
client: client,
inMsgs: make(chan *Message),
outBatches: make(chan *Messages),
closed: 0,
closeCh: make(chan interface{}),
batchingLoopDie: make(chan interface{}),
sendingLoopDie: make(chan interface{}),
}
go bc.batchingLoop()
go bc.sendingLoop()
return bc, nil
}
func (bc *BufferedClient) Send(ctx context.Context, message *Message) error {
if message == nil {
return fmt.Errorf("message cannot be nil")
}
if atomic.LoadInt64(&bc.closed) == 1 {
return fmt.Errorf("client was closed")
}
select {
case <-ctx.Done():
return ctx.Err()
case bc.inMsgs <- message:
}
return nil
}
func (bc *BufferedClient) Close(ctx context.Context) error {
bc.conf.Logger.Debug("calling close client")
if atomic.CompareAndSwapInt64(&bc.closed, 0, 1) {
close(bc.closeCh)
}
select {
case <-bc.sendingLoopDie:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
func (bc *BufferedClient) batchingLoop() {
defer bc.conf.Logger.Debug("batching loop exited")
defer close(bc.batchingLoopDie)
timer := time.NewTimer(bc.conf.MaxDurationPerBatch)
defer timer.Stop()
newBatch := func() *Messages {
batchID := fmt.Sprintf("b-%d-%d", time.Now().UnixMilli(), bc.nextBatchID)
bc.nextBatchID++
return &Messages{BatchId: batchID}
}
b := newBatch()
for {
select {
case msg := <-bc.inMsgs:
b.Messages = append(b.Messages, *msg)
if len(b.Messages) >= bc.conf.MaxMessagesPerBatch {
bc.conf.Logger.WithField("batchId", b.BatchId).Debug("seal batch for sending (number of message reach limit)")
bc.outBatches <- b
b = newBatch()
timer.Reset(bc.conf.MaxDurationPerBatch)
}
case <-timer.C:
if len(b.Messages) > 0 {
bc.conf.Logger.WithField("batchId", b.BatchId).Debug("seal batch for sending (batch live duration reach limit)")
bc.outBatches <- b
b = newBatch()
}
case <-bc.closeCh:
if len(b.Messages) > 0 {
bc.conf.Logger.WithField("batchId", b.BatchId).Debug("seal batch for sending (client is closing)")
bc.outBatches <- b
}
return
}
}
}
func (bc *BufferedClient) sendingLoop() {
defer bc.conf.Logger.Debug("sending loop exited")
defer close(bc.sendingLoopDie)
sema := make(chan interface{}, bc.conf.MaxConcurrency)
wg := sync.WaitGroup{}
for {
select {
case b := <-bc.outBatches:
sema <- nil
wg.Add(1)
go func() {
defer func() { <-sema; wg.Done() }()
bc.sendBatch(b)
}()
case <-bc.batchingLoopDie:
wg.Wait()
return
}
}
}
func (bc *BufferedClient) sendBatch(b *Messages) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
start := time.Now()
bc.conf.Logger.WithField("batchId", b.BatchId).WithField("messages", len(b.Messages)).Debug("sending batch")
err := bc.client.Collect(ctx, b)
if err != nil {
bc.conf.Logger.WithField("batchId", b.BatchId).WithField("error", err.Error()).Error("failed to send batch")
return
}
elapsed := time.Since(start)
bc.conf.Logger.WithField("batchId", b.BatchId).WithField("elapsed", elapsed.String()).Debug("batch successfully sent")
}