implement context cancellation on db error
This commit is contained in:
		| @@ -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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user