Skip to content

Commit

Permalink
Merge pull request #123 from msaf1980/fix_file_switch_cpu
Browse files Browse the repository at this point in the history
Fix high cpu usage for file switch loop
  • Loading branch information
msaf1980 authored Nov 30, 2022
2 parents 101d36c + e678674 commit ee3ebeb
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions writer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,23 @@ func (w *Writer) worker(ctx context.Context) {
// open first file
rotateCheck()

ticker := time.NewTicker(100 * time.Millisecond)
tickerC := make(chan struct{}, 1)

go func() {
for {
select {
case <-ctx.Done():
return
case <-time.After(100 * time.Millisecond):
select {
case tickerC <- struct{}{}:
// pass
case <-ctx.Done():
return
}
}
}
}()

write := func(b *RowBinary.WriteBuffer) {
_, err := outBuf.Write(b.Body[:b.Used])
Expand All @@ -270,10 +286,8 @@ func (w *Writer) worker(ctx context.Context) {
select {
case b := <-w.inputChan:
write(b)
case <-ticker.C:
if size > 0 {
rotateCheck()
}
case <-tickerC:
rotateCheck()
case <-ctx.Done():
return
default: // outBuf flush if nothing received
Expand All @@ -284,6 +298,15 @@ func (w *Writer) worker(ctx context.Context) {
w.logger.Error("CompWriter Flush() failed", zap.Error(err))
}
}

select {
case b := <-w.inputChan:
write(b)
case <-tickerC:
rotateCheck()
case <-ctx.Done():
return
}
}
}
}
Expand Down

0 comments on commit ee3ebeb

Please sign in to comment.