Skip to content

Commit ebd2a9d

Browse files
committed
Replace timer with ticker
1 parent 2bc783c commit ebd2a9d

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

exporter/exporterhelper/internal/queuebatch/default_batcher.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ import (
1717
)
1818

1919
type batch struct {
20-
ctx context.Context
21-
req request.Request
22-
done multiDone
20+
ctx context.Context
21+
req request.Request
22+
done multiDone
23+
created time.Time
2324
}
2425

2526
// defaultBatcher continuously batch incoming requests and flushes asynchronously if minimum size limit is met or on timeout.
@@ -30,7 +31,7 @@ type defaultBatcher struct {
3031
stopWG sync.WaitGroup
3132
currentBatchMu sync.Mutex
3233
currentBatch *batch
33-
timer *time.Timer
34+
ticker *time.Ticker
3435
shutdownCh chan struct{}
3536
}
3637

@@ -52,12 +53,6 @@ func newDefaultBatcher(batchCfg BatchConfig, consumeFunc sender.SendFunc[request
5253
}
5354
}
5455

55-
func (qb *defaultBatcher) resetTimer() {
56-
if qb.batchCfg.FlushTimeout > 0 {
57-
qb.timer.Reset(qb.batchCfg.FlushTimeout)
58-
}
59-
}
60-
6156
func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done Done) {
6257
qb.currentBatchMu.Lock()
6358

@@ -81,11 +76,11 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
8176
// Do not flush the last item and add it to the current batch.
8277
reqList = reqList[:len(reqList)-1]
8378
qb.currentBatch = &batch{
84-
ctx: ctx,
85-
req: lastReq,
86-
done: multiDone{done},
79+
ctx: ctx,
80+
req: lastReq,
81+
done: multiDone{done},
82+
created: time.Now(),
8783
}
88-
qb.resetTimer()
8984
}
9085

9186
qb.currentBatchMu.Unlock()
@@ -136,11 +131,11 @@ func (qb *defaultBatcher) Consume(ctx context.Context, req request.Request, done
136131
// Do not flush the last item and add it to the current batch.
137132
reqList = reqList[:len(reqList)-1]
138133
qb.currentBatch = &batch{
139-
ctx: ctx,
140-
req: lastReq,
141-
done: multiDone{done},
134+
ctx: ctx,
135+
req: lastReq,
136+
done: multiDone{done},
137+
created: time.Now(),
142138
}
143-
qb.resetTimer()
144139
}
145140
}
146141

@@ -162,8 +157,8 @@ func (qb *defaultBatcher) startTimeBasedFlushingGoroutine() {
162157
select {
163158
case <-qb.shutdownCh:
164159
return
165-
case <-qb.timer.C:
166-
qb.flushCurrentBatchIfNecessary()
160+
case <-qb.ticker.C:
161+
qb.flushCurrentBatchIfNecessary(false)
167162
}
168163
}
169164
}()
@@ -172,27 +167,30 @@ func (qb *defaultBatcher) startTimeBasedFlushingGoroutine() {
172167
// Start starts the goroutine that reads from the queue and flushes asynchronously.
173168
func (qb *defaultBatcher) Start(_ context.Context, _ component.Host) error {
174169
if qb.batchCfg.FlushTimeout > 0 {
175-
qb.timer = time.NewTimer(qb.batchCfg.FlushTimeout)
170+
qb.ticker = time.NewTicker(qb.batchCfg.FlushTimeout)
176171
qb.startTimeBasedFlushingGoroutine()
177172
}
178173

179174
return nil
180175
}
181176

182177
// flushCurrentBatchIfNecessary sends out the current request batch if it is not nil
183-
func (qb *defaultBatcher) flushCurrentBatchIfNecessary() {
178+
func (qb *defaultBatcher) flushCurrentBatchIfNecessary(forceFlush bool) {
184179
qb.currentBatchMu.Lock()
185180
if qb.currentBatch == nil {
186181
qb.currentBatchMu.Unlock()
187182
return
188183
}
184+
if !forceFlush && time.Since(qb.currentBatch.created) < qb.batchCfg.FlushTimeout {
185+
qb.currentBatchMu.Unlock()
186+
return
187+
}
189188
batchToFlush := qb.currentBatch
190189
qb.currentBatch = nil
191190
qb.currentBatchMu.Unlock()
192191

193192
// flush() blocks until successfully started a goroutine for flushing.
194193
qb.flush(batchToFlush.ctx, batchToFlush.req, batchToFlush.done)
195-
qb.resetTimer()
196194
}
197195

198196
// flush starts a goroutine that calls consumeFunc. It blocks until a worker is available if necessary.
@@ -214,7 +212,7 @@ func (qb *defaultBatcher) flush(ctx context.Context, req request.Request, done D
214212
func (qb *defaultBatcher) Shutdown(_ context.Context) error {
215213
close(qb.shutdownCh)
216214
// Make sure execute one last flush if necessary.
217-
qb.flushCurrentBatchIfNecessary()
215+
qb.flushCurrentBatchIfNecessary(true)
218216
qb.stopWG.Wait()
219217
return nil
220218
}

exporter/exporterhelper/internal/queuebatch/queue_batch_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -591,8 +591,8 @@ func TestQueueBatchTimerFlush(t *testing.T) {
591591
assert.LessOrEqual(t, 1, sink.RequestsCount())
592592
assert.Equal(t, 8, sink.ItemsCount())
593593

594-
// Confirm that it is flushed after 100ms (using 60+50=110 here to be safe)
595-
time.Sleep(50 * time.Millisecond)
594+
// Confirm that it is flushed after 100ms (using 100+50=150 here to be safe)
595+
time.Sleep(100 * time.Millisecond)
596596
assert.LessOrEqual(t, 2, sink.RequestsCount())
597597
assert.Equal(t, 12, sink.ItemsCount())
598598
require.NoError(t, qb.Shutdown(context.Background()))

0 commit comments

Comments
 (0)