mirror of
https://blitiri.com.ar/repos/chasquid
synced 2025-12-18 14:47:03 +00:00
domaininfo: Do not reload the database periodically
It is not expected that the user modifies the domaininfo database behind chasquid's back, and reloading it can be somewhat expensive. So this patch removes the periodic reload, and instead makes it triggered by SIGHUP so the user can trigger a reload manually if needed.
This commit is contained in:
26
chasquid.go
26
chasquid.go
@@ -18,6 +18,7 @@ import (
|
|||||||
|
|
||||||
"blitiri.com.ar/go/chasquid/internal/config"
|
"blitiri.com.ar/go/chasquid/internal/config"
|
||||||
"blitiri.com.ar/go/chasquid/internal/courier"
|
"blitiri.com.ar/go/chasquid/internal/courier"
|
||||||
|
"blitiri.com.ar/go/chasquid/internal/domaininfo"
|
||||||
"blitiri.com.ar/go/chasquid/internal/dovecot"
|
"blitiri.com.ar/go/chasquid/internal/dovecot"
|
||||||
"blitiri.com.ar/go/chasquid/internal/maillog"
|
"blitiri.com.ar/go/chasquid/internal/maillog"
|
||||||
"blitiri.com.ar/go/chasquid/internal/normalize"
|
"blitiri.com.ar/go/chasquid/internal/normalize"
|
||||||
@@ -70,8 +71,6 @@ func main() {
|
|||||||
|
|
||||||
initMailLog(conf.MailLogPath)
|
initMailLog(conf.MailLogPath)
|
||||||
|
|
||||||
go signalHandler()
|
|
||||||
|
|
||||||
if conf.MonitoringAddress != "" {
|
if conf.MonitoringAddress != "" {
|
||||||
go launchMonitoringServer(conf)
|
go launchMonitoringServer(conf)
|
||||||
}
|
}
|
||||||
@@ -132,7 +131,11 @@ func main() {
|
|||||||
// as a remote domain (for loops, alias resolutions, etc.).
|
// as a remote domain (for loops, alias resolutions, etc.).
|
||||||
s.AddDomain("localhost")
|
s.AddDomain("localhost")
|
||||||
|
|
||||||
dinfo := s.InitDomainInfo(conf.DataDir + "/domaininfo")
|
dinfo, err := domaininfo.New(conf.DataDir + "/domaininfo")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error opening domain info database: %v", err)
|
||||||
|
}
|
||||||
|
s.SetDomainInfo(dinfo)
|
||||||
|
|
||||||
stsCache, err := sts.NewCache(conf.DataDir + "/sts-cache")
|
stsCache, err := sts.NewCache(conf.DataDir + "/sts-cache")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -169,6 +172,8 @@ func main() {
|
|||||||
log.Fatalf("No address to listen on")
|
log.Fatalf("No address to listen on")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
go signalHandler(dinfo, s)
|
||||||
|
|
||||||
s.ListenAndServe()
|
s.ListenAndServe()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,7 +217,7 @@ func initMailLog(path string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func signalHandler() {
|
func signalHandler(dinfo *domaininfo.DB, srv *smtpsrv.Server) {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
signals := make(chan os.Signal, 1)
|
signals := make(chan os.Signal, 1)
|
||||||
@@ -221,6 +226,8 @@ func signalHandler() {
|
|||||||
for {
|
for {
|
||||||
switch sig := <-signals; sig {
|
switch sig := <-signals; sig {
|
||||||
case syscall.SIGHUP:
|
case syscall.SIGHUP:
|
||||||
|
log.Infof("Received SIGHUP, reloading")
|
||||||
|
|
||||||
// SIGHUP triggers a reopen of the log files. This is used for log
|
// SIGHUP triggers a reopen of the log files. This is used for log
|
||||||
// rotation.
|
// rotation.
|
||||||
err = log.Default.Reopen()
|
err = log.Default.Reopen()
|
||||||
@@ -232,6 +239,17 @@ func signalHandler() {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error reopening maillog: %v", err)
|
log.Fatalf("Error reopening maillog: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We don't want to reload the domain info database periodically,
|
||||||
|
// as it can be expensive, and it is not expected that the user
|
||||||
|
// changes this behind chasquid's back.
|
||||||
|
err = dinfo.Reload()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Error reloading domain info: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Also trigger a server reload.
|
||||||
|
srv.Reload()
|
||||||
case syscall.SIGTERM, syscall.SIGINT:
|
case syscall.SIGTERM, syscall.SIGINT:
|
||||||
log.Fatalf("Got signal to exit: %v", sig)
|
log.Fatalf("Got signal to exit: %v", sig)
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -138,15 +138,9 @@ func (s *Server) SetAliasesConfig(suffixSep, dropChars string) {
|
|||||||
s.aliasesR.ResolveHook = path.Join(s.HookPath, "alias-resolve")
|
s.aliasesR.ResolveHook = path.Join(s.HookPath, "alias-resolve")
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitDomainInfo initializes the domain info database.
|
// SetDomainInfo sets the domain info database to use.
|
||||||
func (s *Server) InitDomainInfo(dir string) *domaininfo.DB {
|
func (s *Server) SetDomainInfo(dinfo *domaininfo.DB) {
|
||||||
var err error
|
s.dinfo = dinfo
|
||||||
s.dinfo, err = domaininfo.New(dir)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Error opening domain info database: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return s.dinfo
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// InitQueue initializes the queue.
|
// InitQueue initializes the queue.
|
||||||
@@ -168,8 +162,8 @@ func (s *Server) InitQueue(path string, localC, remoteC courier.Courier) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// periodicallyReload some of the server's information, such as aliases and
|
// periodicallyReload some of the server's information that can be changed
|
||||||
// the user databases.
|
// without the server knowing, such as aliases and the user databases.
|
||||||
func (s *Server) periodicallyReload() {
|
func (s *Server) periodicallyReload() {
|
||||||
if reloadEvery == nil {
|
if reloadEvery == nil {
|
||||||
return
|
return
|
||||||
@@ -177,20 +171,20 @@ func (s *Server) periodicallyReload() {
|
|||||||
|
|
||||||
//lint:ignore SA1015 This lasts the program's lifetime.
|
//lint:ignore SA1015 This lasts the program's lifetime.
|
||||||
for range time.Tick(*reloadEvery) {
|
for range time.Tick(*reloadEvery) {
|
||||||
err := s.aliasesR.Reload()
|
s.Reload()
|
||||||
if err != nil {
|
}
|
||||||
log.Errorf("Error reloading aliases: %v", err)
|
}
|
||||||
}
|
|
||||||
|
|
||||||
err = s.authr.Reload()
|
func (s *Server) Reload() {
|
||||||
if err != nil {
|
// Note that any error while reloading is fatal: this way, if there is an
|
||||||
log.Errorf("Error reloading authenticators: %v", err)
|
// unexpected error it can be detected (and corrected) quickly, instead of
|
||||||
}
|
// much later (e.g. upon restart) when it might be harder to debug.
|
||||||
|
if err := s.aliasesR.Reload(); err != nil {
|
||||||
|
log.Fatalf("Error reloading aliases: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
err = s.dinfo.Reload()
|
if err := s.authr.Reload(); err != nil {
|
||||||
if err != nil {
|
log.Fatalf("Error reloading authenticators: %v", err)
|
||||||
log.Errorf("Error reloading domaininfo: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"blitiri.com.ar/go/chasquid/internal/aliases"
|
"blitiri.com.ar/go/chasquid/internal/aliases"
|
||||||
|
"blitiri.com.ar/go/chasquid/internal/domaininfo"
|
||||||
"blitiri.com.ar/go/chasquid/internal/maillog"
|
"blitiri.com.ar/go/chasquid/internal/maillog"
|
||||||
"blitiri.com.ar/go/chasquid/internal/testlib"
|
"blitiri.com.ar/go/chasquid/internal/testlib"
|
||||||
"blitiri.com.ar/go/chasquid/internal/userdb"
|
"blitiri.com.ar/go/chasquid/internal/userdb"
|
||||||
@@ -593,7 +594,13 @@ func realMain(m *testing.M) int {
|
|||||||
s.AddAddr(submissionTLSAddr, ModeSubmissionTLS)
|
s.AddAddr(submissionTLSAddr, ModeSubmissionTLS)
|
||||||
|
|
||||||
s.InitQueue(tmpDir+"/queue", localC, remoteC)
|
s.InitQueue(tmpDir+"/queue", localC, remoteC)
|
||||||
s.InitDomainInfo(tmpDir + "/domaininfo")
|
|
||||||
|
dinfo, err := domaininfo.New(tmpDir + "/domaininfo")
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error initializing domaininfo: %v", err)
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
s.SetDomainInfo(dinfo)
|
||||||
|
|
||||||
udb := userdb.New("/dev/null")
|
udb := userdb.New("/dev/null")
|
||||||
udb.AddUser("testuser", "testpasswd")
|
udb.AddUser("testuser", "testpasswd")
|
||||||
|
|||||||
Reference in New Issue
Block a user