implement context cancellation on db error

This commit is contained in:
2022-11-09 19:56:44 +02:00
parent 801ac76eef
commit f26438f32a
3 changed files with 94 additions and 56 deletions

View File

@@ -1,6 +1,7 @@
package lines
import (
"context"
"log"
"sync"
@@ -23,7 +24,7 @@ type (
)
// assumes all lines are from same file
func (unparsed RawC) Process(bufferLimitBytes int, parsed chan<- m.Record) {
func (unparsed RawC) Process(ctx context.Context, bufferLimitBytes int, parsed chan<- m.Record) {
lines := make(chan singleLine)
go unparsed.parse(lines)
@@ -32,34 +33,42 @@ func (unparsed RawC) Process(bufferLimitBytes int, parsed chan<- m.Record) {
stdOut, stdErr := make(chan singleLine), make(chan singleLine)
go func() {
singleLines(stdOut).process(bufferLimitBytes, parsed)
singleLines(stdOut).process(ctx, bufferLimitBytes, parsed)
wg.Done()
}()
go func() {
singleLines(stdErr).process(bufferLimitBytes, parsed)
singleLines(stdErr).process(ctx, bufferLimitBytes, parsed)
wg.Done()
}()
defer func() {
close(stdOut)
close(stdErr)
wg.Wait()
close(parsed)
}()
// split stdout and stderr
for {
line, ok := <-lines
if !ok {
close(stdOut)
close(stdErr)
wg.Wait()
close(parsed)
select {
case <-ctx.Done():
return
}
if line.StdErr {
stdErr <- line
} else {
stdOut <- line
case line, ok := <-lines:
if !ok {
return
}
if line.StdErr {
stdErr <- line
} else {
stdOut <- line
}
}
}
}
func (lines singleLines) process(bufferLimitBytes int, parsed chan<- m.Record) {
func (lines singleLines) process(ctx context.Context, bufferLimitBytes int, parsed chan<- m.Record) {
var firstMetadata *m.ParsedMetadata
var buffer []byte
@@ -86,12 +95,17 @@ func (lines singleLines) process(bufferLimitBytes int, parsed chan<- m.Record) {
}
if !line.partial {
parsed <- m.Record{
select {
case <-ctx.Done():
return
case parsed <- m.Record{
File: line.File.File,
Offset: line.Offset,
String: string(buffer),
ParsedMetadata: *firstMetadata,
}:
}
buffer = nil