podWatcher: prefer bufio.Reader to Scanner
continuous-integration/drone/push Build is failing Details

This avoids "token too large" errors when scanning large lines, probably
at the cost of some memory usage and/or allocations.
This commit is contained in:
Rob Watson 2022-07-05 18:26:47 +02:00
parent 0bbbb4015b
commit 41f53c88df
2 changed files with 17 additions and 7 deletions

View File

@ -225,13 +225,22 @@ func copyLogStream(ctx context.Context, wg *sync.WaitGroup, client KubernetesCli
errch := make(chan error, 1) errch := make(chan error, 1)
go func() { go func() {
defer close(errch) defer close(errch)
scanner := bufio.NewScanner(logReader) r := bufio.NewReader(logReader)
for scanner.Scan() { for {
logsLines <- "[" + stream.podName + "] " + scanner.Text() + nl lineBytes, err := r.ReadBytes('\n')
} line := string(lineBytes)
if err := scanner.Err(); err != nil { if len(line) > 0 && !strings.HasSuffix(line, "\n") {
errch <- newLogError(fmt.Errorf("error scanning logs: %v", err), stream) line += "\n"
return }
if len(line) > 0 {
logsLines <- "[" + stream.podName + "] " + line
}
if err != nil {
if err != io.EOF {
errch <- newLogError(fmt.Errorf("error reading logs: %v", err), stream)
}
return
}
} }
}() }()

View File

@ -164,6 +164,7 @@ func TestWatcherWithPodWatcher(t *testing.T) {
assert.ElementsMatch(t, []string{"[foo] fake logs", "[bar] fake logs"}, lines) assert.ElementsMatch(t, []string{"[foo] fake logs", "[bar] fake logs"}, lines)
} }
// bufToLines splits the output buffer removing newline characters.
func bufToLines(buf *bytes.Buffer) []string { func bufToLines(buf *bytes.Buffer) []string {
return strings.Split(strings.TrimSpace(buf.String()), "\n") return strings.Split(strings.TrimSpace(buf.String()), "\n")
} }