Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions writers/batchwriter/batchwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ func (w *BatchWriter) Close(context.Context) error {
func (w *BatchWriter) worker(ctx context.Context, tableName string, ch <-chan *message.WriteInsert, flush <-chan chan bool) {
sizeBytes := int64(0)
resources := make([]*message.WriteInsert, 0, w.batchSize)
tick := timer(w.batchTimeout)
tick, tickClose := timer(w.batchTimeout)
defer func() {
tickClose()
}()
for {
select {
case r, ok := <-ch:
Expand All @@ -145,7 +148,8 @@ func (w *BatchWriter) worker(ctx context.Context, tableName string, ch <-chan *m
w.flushTable(ctx, tableName, resources)
resources, sizeBytes = resources[:0], 0
}
tick = timer(w.batchTimeout)
tickClose()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need to manually close on every iteration

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

incoming commit:

type ticker interface {
	Chan() <-chan time.Time
	Reset(time.Duration)
	Stop() bool
}

tick, tickClose = timer(w.batchTimeout)
case done := <-flush:
if len(resources) > 0 {
w.flushTable(ctx, tableName, resources)
Expand Down Expand Up @@ -325,9 +329,14 @@ func (w *BatchWriter) startWorker(ctx context.Context, msg *message.WriteInsert)
return nil
}

func timer(timeout time.Duration) <-chan time.Time {
func timer(timeout time.Duration) (<-chan time.Time, func()) {
if timeout == 0 {
return nil
return nil, func() {}
}
t := time.NewTimer(timeout)
return t.C, func() {
if !t.Stop() {
<-t.C
}
}
return time.After(timeout)
}
21 changes: 15 additions & 6 deletions writers/mixedbatchwriter/mixedbatchwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Client interface {
DeleteStaleBatch(ctx context.Context, messages message.WriteDeleteStales) error
}

type timerFn func(timeout time.Duration) <-chan time.Time
type timerFn func(timeout time.Duration) (<-chan time.Time, func())

type MixedBatchWriter struct {
client Client
Expand Down Expand Up @@ -116,7 +116,10 @@ func (w *MixedBatchWriter) Write(ctx context.Context, msgChan <-chan message.Wri
}
prevMsgType := writers.MsgTypeUnset
var err error
tick := w.timerFn(w.batchTimeout)
tick, tickClose := w.timerFn(w.batchTimeout)
defer func() {
tickClose()
}()
loop:
for {
select {
Expand Down Expand Up @@ -149,7 +152,8 @@ loop:
return err
}
prevMsgType = writers.MsgTypeUnset
tick = w.timerFn(w.batchTimeout)
tickClose()
tick, tickClose = w.timerFn(w.batchTimeout)
}
}
return flush(prevMsgType)
Expand Down Expand Up @@ -216,9 +220,14 @@ func (m *insertBatchManager) flush(ctx context.Context) error {
return nil
}

func timer(timeout time.Duration) <-chan time.Time {
func timer(timeout time.Duration) (<-chan time.Time, func()) {
if timeout == 0 {
return nil
return nil, func() {}
}
t := time.NewTimer(timeout)
return t.C, func() {
if !t.Stop() {
<-t.C
}
}
return time.After(timeout)
}
4 changes: 2 additions & 2 deletions writers/mixedbatchwriter/mixedbatchwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,13 @@ func TestMixedBatchWriterTimeout(t *testing.T) {
wr, err := New(client,
WithBatchSize(1000),
WithBatchSizeBytes(1000000),
withTimerFn(func(_ time.Duration) <-chan time.Time {
withTimerFn(func(_ time.Duration) (<-chan time.Time, func()) {
c := make(chan time.Time)
go func() {
<-triggerTimeout
c <- time.Now()
}()
return c
return c, func() {}
}),
)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions writers/streamingbatchwriter/mocktimer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type mockTimer struct {
expire chan time.Time
}

func (t *mockTimer) timer(time.Duration) <-chan time.Time {
return t.expire
func (t *mockTimer) timer(time.Duration) (<-chan time.Time, func()) {
return t.expire, func() {}
}

func newMockTimer() (timerFn, chan time.Time) {
Expand Down
21 changes: 15 additions & 6 deletions writers/streamingbatchwriter/streamingbatchwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type StreamingBatchWriter struct {
timerFn timerFn
}

type timerFn func(timeout time.Duration) <-chan time.Time
type timerFn func(timeout time.Duration) (<-chan time.Time, func())

// Assert at compile-time that StreamingBatchWriter implements the Writer interface
var _ writers.Writer = (*StreamingBatchWriter)(nil)
Expand Down Expand Up @@ -345,7 +345,10 @@ func (s *streamingWorkerManager[T]) run(ctx context.Context, wg *sync.WaitGroup,
}
defer closeFlush()

tick := s.timerFn(s.batchTimeout)
tick, tickClose := s.timerFn(s.batchTimeout)
defer func() {
tickClose()
}()
for {
select {
case r, ok := <-s.ch:
Expand All @@ -370,7 +373,8 @@ func (s *streamingWorkerManager[T]) run(ctx context.Context, wg *sync.WaitGroup,
if sizeRows > 0 {
closeFlush()
}
tick = s.timerFn(s.batchTimeout)
tickClose()
tick, tickClose = s.timerFn(s.batchTimeout)
case done := <-s.flush:
if sizeRows > 0 {
closeFlush()
Expand All @@ -380,9 +384,14 @@ func (s *streamingWorkerManager[T]) run(ctx context.Context, wg *sync.WaitGroup,
}
}

func timer(timeout time.Duration) <-chan time.Time {
func timer(timeout time.Duration) (<-chan time.Time, func()) {
if timeout == 0 {
return nil
return nil, func() {}
}
t := time.NewTimer(timeout)
return t.C, func() {
if !t.Stop() {
<-t.C
}
}
return time.After(timeout)
}