1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #13564 from burke/fix-memory-leak

Use bufio.Reader instead of bufio.Scanner for logger.Copier
This commit is contained in:
Alexander Morozov 2015-05-29 13:37:15 -07:00
commit c42810fe99

View file

@ -2,6 +2,7 @@ package logger
import ( import (
"bufio" "bufio"
"bytes"
"io" "io"
"sync" "sync"
"time" "time"
@ -40,15 +41,28 @@ func (c *Copier) Run() {
func (c *Copier) copySrc(name string, src io.Reader) { func (c *Copier) copySrc(name string, src io.Reader) {
defer c.copyJobs.Done() defer c.copyJobs.Done()
scanner := bufio.NewScanner(src) reader := bufio.NewReader(src)
for scanner.Scan() {
if err := c.dst.Log(&Message{ContainerID: c.cid, Line: scanner.Bytes(), Source: name, Timestamp: time.Now().UTC()}); err != nil { for {
logrus.Errorf("Failed to log msg %q for logger %s: %s", scanner.Bytes(), c.dst.Name(), err) line, err := reader.ReadBytes('\n')
line = bytes.TrimSuffix(line, []byte{'\n'})
// ReadBytes can return full or partial output even when it failed.
// e.g. it can return a full entry and EOF.
if err == nil || len(line) > 0 {
if logErr := c.dst.Log(&Message{ContainerID: c.cid, Line: line, Source: name, Timestamp: time.Now().UTC()}); logErr != nil {
logrus.Errorf("Failed to log msg %q for logger %s: %s", line, c.dst.Name(), logErr)
} }
} }
if err := scanner.Err(); err != nil {
if err != nil {
if err != io.EOF {
logrus.Errorf("Error scanning log stream: %s", err) logrus.Errorf("Error scanning log stream: %s", err)
} }
return
}
}
} }
// Wait waits until all copying is done // Wait waits until all copying is done