Added some docs to the syslog server

This commit is contained in:
Diptanu Choudhury 2016-02-19 14:20:21 -08:00
parent 8096722cad
commit 694cecfbf8

View file

@ -6,6 +6,7 @@ import (
"net" "net"
) )
// SyslogServer is a server which listens to syslog messages and parses them
type SyslogServer struct { type SyslogServer struct {
listener net.Listener listener net.Listener
messages chan *SyslogMessage messages chan *SyslogMessage
@ -15,6 +16,7 @@ type SyslogServer struct {
logger *log.Logger logger *log.Logger
} }
// NewSyslogServer creates a new syslog server
func NewSyslogServer(l net.Listener, messages chan *SyslogMessage, logger *log.Logger) *SyslogServer { func NewSyslogServer(l net.Listener, messages chan *SyslogMessage, logger *log.Logger) *SyslogServer {
parser := NewDockerLogParser(logger) parser := NewDockerLogParser(logger)
return &SyslogServer{ return &SyslogServer{
@ -26,10 +28,12 @@ func NewSyslogServer(l net.Listener, messages chan *SyslogMessage, logger *log.L
} }
} }
// Start starts accepting syslog connections
func (s *SyslogServer) Start() { func (s *SyslogServer) Start() {
for { for {
select { select {
case <-s.doneCh: case <-s.doneCh:
s.listener.Close()
return return
default: default:
connection, err := s.listener.Accept() connection, err := s.listener.Accept()
@ -38,12 +42,13 @@ func (s *SyslogServer) Start() {
s.logger.Printf("[ERROR] logcollector.server: error in accepting connection: %v", err) s.logger.Printf("[ERROR] logcollector.server: error in accepting connection: %v", err)
continue continue
} }
go s.Read(connection) go s.read(connection)
} }
} }
} }
func (s *SyslogServer) Read(connection net.Conn) { // read reads the bytes from a connection
func (s *SyslogServer) read(connection net.Conn) {
defer connection.Close() defer connection.Close()
scanner := bufio.NewScanner(bufio.NewReader(connection)) scanner := bufio.NewScanner(bufio.NewReader(connection))
@ -65,6 +70,7 @@ LOOP:
} }
} }
// Shutdown shutsdown the syslog server
func (s *SyslogServer) Shutdown() { func (s *SyslogServer) Shutdown() {
close(s.doneCh) close(s.doneCh)
} }