diff --git a/backend/app/cmd/server.go b/backend/app/cmd/server.go index 68ed725a..88acccf5 100644 --- a/backend/app/cmd/server.go +++ b/backend/app/cmd/server.go @@ -229,7 +229,7 @@ func (s *ServerCommand) newServerApp() (*serverApp, error) { notifyService, err := s.makeNotify(dataService) if err != nil { log.Printf("[WARN] failed to make notify service, %s", err) - notifyService = nil // disable notifier + notifyService = notify.NopService // disable notifier } authProviders := s.makeAuthProviders(jwtService, avatarProxy, dataService) @@ -474,7 +474,7 @@ func (s *ServerCommand) makeNotify(dataStore *service.DataStore) (*notify.Servic } return notify.NewService(dataStore, s.Notify.QueueSize, tg), nil case "none": - return notify.NewService(dataStore, s.Notify.QueueSize), nil + return notify.NopService, nil } return nil, errors.Errorf("unsupported notification type %q", s.Notify.Type) } diff --git a/backend/app/notify/notify.go b/backend/app/notify/notify.go index a7f31efc..77c44d87 100644 --- a/backend/app/notify/notify.go +++ b/backend/app/notify/notify.go @@ -76,9 +76,11 @@ func (s *Service) Submit(comment store.Comment) { // Close queue channel and wait for completion func (s *Service) Close() { - close(s.queue) - s.cancel() - <-s.ctx.Done() + if s.queue != nil { + close(s.queue) + s.cancel() + <-s.ctx.Done() + } s.closed = true } @@ -97,3 +99,6 @@ func (s *Service) do() { wg.Wait() } } + +// NopService is do-nothing notifier, without destinations +var NopService = &Service{} diff --git a/backend/app/notify/notify_test.go b/backend/app/notify/notify_test.go index d52ce793..6d9f88a6 100644 --- a/backend/app/notify/notify_test.go +++ b/backend/app/notify/notify_test.go @@ -80,6 +80,13 @@ func TestService_Many(t *testing.T) { assert.True(t, d2.closed) } +func TestService_Nop(t *testing.T) { + s := NopService + s.Submit(store.Comment{}) + s.Close() + assert.True(t, s.closed) +} + type mockDest struct { data []store.Comment id int