From 6893c729c41a07d1c1dbb305468e07d4b2b8f61e Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Tue, 4 May 2021 22:10:32 +0100 Subject: [PATCH 1/5] Hold the event source when there are no listeners The event source does not need to run when there are no listeners. Therefore pause it when there are none. Signed-off-by: Andrew Thornton --- modules/eventsource/manager.go | 6 ++++++ modules/eventsource/manager_run.go | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/modules/eventsource/manager.go b/modules/eventsource/manager.go index 212fe60569698..812d6739929da 100644 --- a/modules/eventsource/manager.go +++ b/modules/eventsource/manager.go @@ -13,6 +13,7 @@ type Manager struct { mutex sync.Mutex messengers map[int64]*Messenger + connection chan struct{} } var manager *Manager @@ -20,6 +21,7 @@ var manager *Manager func init() { manager = &Manager{ messengers: make(map[int64]*Messenger), + connection: make(chan struct{}, 1), } } @@ -36,6 +38,10 @@ func (m *Manager) Register(uid int64) <-chan *Event { messenger = NewMessenger(uid) m.messengers[uid] = messenger } + select { + case m.connection <- struct{}{}: + default: + } m.mutex.Unlock() return messenger.Register() } diff --git a/modules/eventsource/manager_run.go b/modules/eventsource/manager_run.go index ccfe2e07097a0..ebf3d8c96a7de 100644 --- a/modules/eventsource/manager_run.go +++ b/modules/eventsource/manager_run.go @@ -34,6 +34,31 @@ loop: timer.Stop() break loop case <-timer.C: + m.mutex.Lock() + connectionCount := len(m.messengers) + if connectionCount == 0 { + // empty the connection channel + select { + case <-m.connection: + default: + } + } + m.mutex.Unlock() + if connectionCount == 0 { + // No listeners so the source can be paused + timer.Stop() + select { + case <-ctx.Done(): + timer.Stop() + break loop + case <-m.connection: + // OK we're back so lets reset the timer and start again + // We won't change the "then" time because there could be concurrency issues + timer.Reset(setting.UI.Notification.EventSourceUpdateTime) + continue + } + } + now := timeutil.TimeStampNow().Add(-2) uidCounts, err := models.GetUIDsAndNotificationCounts(then, now) From fda733d85144c3a93c445955515d59e7c6b08994 Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 4 May 2021 23:08:15 +0100 Subject: [PATCH 2/5] Apply suggestions from code review --- modules/eventsource/manager_run.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/eventsource/manager_run.go b/modules/eventsource/manager_run.go index ebf3d8c96a7de..2d2ff7c55dcf5 100644 --- a/modules/eventsource/manager_run.go +++ b/modules/eventsource/manager_run.go @@ -49,12 +49,14 @@ loop: timer.Stop() select { case <-ctx.Done(): - timer.Stop() break loop case <-m.connection: // OK we're back so lets reset the timer and start again // We won't change the "then" time because there could be concurrency issues - timer.Reset(setting.UI.Notification.EventSourceUpdateTime) + select { + case <-timer.C: + default: + } continue } } From f74edf2f7be614de2c98f70721a8757637a778d3 Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 4 May 2021 23:30:34 +0100 Subject: [PATCH 3/5] Apply suggestions from code review --- modules/eventsource/manager_run.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/eventsource/manager_run.go b/modules/eventsource/manager_run.go index 2d2ff7c55dcf5..618552096b279 100644 --- a/modules/eventsource/manager_run.go +++ b/modules/eventsource/manager_run.go @@ -54,8 +54,8 @@ loop: // OK we're back so lets reset the timer and start again // We won't change the "then" time because there could be concurrency issues select { - case <-timer.C: - default: + case <-timer.C: + default: } continue } From bead9f0195435bcb2f65d95c134fd560deed0e71 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 5 May 2021 17:01:03 +0100 Subject: [PATCH 4/5] Remove spurious timer.Stop() Signed-off-by: Andrew Thornton --- modules/eventsource/manager_run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/eventsource/manager_run.go b/modules/eventsource/manager_run.go index 618552096b279..732133be976be 100644 --- a/modules/eventsource/manager_run.go +++ b/modules/eventsource/manager_run.go @@ -37,6 +37,7 @@ loop: m.mutex.Lock() connectionCount := len(m.messengers) if connectionCount == 0 { + log.Debug("No listeners") // empty the connection channel select { case <-m.connection: @@ -46,7 +47,6 @@ loop: m.mutex.Unlock() if connectionCount == 0 { // No listeners so the source can be paused - timer.Stop() select { case <-ctx.Done(): break loop From 407df1397ed4d1ce6ee825aabdd18181d58dfd2d Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Wed, 5 May 2021 17:02:21 +0100 Subject: [PATCH 5/5] add some more logging Signed-off-by: Andrew Thornton --- modules/eventsource/manager_run.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/modules/eventsource/manager_run.go b/modules/eventsource/manager_run.go index 732133be976be..60598ecb495f5 100644 --- a/modules/eventsource/manager_run.go +++ b/modules/eventsource/manager_run.go @@ -37,7 +37,7 @@ loop: m.mutex.Lock() connectionCount := len(m.messengers) if connectionCount == 0 { - log.Debug("No listeners") + log.Trace("Event source has no listeners") // empty the connection channel select { case <-m.connection: @@ -47,10 +47,12 @@ loop: m.mutex.Unlock() if connectionCount == 0 { // No listeners so the source can be paused + log.Trace("Pausing the eventsource") select { case <-ctx.Done(): break loop case <-m.connection: + log.Trace("Connection detected - restarting the eventsource") // OK we're back so lets reset the timer and start again // We won't change the "then" time because there could be concurrency issues select {