From 61ad4654f584f2761aca7c69ea1f4a51bcd87259 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Mon, 30 Mar 2015 14:30:01 -0700 Subject: [PATCH] Skip heavy operations if there is no jsonlog writers Signed-off-by: Alexander Morozov --- broadcastwriter/broadcastwriter.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/broadcastwriter/broadcastwriter.go b/broadcastwriter/broadcastwriter.go index ce63f76..248cd8f 100644 --- a/broadcastwriter/broadcastwriter.go +++ b/broadcastwriter/broadcastwriter.go @@ -34,8 +34,6 @@ 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 { for sw := range writers { @@ -44,11 +42,19 @@ func (w *BroadcastWriter) Write(p []byte) (n int, err error) { delete(writers, sw) } } + // exit if there is no more writers + if len(w.streams) == 1 { + w.buf.Reset() + w.Unlock() + return len(p), nil + } } if w.jsLogBuf == nil { w.jsLogBuf = new(bytes.Buffer) w.jsLogBuf.Grow(1024) } + var timestamp string + created := time.Now().UTC() w.buf.Write(p) for { if n := w.buf.Len(); n == 0 {