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

lifecycle: Don't create multiple notify channels (#435)

Signed-off-by: James Hillyerd <james@hillyerd.com>
This commit is contained in:
James Hillyerd
2023-11-13 11:54:14 -08:00
committed by GitHub
parent 535438342e
commit 843cb8a015

View File

@@ -67,7 +67,7 @@ func FullAssembly(conf *config.Root) (*Services, error) {
} }
smtpServer := smtp.NewServer(conf.SMTP, mmanager, addrPolicy, extHost) smtpServer := smtp.NewServer(conf.SMTP, mmanager, addrPolicy, extHost)
return &Services{ s := &Services{
MsgHub: msgHub, MsgHub: msgHub,
RetentionScanner: retentionScanner, RetentionScanner: retentionScanner,
POP3Server: pop3Server, POP3Server: pop3Server,
@@ -76,7 +76,10 @@ func FullAssembly(conf *config.Root) (*Services, error) {
ExtHost: extHost, ExtHost: extHost,
LuaHost: luaHost, LuaHost: luaHost,
ready: &sync.WaitGroup{}, ready: &sync.WaitGroup{},
}, nil }
s.setupNotify()
return s, nil
} }
// Start all services, returns immediately. Callers may use Notify to detect failed services. // Start all services, returns immediately. Callers may use Notify to detect failed services.
@@ -94,10 +97,16 @@ func (s *Services) Start(ctx context.Context, readyFunc func()) {
}() }()
} }
// Notify merges the error notification channels of all fallible services, allowing the process to // Notify returns a merged channel of the error notification channels of all fallible services,
// be shutdown if needed. // allowing the process to be shutdown if needed.
func (s *Services) Notify() <-chan error { func (s *Services) Notify() <-chan error {
return s.notify
}
// setupNotify merges the error notification channels of all fallible services.
func (s *Services) setupNotify() {
c := make(chan error, 1) c := make(chan error, 1)
s.notify = c
go func() { go func() {
// TODO: What level to log failure. // TODO: What level to log failure.
select { select {
@@ -109,10 +118,10 @@ func (s *Services) Notify() <-chan error {
c <- err c <- err
} }
}() }()
return c
} }
// makeReadyFunc returns a function used to signal that a service is ready. The `Services.ready`
// wait group can then be used to await all services being ready.
func (s *Services) makeReadyFunc() func() { func (s *Services) makeReadyFunc() func() {
s.ready.Add(1) s.ready.Add(1)
var once sync.Once var once sync.Once