From c60c8d3f15e286181875df1e58c0737e8948caab Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 12 Nov 2018 02:26:52 -0600 Subject: [PATCH] make norify close flag atomic --- backend/app/notify/notify.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/backend/app/notify/notify.go b/backend/app/notify/notify.go index bdec6404..d9b8268b 100644 --- a/backend/app/notify/notify.go +++ b/backend/app/notify/notify.go @@ -6,6 +6,7 @@ import ( "fmt" "log" "sync" + "sync/atomic" "github.com/go-pkgz/repeater" "github.com/go-pkgz/repeater/strategy" @@ -14,11 +15,6 @@ import ( "github.com/umputun/remark/backend/app/store/service" ) -type request struct { - comment store.Comment - parent store.Comment -} - // Destination defines interface for a given destination service, like telegram, email and so on type Destination interface { fmt.Stringer @@ -31,11 +27,16 @@ type Service struct { destinations []Destination queue chan request - closed bool + closed uint32 // non-zero means closed. uses uint instead of bool for atomic ctx context.Context cancel context.CancelFunc } +type request struct { + comment store.Comment + parent store.Comment +} + const defaultQueueSize = 100 const uiNav = "#remark42__comment-" @@ -55,13 +56,13 @@ func NewService(dataService *service.DataStore, size int, destinations ...Destin if len(destinations) > 0 { go res.do() } - log.Printf("[INFO] create notifier service, queue size=%d", size) + log.Printf("[INFO] create notifier service, queue size=%d, destinations=%d", size, len(destinations)) return &res } // Submit comment to internal channel if not busy, drop if can't send func (s *Service) Submit(comment store.Comment) { - if len(s.destinations) == 0 || s.closed { + if len(s.destinations) == 0 || atomic.LoadUint32(&s.closed) != 0 { return } parentComment := store.Comment{} @@ -85,7 +86,7 @@ func (s *Service) Close() { s.cancel() <-s.ctx.Done() } - s.closed = true + atomic.StoreUint32(&s.closed, 1) } func (s *Service) do() {