pkg/broadcastwriter: use []byte to lower alloc

Signed-off-by: Cristian Staretu <cristian.staretu@gmail.com>
This commit is contained in:
unclejack 2015-03-30 22:09:56 +03:00
parent b535ed3595
commit 5550a46946
1 changed files with 16 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/Sirupsen/logrus"
"github.com/docker/docker/pkg/jsonlog"
"github.com/docker/docker/pkg/timeutils"
)
// 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
// this call.
func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
var timestamp string
created := time.Now().UTC()
w.Lock()
if writers, ok := w.streams[""]; ok {
@ -49,16 +51,26 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) {
}
w.buf.Write(p)
for {
line, err := w.buf.ReadString('\n')
if err != nil {
w.buf.WriteString(line)
if n := w.buf.Len(); n == 0 {
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 {
if stream == "" {
continue
}
jsonLog := jsonlog.JSONLog{Log: line, Stream: stream, Created: created}
jsonLog := jsonlog.JSONLogBytes{Log: lineBytes, Stream: stream, Created: timestamp}
err = jsonLog.MarshalJSONBuf(w.jsLogBuf)
if err != nil {
logrus.Errorf("Error making JSON log line: %s", err)