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,14 +225,23 @@ 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 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)
} }
if err := scanner.Err(); err != nil {
errch <- newLogError(fmt.Errorf("error scanning logs: %v", err), stream)
return return
} }
}
}() }()
for { for {

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")
} }