1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2026-01-27 21:45:56 +00:00

Basic SIGTERM handling

This commit is contained in:
James Hillyerd
2012-11-16 20:44:41 -08:00
parent cabc5f0d2b
commit 7ccef7b977
3 changed files with 76 additions and 13 deletions

View File

@@ -20,6 +20,8 @@ type Server struct {
maxMessageBytes int
dataStore DataStore
storeMessages bool
listener net.Listener
shutdown bool
}
// Raw stat collectors
@@ -62,9 +64,9 @@ func (s *Server) Start() {
}
log.Info("SMTP listening on TCP4 %v", addr)
ln, err := net.ListenTCP("tcp4", addr)
s.listener, err = net.ListenTCP("tcp4", addr)
if err != nil {
log.Error("Failed to start tcp4 listener: %v", err)
log.Error("SMTP failed to start tcp4 listener: %v", err)
// TODO More graceful early-shutdown procedure
panic(err)
}
@@ -79,18 +81,45 @@ func (s *Server) Start() {
StartRetentionScanner(s.dataStore)
// Handle incoming connections
var tempDelay time.Duration
for sid := 1; ; sid++ {
if conn, err := ln.Accept(); err != nil {
// TODO Implement a max error counter before shutdown?
// or maybe attempt to restart smtpd
panic(err)
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
if tempDelay == 0 {
tempDelay = 5 * time.Millisecond
} else {
tempDelay *= 2
}
if max := 1 * time.Second; tempDelay > max {
tempDelay = max
}
log.Error("SMTP accept error: %v; retrying in %v", err, tempDelay)
time.Sleep(tempDelay)
continue
} else {
if s.shutdown {
log.Trace("SMTP listener shutting down on request")
return
}
// TODO Implement a max error counter before shutdown?
// or maybe attempt to restart smtpd
panic(err)
}
} else {
tempDelay = 0
expConnectsTotal.Add(1)
go s.startSession(sid, conn)
}
}
}
func (s *Server) Stop() {
log.Trace("SMTP shutdown requested")
s.shutdown = true
s.listener.Close()
}
// When the provided Ticker ticks, we update our metrics history
func metricsTicker(t *time.Ticker) {
ok := true