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)
go func() {
defer close(errch)
scanner := bufio.NewScanner(logReader)
for scanner.Scan() {
logsLines <- "[" + stream.podName + "] " + scanner.Text() + nl
}
if err := scanner.Err(); err != nil {
errch <- newLogError(fmt.Errorf("error scanning logs: %v", err), stream)
return
r := bufio.NewReader(logReader)
for {
lineBytes, err := r.ReadBytes('\n')
line := string(lineBytes)
if len(line) > 0 && !strings.HasSuffix(line, "\n") {
line += "\n"
}
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)
}
// bufToLines splits the output buffer removing newline characters.
func bufToLines(buf *bytes.Buffer) []string {
return strings.Split(strings.TrimSpace(buf.String()), "\n")
}