mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-20 02:57:05 +00:00
Replace pkg/log with zerolog for normal logging #90
This commit is contained in:
@@ -7,8 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/jhillyerd/inbucket/pkg/config"
|
||||
"github.com/jhillyerd/inbucket/pkg/log"
|
||||
"github.com/jhillyerd/inbucket/pkg/storage"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// Server defines an instance of our POP3 server
|
||||
@@ -36,44 +36,42 @@ func New(cfg config.POP3, shutdownChan chan bool, store storage.Store) *Server {
|
||||
|
||||
// Start the server and listen for connections
|
||||
func (s *Server) Start(ctx context.Context) {
|
||||
slog := log.With().Str("module", "pop3").Str("phase", "startup").Logger()
|
||||
addr, err := net.ResolveTCPAddr("tcp4", s.host)
|
||||
if err != nil {
|
||||
log.Errorf("POP3 Failed to build tcp4 address: %v", err)
|
||||
slog.Error().Err(err).Msg("Failed to build tcp4 address")
|
||||
s.emergencyShutdown()
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("POP3 listening on TCP4 %v", addr)
|
||||
slog.Info().Str("addr", addr.String()).Msg("POP3 listening on tcp4")
|
||||
s.listener, err = net.ListenTCP("tcp4", addr)
|
||||
if err != nil {
|
||||
log.Errorf("POP3 failed to start tcp4 listener: %v", err)
|
||||
slog.Error().Err(err).Msg("Failed to start tcp4 listener")
|
||||
s.emergencyShutdown()
|
||||
return
|
||||
}
|
||||
|
||||
// Listener go routine
|
||||
// Listener go routine.
|
||||
go s.serve(ctx)
|
||||
|
||||
// Wait for shutdown
|
||||
// Wait for shutdown.
|
||||
select {
|
||||
case _ = <-ctx.Done():
|
||||
}
|
||||
|
||||
log.Tracef("POP3 shutdown requested, connections will be drained")
|
||||
// Closing the listener will cause the serve() go routine to exit
|
||||
slog = log.With().Str("module", "pop3").Str("phase", "shutdown").Logger()
|
||||
slog.Debug().Msg("POP3 shutdown requested, connections will be drained")
|
||||
// Closing the listener will cause the serve() go routine to exit.
|
||||
if err := s.listener.Close(); err != nil {
|
||||
log.Errorf("Error closing POP3 listener: %v", err)
|
||||
slog.Error().Err(err).Msg("Failed to close POP3 listener")
|
||||
}
|
||||
}
|
||||
|
||||
// serve is the listen/accept loop
|
||||
// serve is the listen/accept loop.
|
||||
func (s *Server) serve(ctx context.Context) {
|
||||
// Handle incoming connections
|
||||
// Handle incoming connections.
|
||||
var tempDelay time.Duration
|
||||
for sid := 1; ; sid++ {
|
||||
if conn, err := s.listener.Accept(); err != nil {
|
||||
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
|
||||
// Temporary error, sleep for a bit and try again
|
||||
// Temporary error, sleep for a bit and try again.
|
||||
if tempDelay == 0 {
|
||||
tempDelay = 5 * time.Millisecond
|
||||
} else {
|
||||
@@ -82,17 +80,18 @@ func (s *Server) serve(ctx context.Context) {
|
||||
if max := 1 * time.Second; tempDelay > max {
|
||||
tempDelay = max
|
||||
}
|
||||
log.Errorf("POP3 accept error: %v; retrying in %v", err, tempDelay)
|
||||
log.Error().Str("module", "pop3").Err(err).
|
||||
Msgf("POP3 accept error; retrying in %v", tempDelay)
|
||||
time.Sleep(tempDelay)
|
||||
continue
|
||||
} else {
|
||||
// Permanent error
|
||||
// Permanent error.
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
// POP3 is shutting down
|
||||
// POP3 is shutting down.
|
||||
return
|
||||
default:
|
||||
// Something went wrong
|
||||
// Something went wrong.
|
||||
s.emergencyShutdown()
|
||||
return
|
||||
}
|
||||
@@ -118,5 +117,5 @@ func (s *Server) emergencyShutdown() {
|
||||
func (s *Server) Drain() {
|
||||
// Wait for sessions to close
|
||||
s.waitgroup.Wait()
|
||||
log.Tracef("POP3 connections have drained")
|
||||
log.Debug().Str("module", "pop3").Str("phase", "shutdown").Msg("POP3 connections have drained")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user