mirror of
https://github.com/wneessen/logranger.git
synced 2024-11-22 12:50:50 +01:00
Refactor processing deadline logics in server.go
The processing deadline handling was moved from the connection-level anonymous go-routine to the HandleConnection method. This refactor allows the reading from the connection to be looped, improving the handling of timeouts and parsing failure cases.
This commit is contained in:
parent
8d75b19e27
commit
5d72bcac91
1 changed files with 33 additions and 34 deletions
23
server.go
23
server.go
|
@ -110,14 +110,7 @@ func (s *Server) Listen() {
|
|||
conn := NewConnection(c)
|
||||
s.wg.Add(1)
|
||||
go func(co *Connection) {
|
||||
err = co.conn.SetDeadline(time.Now().Add(s.conf.Parser.Timeout))
|
||||
switch {
|
||||
case err == nil:
|
||||
s.HandleConnection(co)
|
||||
default:
|
||||
s.log.Error("failed to set processing deadline", LogErrKey, err,
|
||||
slog.Duration("timeout", s.conf.Parser.Timeout))
|
||||
}
|
||||
s.wg.Done()
|
||||
}(conn)
|
||||
}
|
||||
|
@ -127,14 +120,20 @@ func (s *Server) Listen() {
|
|||
// It logs debug information about the connection and measures the processing time.
|
||||
// It closes the connection when done, and logs any error encountered during the process.
|
||||
func (s *Server) HandleConnection(c *Connection) {
|
||||
b := time.Now()
|
||||
s.log.Debug("handling connection")
|
||||
defer func() {
|
||||
if err := c.conn.Close(); err != nil {
|
||||
s.log.Error("failed to close connection", LogErrKey, err)
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
ReadLoop:
|
||||
for {
|
||||
if err := c.conn.SetDeadline(time.Now().Add(s.conf.Parser.Timeout)); err != nil {
|
||||
s.log.Error("failed to set processing deadline", LogErrKey, err,
|
||||
slog.Duration("timeout", s.conf.Parser.Timeout))
|
||||
return
|
||||
}
|
||||
lm, err := s.parser.ParseReader(c.rb)
|
||||
if err != nil {
|
||||
var ne *net.OpError
|
||||
|
@ -154,14 +153,14 @@ func (s *Server) HandleConnection(c *Connection) {
|
|||
default:
|
||||
s.log.Error("failed to parse message", LogErrKey, err,
|
||||
slog.String("parser_type", s.conf.Parser.Type))
|
||||
return
|
||||
continue ReadLoop
|
||||
}
|
||||
}
|
||||
s.log.Debug("log message successfully received",
|
||||
slog.String("message", lm.Message.String()),
|
||||
slog.String("facility", lm.Facility.String()),
|
||||
slog.String("severity", lm.Severity.String()),
|
||||
slog.String("processing_time", time.Since(b).String()))
|
||||
slog.String("severity", lm.Severity.String()))
|
||||
}
|
||||
}
|
||||
|
||||
// setLogLevel sets the log level based on the value of `s.conf.Log.Level`.
|
||||
|
|
Loading…
Reference in a new issue