@@ -61,11 +61,9 @@ type StreamingBatchWriter struct {
6161 batchSizeRows int64
6262 batchSizeBytes int64
6363
64- timerFn timerFn
64+ tickerFn writers. TickerFunc
6565}
6666
67- type timerFn func (timeout time.Duration ) <- chan time.Time
68-
6967// Assert at compile-time that StreamingBatchWriter implements the Writer interface
7068var _ writers.Writer = (* StreamingBatchWriter )(nil )
7169
@@ -95,9 +93,9 @@ func WithBatchSizeBytes(size int64) Option {
9593 }
9694}
9795
98- func withTimerFn ( timer timerFn ) Option {
96+ func withTickerFn ( tickerFn writers. TickerFunc ) Option {
9997 return func (p * StreamingBatchWriter ) {
100- p .timerFn = timer
98+ p .tickerFn = tickerFn
10199 }
102100}
103101
@@ -115,7 +113,7 @@ func New(client Client, opts ...Option) (*StreamingBatchWriter, error) {
115113 batchTimeout : defaultBatchTimeoutSeconds * time .Second ,
116114 batchSizeRows : defaultBatchSize ,
117115 batchSizeBytes : defaultBatchSizeBytes ,
118- timerFn : timer ,
116+ tickerFn : writers . NewTicker ,
119117 }
120118 for _ , opt := range opts {
121119 opt (c )
@@ -225,7 +223,7 @@ func (w *StreamingBatchWriter) startWorker(ctx context.Context, errCh chan<- err
225223
226224 batchSizeRows : w .batchSizeRows ,
227225 batchTimeout : w .batchTimeout ,
228- timerFn : w . timerFn ,
226+ tickerFn : w . tickerFn ,
229227 }
230228
231229 w .workersWaitGroup .Add (1 )
@@ -277,7 +275,7 @@ func (w *StreamingBatchWriter) startWorker(ctx context.Context, errCh chan<- err
277275 batchSizeRows : w .batchSizeRows ,
278276 batchSizeBytes : w .batchSizeBytes ,
279277 batchTimeout : w .batchTimeout ,
280- timerFn : w . timerFn ,
278+ tickerFn : w . tickerFn ,
281279 }
282280 w .workersLock .Lock ()
283281 w .insertWorkers [tableName ] = wr
@@ -303,7 +301,7 @@ type streamingWorkerManager[T message.WriteMessage] struct {
303301 batchSizeRows int64
304302 batchSizeBytes int64
305303 batchTimeout time.Duration
306- timerFn timerFn
304+ tickerFn writers. TickerFunc
307305}
308306
309307func (s * streamingWorkerManager [T ]) run (ctx context.Context , wg * sync.WaitGroup , tableName string ) {
@@ -345,7 +343,8 @@ func (s *streamingWorkerManager[T]) run(ctx context.Context, wg *sync.WaitGroup,
345343 }
346344 defer closeFlush ()
347345
348- tick := s .timerFn (s .batchTimeout )
346+ tick , done := s .tickerFn (s .batchTimeout )
347+ defer done ()
349348 for {
350349 select {
351350 case r , ok := <- s .ch :
@@ -370,7 +369,6 @@ func (s *streamingWorkerManager[T]) run(ctx context.Context, wg *sync.WaitGroup,
370369 if sizeRows > 0 {
371370 closeFlush ()
372371 }
373- tick = s .timerFn (s .batchTimeout )
374372 case done := <- s .flush :
375373 if sizeRows > 0 {
376374 closeFlush ()
@@ -379,10 +377,3 @@ func (s *streamingWorkerManager[T]) run(ctx context.Context, wg *sync.WaitGroup,
379377 }
380378 }
381379}
382-
383- func timer (timeout time.Duration ) <- chan time.Time {
384- if timeout == 0 {
385- return nil
386- }
387- return time .After (timeout )
388- }
0 commit comments