1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-17 09:37:02 +00:00

pop3: Use config.POP3 directly in server #91

This commit is contained in:
James Hillyerd
2018-03-31 17:01:02 -07:00
parent 2c813081eb
commit 7b073562eb
2 changed files with 17 additions and 24 deletions

View File

@@ -107,12 +107,12 @@ func (s *Server) startSession(id int, conn net.Conn) {
if err := conn.Close(); err != nil { if err := conn.Close(); err != nil {
logger.Warn().Err(err).Msg("Closing connection") logger.Warn().Err(err).Msg("Closing connection")
} }
s.waitgroup.Done() s.wg.Done()
}() }()
ssn := NewSession(s, id, conn, logger) ssn := NewSession(s, id, conn, logger)
ssn.send(fmt.Sprintf("+OK Inbucket POP3 server ready <%v.%v@%v>", os.Getpid(), ssn.send(fmt.Sprintf("+OK Inbucket POP3 server ready <%v.%v@%v>", os.Getpid(),
time.Now().Unix(), s.domain)) time.Now().Unix(), s.config.Domain))
// This is our command reading loop // This is our command reading loop
for ssn.state != QUIT && ssn.sendError == nil { for ssn.state != QUIT && ssn.sendError == nil {
@@ -545,9 +545,9 @@ func (s *Session) enterState(state State) {
s.logger.Debug().Msgf("Entering state %v", state) s.logger.Debug().Msgf("Entering state %v", state)
} }
// Calculate the next read or write deadline based on maxIdleSeconds // nextDeadline calculates the next read or write deadline based on configured timeout.
func (s *Session) nextDeadline() time.Time { func (s *Session) nextDeadline() time.Time {
return time.Now().Add(s.server.timeout) return time.Now().Add(s.server.config.Timeout)
} }
// Send requested message, store errors in Session.sendError // Send requested message, store errors in Session.sendError

View File

@@ -11,36 +11,29 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
// Server defines an instance of our POP3 server // Server defines an instance of the POP3 server.
type Server struct { type Server struct {
// TODO(#91) Refactor config items out of this struct config config.POP3 // POP3 configuration.
config config.POP3 store storage.Store // Mail store.
host string listener net.Listener // TCP listener.
domain string globalShutdown chan bool // Inbucket shutdown signal.
timeout time.Duration wg *sync.WaitGroup // Waitgroup tracking sessions.
store storage.Store
listener net.Listener
globalShutdown chan bool
waitgroup *sync.WaitGroup
} }
// New creates a new Server struct // New creates a new Server struct.
func New(cfg config.POP3, shutdownChan chan bool, store storage.Store) *Server { func New(pop3Config config.POP3, shutdownChan chan bool, store storage.Store) *Server {
return &Server{ return &Server{
config: cfg, config: pop3Config,
host: cfg.Addr,
domain: cfg.Domain,
store: store, store: store,
timeout: cfg.Timeout,
globalShutdown: shutdownChan, globalShutdown: shutdownChan,
waitgroup: new(sync.WaitGroup), wg: new(sync.WaitGroup),
} }
} }
// Start the server and listen for connections // Start the server and listen for connections
func (s *Server) Start(ctx context.Context) { func (s *Server) Start(ctx context.Context) {
slog := log.With().Str("module", "pop3").Str("phase", "startup").Logger() slog := log.With().Str("module", "pop3").Str("phase", "startup").Logger()
addr, err := net.ResolveTCPAddr("tcp4", s.host) addr, err := net.ResolveTCPAddr("tcp4", s.config.Addr)
if err != nil { if err != nil {
slog.Error().Err(err).Msg("Failed to build tcp4 address") slog.Error().Err(err).Msg("Failed to build tcp4 address")
s.emergencyShutdown() s.emergencyShutdown()
@@ -101,7 +94,7 @@ func (s *Server) serve(ctx context.Context) {
} }
} else { } else {
tempDelay = 0 tempDelay = 0
s.waitgroup.Add(1) s.wg.Add(1)
go s.startSession(sid, conn) go s.startSession(sid, conn)
} }
} }
@@ -119,6 +112,6 @@ func (s *Server) emergencyShutdown() {
// Drain causes the caller to block until all active POP3 sessions have finished // Drain causes the caller to block until all active POP3 sessions have finished
func (s *Server) Drain() { func (s *Server) Drain() {
// Wait for sessions to close // Wait for sessions to close
s.waitgroup.Wait() s.wg.Wait()
log.Debug().Str("module", "pop3").Str("phase", "shutdown").Msg("POP3 connections have drained") log.Debug().Str("module", "pop3").Str("phase", "shutdown").Msg("POP3 connections have drained")
} }