зеркало из https://github.com/microsoft/docker.git
Merge pull request #8109 from unclejack/avoid_jsonlog_alloc
daemon/logs: avoid JSONLog struct alloc in loop
This commit is contained in:
Коммит
ac75835931
|
@ -88,9 +88,8 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
|
||||||
cLog = tmp
|
cLog = tmp
|
||||||
}
|
}
|
||||||
dec := json.NewDecoder(cLog)
|
dec := json.NewDecoder(cLog)
|
||||||
|
l := &jsonlog.JSONLog{}
|
||||||
for {
|
for {
|
||||||
l := &jsonlog.JSONLog{}
|
|
||||||
|
|
||||||
if err := dec.Decode(l); err == io.EOF {
|
if err := dec.Decode(l); err == io.EOF {
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -102,11 +101,12 @@ func (daemon *Daemon) ContainerLogs(job *engine.Job) engine.Status {
|
||||||
logLine = fmt.Sprintf("%s %s", l.Created.Format(format), logLine)
|
logLine = fmt.Sprintf("%s %s", l.Created.Format(format), logLine)
|
||||||
}
|
}
|
||||||
if l.Stream == "stdout" && stdout {
|
if l.Stream == "stdout" && stdout {
|
||||||
fmt.Fprintf(job.Stdout, "%s", logLine)
|
io.WriteString(job.Stdout, logLine)
|
||||||
}
|
}
|
||||||
if l.Stream == "stderr" && stderr {
|
if l.Stream == "stderr" && stderr {
|
||||||
fmt.Fprintf(job.Stderr, "%s", logLine)
|
io.WriteString(job.Stderr, logLine)
|
||||||
}
|
}
|
||||||
|
l.Reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,11 +25,16 @@ func (jl *JSONLog) Format(format string) (string, error) {
|
||||||
return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
|
return fmt.Sprintf("[%s] %s", jl.Created.Format(format), jl.Log), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (jl *JSONLog) Reset() {
|
||||||
|
jl.Log = ""
|
||||||
|
jl.Stream = ""
|
||||||
|
jl.Created = time.Time{}
|
||||||
|
}
|
||||||
|
|
||||||
func WriteLog(src io.Reader, dst io.Writer, format string) error {
|
func WriteLog(src io.Reader, dst io.Writer, format string) error {
|
||||||
dec := json.NewDecoder(src)
|
dec := json.NewDecoder(src)
|
||||||
|
l := &JSONLog{}
|
||||||
for {
|
for {
|
||||||
l := &JSONLog{}
|
|
||||||
|
|
||||||
if err := dec.Decode(l); err == io.EOF {
|
if err := dec.Decode(l); err == io.EOF {
|
||||||
return nil
|
return nil
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
|
@ -43,5 +48,6 @@ func WriteLog(src io.Reader, dst io.Writer, format string) error {
|
||||||
if _, err := io.WriteString(dst, line); err != nil {
|
if _, err := io.WriteString(dst, line); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
l.Reset()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Ссылка в новой задаче