@@ -16,9 +16,10 @@ import (
16
16
)
17
17
18
18
type batch struct {
19
- ctx context.Context
20
- req request.Request
21
- done multiDone
19
+ ctx context.Context
20
+ req request.Request
21
+ done multiDone
22
+ created time.Time
22
23
}
23
24
24
25
type batcherSettings [K any ] struct {
@@ -38,7 +39,7 @@ type defaultBatcher struct {
38
39
stopWG sync.WaitGroup
39
40
currentBatchMu sync.Mutex
40
41
currentBatch * batch
41
- timer * time.Timer
42
+ ticker * time.Ticker
42
43
shutdownCh chan struct {}
43
44
}
44
45
@@ -62,12 +63,6 @@ func newDefaultBatcher(bCfg BatchConfig, bSet batcherSettings[request.Request])
62
63
}
63
64
}
64
65
65
- func (qb * defaultBatcher ) resetTimer () {
66
- if qb .cfg .FlushTimeout > 0 {
67
- qb .timer .Reset (qb .cfg .FlushTimeout )
68
- }
69
- }
70
-
71
66
func (qb * defaultBatcher ) Consume (ctx context.Context , req request.Request , done Done ) {
72
67
qb .currentBatchMu .Lock ()
73
68
@@ -91,11 +86,11 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
91
86
// Do not flush the last item and add it to the current batch.
92
87
reqList = reqList [:len (reqList )- 1 ]
93
88
qb .currentBatch = & batch {
94
- ctx : ctx ,
95
- req : lastReq ,
96
- done : multiDone {done },
89
+ ctx : ctx ,
90
+ req : lastReq ,
91
+ done : multiDone {done },
92
+ created : time .Now (),
97
93
}
98
- qb .resetTimer ()
99
94
}
100
95
101
96
qb .currentBatchMu .Unlock ()
@@ -146,11 +141,11 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
146
141
// Do not flush the last item and add it to the current batch.
147
142
reqList = reqList [:len (reqList )- 1 ]
148
143
qb .currentBatch = & batch {
149
- ctx : ctx ,
150
- req : lastReq ,
151
- done : multiDone {done },
144
+ ctx : ctx ,
145
+ req : lastReq ,
146
+ done : multiDone {done },
147
+ created : time .Now (),
152
148
}
153
- qb .resetTimer ()
154
149
}
155
150
}
156
151
@@ -172,8 +167,8 @@ func (qb *defaultBatcher) startTimeBasedFlushingGoroutine() {
172
167
select {
173
168
case <- qb .shutdownCh :
174
169
return
175
- case <- qb .timer .C :
176
- qb .flushCurrentBatchIfNecessary ()
170
+ case <- qb .ticker .C :
171
+ qb .flushCurrentBatchIfNecessary (false )
177
172
}
178
173
}
179
174
}()
@@ -182,27 +177,30 @@ func (qb *defaultBatcher) startTimeBasedFlushingGoroutine() {
182
177
// Start starts the goroutine that reads from the queue and flushes asynchronously.
183
178
func (qb * defaultBatcher ) Start (_ context.Context , _ component.Host ) error {
184
179
if qb .cfg .FlushTimeout > 0 {
185
- qb .timer = time .NewTimer (qb .cfg .FlushTimeout )
180
+ qb .ticker = time .NewTicker (qb .cfg .FlushTimeout )
186
181
qb .startTimeBasedFlushingGoroutine ()
187
182
}
188
183
189
184
return nil
190
185
}
191
186
192
187
// flushCurrentBatchIfNecessary sends out the current request batch if it is not nil
193
- func (qb * defaultBatcher ) flushCurrentBatchIfNecessary () {
188
+ func (qb * defaultBatcher ) flushCurrentBatchIfNecessary (forceFlush bool ) {
194
189
qb .currentBatchMu .Lock ()
195
190
if qb .currentBatch == nil {
196
191
qb .currentBatchMu .Unlock ()
197
192
return
198
193
}
194
+ if ! forceFlush && time .Since (qb .currentBatch .created ) < qb .cfg .FlushTimeout {
195
+ qb .currentBatchMu .Unlock ()
196
+ return
197
+ }
199
198
batchToFlush := qb .currentBatch
200
199
qb .currentBatch = nil
201
200
qb .currentBatchMu .Unlock ()
202
201
203
202
// flush() blocks until successfully started a goroutine for flushing.
204
203
qb .flush (batchToFlush .ctx , batchToFlush .req , batchToFlush .done )
205
- qb .resetTimer ()
206
204
}
207
205
208
206
// flush starts a goroutine that calls consumeFunc. It blocks until a worker is available if necessary.
@@ -224,7 +222,7 @@ func (qb *defaultBatcher) flush(ctx context.Context, req request.Request, done D
224
222
func (qb * defaultBatcher ) Shutdown (_ context.Context ) error {
225
223
close (qb .shutdownCh )
226
224
// Make sure execute one last flush if necessary.
227
- qb .flushCurrentBatchIfNecessary ()
225
+ qb .flushCurrentBatchIfNecessary (true )
228
226
qb .stopWG .Wait ()
229
227
return nil
230
228
}
0 commit comments