podWatcher: prefer bufio.Reader to Scanner
continuous-integration/drone/push Build is failing
Details
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:
parent
0bbbb4015b
commit
41f53c88df
|
@ -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
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue