pkg/broadcastwriter: use []byte to lower alloc
Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
This commit is contained in:
parent
90c3318cb6
commit
dd54d0419c
1 changed files with 16 additions and 4 deletions
|
@ -8,6 +8,7 @@ import (
|
||||||
|
|
||||||
"github.com/Sirupsen/logrus"
|
"github.com/Sirupsen/logrus"
|
||||||
"github.com/docker/docker/pkg/jsonlog"
|
"github.com/docker/docker/pkg/jsonlog"
|
||||||
|
"github.com/docker/docker/pkg/timeutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BroadcastWriter accumulate multiple io.WriteCloser by stream.
|
// BroadcastWriter accumulate multiple io.WriteCloser by stream.
|
||||||
|
@ -33,6 +34,7 @@ func (w *BroadcastWriter) AddWriter(writer io.WriteCloser, stream string) {
|
||||||
// Write writes bytes to all writers. Failed writers will be evicted during
|
// Write writes bytes to all writers. Failed writers will be evicted during
|
||||||
// this call.
|
// this call.
|
||||||
func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
|
func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
|
||||||
|
var timestamp string
|
||||||
created := time.Now().UTC()
|
created := time.Now().UTC()
|
||||||
w.Lock()
|
w.Lock()
|
||||||
if writers, ok := w.streams[""]; ok {
|
if writers, ok := w.streams[""]; ok {
|
||||||
|
@ -49,16 +51,26 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
|
||||||
}
|
}
|
||||||
w.buf.Write(p)
|
w.buf.Write(p)
|
||||||
for {
|
for {
|
||||||
line, err := w.buf.ReadString('\n')
|
if n := w.buf.Len(); n == 0 {
|
||||||
if err != nil {
|
|
||||||
w.buf.WriteString(line)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
i := bytes.IndexByte(w.buf.Bytes(), '\n')
|
||||||
|
if i < 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
lineBytes := w.buf.Next(i + 1)
|
||||||
|
if timestamp == "" {
|
||||||
|
timestamp, err = timeutils.FastMarshalJSON(created)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for stream, writers := range w.streams {
|
for stream, writers := range w.streams {
|
||||||
if stream == "" {
|
if stream == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
jsonLog := jsonlog.JSONLog{Log: line, Stream: stream, Created: created}
|
jsonLog := jsonlog.JSONLogBytes{Log: lineBytes, Stream: stream, Created: timestamp}
|
||||||
err = jsonLog.MarshalJSONBuf(w.jsLogBuf)
|
err = jsonLog.MarshalJSONBuf(w.jsLogBuf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("Error making JSON log line: %s", err)
|
logrus.Errorf("Error making JSON log line: %s", err)
|
||||||
|
|
Loading…
Add table
Reference in a new issue