Made the syslog server use a buffered chan

This commit is contained in:
Diptanu Choudhury 2016-02-22 16:10:23 -08:00
parent 91e8800b6b
commit 3211e67961
1 changed files with 25 additions and 22 deletions

View File

@ -4,6 +4,7 @@ package logging
import (
"fmt"
"io"
"log"
"log/syslog"
"net"
@ -60,6 +61,7 @@ type SyslogCollector struct {
lro *FileRotator
lre *FileRotator
server *SyslogServer
syslogChan chan *SyslogMessage
taskDir string
logger *log.Logger
@ -67,7 +69,7 @@ type SyslogCollector struct {
// NewSyslogCollector returns an implementation of the SyslogCollector
func NewSyslogCollector(logger *log.Logger) *SyslogCollector {
return &SyslogCollector{logger: logger}
return &SyslogCollector{logger: logger, syslogChan: make(chan *SyslogMessage, 2048)}
}
// LaunchCollector launches a new syslog server and starts writing log lines to
@ -84,10 +86,8 @@ func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogColl
return nil, err
}
channel := make(chan *SyslogMessage)
syslogServer := NewSyslogServer(l, channel, s.logger)
s.server = syslogServer
go syslogServer.Start()
s.server = NewSyslogServer(l, s.syslogChan, s.logger)
go s.server.Start()
logFileSize := int64(ctx.LogConfig.MaxFileSizeMB * 1024 * 1024)
path := filepath.Join(s.taskDir, allocdir.TaskLocal)
@ -106,8 +106,12 @@ func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogColl
}
s.lre = lre
go func(channel chan *SyslogMessage) {
for logParts := range channel {
go s.collectLogs(lre, lro)
return &SyslogCollectorState{Addr: l.Addr().String()}, nil
}
func (s *SyslogCollector) collectLogs(we io.Writer, wo io.Writer) {
for logParts := range s.syslogChan {
// If the severity of the log line is err then we write to stderr
// otherwise all messages go to stdout
if logParts.Severity == syslog.LOG_ERR {
@ -118,13 +122,12 @@ func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogColl
s.lro.Write([]byte{'\n'})
}
}
}(channel)
return &SyslogCollectorState{Addr: l.Addr().String()}, nil
}
// Exit kills the syslog server
func (s *SyslogCollector) Exit() error {
s.server.Shutdown()
close(s.syslogChan)
return nil
}