From 66a4288ec9b4cee7c50dc2d71735b99db735c5d7 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Tue, 23 Jun 2020 16:46:38 +0200 Subject: [PATCH] Throttle queue notifications --- api/scanner/queue.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/api/scanner/queue.go b/api/scanner/queue.go index ccd9643d..c15b513f 100644 --- a/api/scanner/queue.go +++ b/api/scanner/queue.go @@ -5,10 +5,12 @@ import ( "fmt" "log" "sync" + "time" "github.com/pkg/errors" "github.com/viktorstrate/photoview/api/graphql/models" "github.com/viktorstrate/photoview/api/graphql/notification" + "github.com/viktorstrate/photoview/api/utils" ) type ScannerJob struct { @@ -48,6 +50,9 @@ func InitializeScannerQueue(db *sql.DB) { } func (queue *ScannerQueue) startBackgroundWorker() { + + notifyThrottle := utils.NewThrottle(500 * time.Millisecond) + for { log.Println("Queue waiting") <-queue.idle_chan @@ -95,11 +100,13 @@ func (queue *ScannerQueue) startBackgroundWorker() { Positive: true, }) } else { - notification.BroadcastNotification(&models.Notification{ - Key: "global-scanner-progress", - Type: models.NotificationTypeMessage, - Header: fmt.Sprintf("Scanning photos"), - Content: fmt.Sprintf("%d jobs in progress\n%d jobs waiting", in_progress_length, up_next_length), + notifyThrottle.Trigger(func() { + notification.BroadcastNotification(&models.Notification{ + Key: "global-scanner-progress", + Type: models.NotificationTypeMessage, + Header: fmt.Sprintf("Scanning photos"), + Content: fmt.Sprintf("%d jobs in progress\n%d jobs waiting", in_progress_length, up_next_length), + }) }) }