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
```
Этот коммит содержится в:
Agniva De Sarker
2022-02-08 12:20:02 +05:30
коммит произвёл GitHub
родитель 569ee94116
Коммит 6605300c66

Просмотреть файл

@@ -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 {