From 6605300c66e482e79a06fac98139fe1c3cefa330 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Tue, 8 Feb 2022 12:20:02 +0530 Subject: [PATCH] MM-41085: Use a counting semaphore in sendNotifications (#19498) We throttle the concurrency limit in maintaining thread membership for a given thread using a counting semaphore. The limit is currently 8 which is a decent number to start with. On some more thinking, it would be even better if the sql query could be modified to support batch updates. https://mattermost.atlassian.net/browse/MM-41085 ```release-note NONE ``` --- app/notification.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/notification.go b/app/notification.go index d5dc8be664..bb9c642a3d 100644 --- a/app/notification.go +++ b/app/notification.go @@ -219,11 +219,22 @@ func (a *App) SendNotifications(post *model.Post, team *model.Team, channel *mod for id := range mentions.Mentions { threadParticipants[id] = true } + + // sema is a counting semaphore to throttle the number of concurrent DB requests. + // A concurrency of 8 should be sufficient. + // We don't want to set a higher limit which can bring down the DB. + sema := make(chan struct{}, 8) // for each mention, make sure to update thread autofollow (if enabled) and update increment mention count for id := range threadParticipants { mac := make(chan *model.AppError, 1) + // Get token. + sema <- struct{}{} go func(userID string) { - defer close(mac) + defer func() { + close(mac) + // Release token. + <-sema + }() mentionType, incrementMentions := mentions.Mentions[userID] // if the user was not explicitly mentioned, check if they explicitly unfollowed the thread if !incrementMentions {