This is an automated email from the ASF dual-hosted git repository.
LinkinStars pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/answer-plugins.git
The following commit(s) were added to refs/heads/main by this push:
new ce22fdd fix: synchronize notification user configuration cache access
(#336)
ce22fdd is described below
commit ce22fdd12a2f6e42b8ebf2267b05195dd04587b1
Author: LinkinStars <[email protected]>
AuthorDate: Fri Nov 13 17:09:36 2026 +0800
fix: synchronize notification user configuration cache access (#336)
Synchronize access to notification user configuration caches across
supported notification plugins.
Adds concurrent regression coverage for configuration updates and
subscriber snapshot reads.
Validation: `go test -race ./...` in each affected plugin module.
---
notification-dingtalk/dingtalk_notification.go | 7 +--
notification-dingtalk/user_config.go | 15 ++++++-
.../user_config_concurrency_test.go | 51 ++++++++++++++++++++++
notification-lark/notification.go | 7 +--
notification-lark/user_config.go | 15 ++++++-
notification-lark/user_config_concurrency_test.go | 51 ++++++++++++++++++++++
notification-slack/slack_notification.go | 7 +--
notification-slack/user_config.go | 15 ++++++-
notification-slack/user_config_concurrency_test.go | 51 ++++++++++++++++++++++
notification-wecom/notification_wecom.go | 7 +--
notification-wecom/user_config.go | 15 ++++++-
notification-wecom/user_config_concurrency_test.go | 51 ++++++++++++++++++++++
user-center-slack/notification.go | 7 +--
user-center-slack/user_config.go | 15 ++++++-
user-center-slack/user_config_concurrency_test.go | 51 ++++++++++++++++++++++
user-center-wecom/notification.go | 7 +--
user-center-wecom/user_config.go | 15 ++++++-
user-center-wecom/user_config_concurrency_test.go | 51 ++++++++++++++++++++++
18 files changed, 396 insertions(+), 42 deletions(-)
diff --git a/notification-dingtalk/dingtalk_notification.go
b/notification-dingtalk/dingtalk_notification.go
index 8db3677..f295a20 100644
--- a/notification-dingtalk/dingtalk_notification.go
+++ b/notification-dingtalk/dingtalk_notification.go
@@ -63,12 +63,7 @@ func (n *Notification) Info() plugin.Info {
// GetNewQuestionSubscribers returns the subscribers of the new question
notification
func (n *Notification) GetNewQuestionSubscribers() (userIDs []string) {
- for userID, conf := range n.UserConfigCache.userConfigMapping {
- if conf.AllNewQuestions {
- userIDs = append(userIDs, userID)
- }
- }
- return userIDs
+ return n.UserConfigCache.NewQuestionSubscriberIDs()
}
// Notify sends a notification to the user
diff --git a/notification-dingtalk/user_config.go
b/notification-dingtalk/user_config.go
index 5cfdad4..8ec58f8 100644
--- a/notification-dingtalk/user_config.go
+++ b/notification-dingtalk/user_config.go
@@ -39,7 +39,7 @@ type UserConfig struct {
type UserConfigCache struct {
// key: userID value: user config
userConfigMapping map[string]*UserConfig
- sync.Mutex
+ sync.RWMutex
}
func NewUserConfigCache() *UserConfigCache {
@@ -55,6 +55,19 @@ func (ucc *UserConfigCache) SetUserConfig(userID string,
config *UserConfig) {
ucc.userConfigMapping[userID] = config
}
+func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
+ ucc.RLock()
+ defer ucc.RUnlock()
+
+ userIDs := make([]string, 0)
+ for userID, conf := range ucc.userConfigMapping {
+ if conf.AllNewQuestions {
+ userIDs = append(userIDs, userID)
+ }
+ }
+ return userIDs
+}
+
func (n *Notification) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
diff --git a/notification-dingtalk/user_config_concurrency_test.go
b/notification-dingtalk/user_config_concurrency_test.go
new file mode 100644
index 0000000..3da3834
--- /dev/null
+++ b/notification-dingtalk/user_config_concurrency_test.go
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0.
+ */
+
+package dingtalk
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestUserConfigCacheConcurrentNewQuestionSubscribers(t *testing.T) {
+ cache := NewUserConfigCache()
+ const workers, iterations = 8, 500
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for worker := range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for i := range iterations {
+
cache.SetUserConfig(fmt.Sprintf("writer-%d-user-%d", worker, i%32),
&UserConfig{AllNewQuestions: i%2 == 0})
+ }
+ }()
+ }
+ for range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for range iterations {
+ _ = cache.NewQuestionSubscriberIDs()
+ }
+ }()
+ }
+ close(start)
+ wg.Wait()
+
+ cache.SetUserConfig("subscriber", &UserConfig{AllNewQuestions: true})
+ for _, userID := range cache.NewQuestionSubscriberIDs() {
+ if userID == "subscriber" {
+ return
+ }
+ }
+ t.Fatal("subscriber missing from snapshot")
+}
diff --git a/notification-lark/notification.go
b/notification-lark/notification.go
index c9954cb..3e59988 100644
--- a/notification-lark/notification.go
+++ b/notification-lark/notification.go
@@ -211,12 +211,7 @@ func makeCardMsg(args plugin.NotificationMessage) Card {
// GetNewQuestionSubscribers returns the subscribers of the new question
notification
func (n *Notification) GetNewQuestionSubscribers() (userIDs []string) {
- for userID, conf := range n.userConfigCache.userConfigMapping {
- if conf.AllNewQuestions {
- userIDs = append(userIDs, userID)
- }
- }
- return userIDs
+ return n.userConfigCache.NewQuestionSubscriberIDs()
}
// Notify sends a notification to the user
diff --git a/notification-lark/user_config.go b/notification-lark/user_config.go
index 2a62468..57f0468 100644
--- a/notification-lark/user_config.go
+++ b/notification-lark/user_config.go
@@ -39,7 +39,7 @@ type UserConfig struct {
type UserConfigCache struct {
userConfigMapping map[string]*UserConfig
- sync.Mutex
+ sync.RWMutex
}
func NewUserConfigCache() *UserConfigCache {
@@ -55,6 +55,19 @@ func (ucc *UserConfigCache) SetUserConfig(userID string,
config *UserConfig) {
ucc.userConfigMapping[userID] = config
}
+func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
+ ucc.RLock()
+ defer ucc.RUnlock()
+
+ userIDs := make([]string, 0)
+ for userID, conf := range ucc.userConfigMapping {
+ if conf.AllNewQuestions {
+ userIDs = append(userIDs, userID)
+ }
+ }
+ return userIDs
+}
+
func (n *Notification) UserConfigFields() []plugin.ConfigField {
return []plugin.ConfigField{
{
diff --git a/notification-lark/user_config_concurrency_test.go
b/notification-lark/user_config_concurrency_test.go
new file mode 100644
index 0000000..a466c08
--- /dev/null
+++ b/notification-lark/user_config_concurrency_test.go
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0.
+ */
+
+package lark
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestUserConfigCacheConcurrentNewQuestionSubscribers(t *testing.T) {
+ cache := NewUserConfigCache()
+ const workers, iterations = 8, 500
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for worker := range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for i := range iterations {
+
cache.SetUserConfig(fmt.Sprintf("writer-%d-user-%d", worker, i%32),
&UserConfig{AllNewQuestions: i%2 == 0})
+ }
+ }()
+ }
+ for range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for range iterations {
+ _ = cache.NewQuestionSubscriberIDs()
+ }
+ }()
+ }
+ close(start)
+ wg.Wait()
+
+ cache.SetUserConfig("subscriber", &UserConfig{AllNewQuestions: true})
+ for _, userID := range cache.NewQuestionSubscriberIDs() {
+ if userID == "subscriber" {
+ return
+ }
+ }
+ t.Fatal("subscriber missing from snapshot")
+}
diff --git a/notification-slack/slack_notification.go
b/notification-slack/slack_notification.go
index 6d1ba9a..dfb3ea3 100644
--- a/notification-slack/slack_notification.go
+++ b/notification-slack/slack_notification.go
@@ -63,12 +63,7 @@ func (n *Notification) Info() plugin.Info {
// GetNewQuestionSubscribers returns the subscribers of the new question
notification
func (n *Notification) GetNewQuestionSubscribers() (userIDs []string) {
- for userID, conf := range n.UserConfigCache.userConfigMapping {
- if conf.AllNewQuestions {
- userIDs = append(userIDs, userID)
- }
- }
- return userIDs
+ return n.UserConfigCache.NewQuestionSubscriberIDs()
}
// Notify sends a notification to the user
diff --git a/notification-slack/user_config.go
b/notification-slack/user_config.go
index 7ea077e..d37c02e 100644
--- a/notification-slack/user_config.go
+++ b/notification-slack/user_config.go
@@ -43,7 +43,7 @@ type UserConfig struct {
type UserConfigCache struct {
// key: userID value: user config
userConfigMapping map[string]*UserConfig
- sync.Mutex
+ sync.RWMutex
}
func NewUserConfigCache() *UserConfigCache {
@@ -59,6 +59,19 @@ func (ucc *UserConfigCache) SetUserConfig(userID string,
config *UserConfig) {
ucc.userConfigMapping[userID] = config
}
+func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
+ ucc.RLock()
+ defer ucc.RUnlock()
+
+ userIDs := make([]string, 0)
+ for userID, conf := range ucc.userConfigMapping {
+ if conf.AllNewQuestions {
+ userIDs = append(userIDs, userID)
+ }
+ }
+ return userIDs
+}
+
func (n *Notification) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
diff --git a/notification-slack/user_config_concurrency_test.go
b/notification-slack/user_config_concurrency_test.go
new file mode 100644
index 0000000..4afb891
--- /dev/null
+++ b/notification-slack/user_config_concurrency_test.go
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0.
+ */
+
+package slack_notification
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestUserConfigCacheConcurrentNewQuestionSubscribers(t *testing.T) {
+ cache := NewUserConfigCache()
+ const workers, iterations = 8, 500
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for worker := range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for i := range iterations {
+
cache.SetUserConfig(fmt.Sprintf("writer-%d-user-%d", worker, i%32),
&UserConfig{AllNewQuestions: i%2 == 0})
+ }
+ }()
+ }
+ for range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for range iterations {
+ _ = cache.NewQuestionSubscriberIDs()
+ }
+ }()
+ }
+ close(start)
+ wg.Wait()
+
+ cache.SetUserConfig("subscriber", &UserConfig{AllNewQuestions: true})
+ for _, userID := range cache.NewQuestionSubscriberIDs() {
+ if userID == "subscriber" {
+ return
+ }
+ }
+ t.Fatal("subscriber missing from snapshot")
+}
diff --git a/notification-wecom/notification_wecom.go
b/notification-wecom/notification_wecom.go
index 0de5a8b..afdc0fd 100644
--- a/notification-wecom/notification_wecom.go
+++ b/notification-wecom/notification_wecom.go
@@ -62,12 +62,7 @@ func (*Notification) Info() plugin.Info {
// GetNewQuestionSubscribers returns the subscribers of the new question
notification
func (n *Notification) GetNewQuestionSubscribers() (userIDs []string) {
- for userID, conf := range n.UserConfigCache.userConfigMapping {
- if conf.AllNewQuestions {
- userIDs = append(userIDs, userID)
- }
- }
- return userIDs
+ return n.UserConfigCache.NewQuestionSubscriberIDs()
}
// Notify sends a notification to the user
diff --git a/notification-wecom/user_config.go
b/notification-wecom/user_config.go
index f61e249..17ff018 100644
--- a/notification-wecom/user_config.go
+++ b/notification-wecom/user_config.go
@@ -39,7 +39,7 @@ type UserConfig struct {
type UserConfigCache struct {
// key: userID value: user config
userConfigMapping map[string]*UserConfig
- sync.Mutex
+ sync.RWMutex
}
func NewUserConfigCache() *UserConfigCache {
@@ -55,6 +55,19 @@ func (ucc *UserConfigCache) SetUserConfig(userID string,
config *UserConfig) {
ucc.userConfigMapping[userID] = config
}
+func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
+ ucc.RLock()
+ defer ucc.RUnlock()
+
+ userIDs := make([]string, 0)
+ for userID, conf := range ucc.userConfigMapping {
+ if conf.AllNewQuestions {
+ userIDs = append(userIDs, userID)
+ }
+ }
+ return userIDs
+}
+
func (n *Notification) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
diff --git a/notification-wecom/user_config_concurrency_test.go
b/notification-wecom/user_config_concurrency_test.go
new file mode 100644
index 0000000..8ea44c6
--- /dev/null
+++ b/notification-wecom/user_config_concurrency_test.go
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0.
+ */
+
+package wecom
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestUserConfigCacheConcurrentNewQuestionSubscribers(t *testing.T) {
+ cache := NewUserConfigCache()
+ const workers, iterations = 8, 500
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for worker := range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for i := range iterations {
+
cache.SetUserConfig(fmt.Sprintf("writer-%d-user-%d", worker, i%32),
&UserConfig{AllNewQuestions: i%2 == 0})
+ }
+ }()
+ }
+ for range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for range iterations {
+ _ = cache.NewQuestionSubscriberIDs()
+ }
+ }()
+ }
+ close(start)
+ wg.Wait()
+
+ cache.SetUserConfig("subscriber", &UserConfig{AllNewQuestions: true})
+ for _, userID := range cache.NewQuestionSubscriberIDs() {
+ if userID == "subscriber" {
+ return
+ }
+ }
+ t.Fatal("subscriber missing from snapshot")
+}
diff --git a/user-center-slack/notification.go
b/user-center-slack/notification.go
index c647e73..a75a502 100644
--- a/user-center-slack/notification.go
+++ b/user-center-slack/notification.go
@@ -31,12 +31,7 @@ import (
// GetNewQuestionSubscribers returns the subscribers of the new question
notification
func (uc *UserCenter) GetNewQuestionSubscribers() (userIDs []string) {
- for userID, conf := range uc.UserConfigCache.userConfigMapping {
- if conf.AllNewQuestions {
- userIDs = append(userIDs, userID)
- }
- }
- return userIDs
+ return uc.UserConfigCache.NewQuestionSubscriberIDs()
}
// Notify sends a notification to the user
diff --git a/user-center-slack/user_config.go b/user-center-slack/user_config.go
index 5f25434..92d1f48 100644
--- a/user-center-slack/user_config.go
+++ b/user-center-slack/user_config.go
@@ -43,7 +43,7 @@ type UserConfig struct {
type UserConfigCache struct {
// key: userID value: user config
userConfigMapping map[string]*UserConfig
- sync.Mutex
+ sync.RWMutex
}
func NewUserConfigCache() *UserConfigCache {
@@ -59,6 +59,19 @@ func (ucc *UserConfigCache) SetUserConfig(userID string,
config *UserConfig) {
ucc.userConfigMapping[userID] = config
}
+func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
+ ucc.RLock()
+ defer ucc.RUnlock()
+
+ userIDs := make([]string, 0)
+ for userID, conf := range ucc.userConfigMapping {
+ if conf.AllNewQuestions {
+ userIDs = append(userIDs, userID)
+ }
+ }
+ return userIDs
+}
+
func (uc *UserCenter) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
diff --git a/user-center-slack/user_config_concurrency_test.go
b/user-center-slack/user_config_concurrency_test.go
new file mode 100644
index 0000000..00c7c1f
--- /dev/null
+++ b/user-center-slack/user_config_concurrency_test.go
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0.
+ */
+
+package slack_user_center
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestUserConfigCacheConcurrentNewQuestionSubscribers(t *testing.T) {
+ cache := NewUserConfigCache()
+ const workers, iterations = 8, 500
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for worker := range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for i := range iterations {
+
cache.SetUserConfig(fmt.Sprintf("writer-%d-user-%d", worker, i%32),
&UserConfig{AllNewQuestions: i%2 == 0})
+ }
+ }()
+ }
+ for range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for range iterations {
+ _ = cache.NewQuestionSubscriberIDs()
+ }
+ }()
+ }
+ close(start)
+ wg.Wait()
+
+ cache.SetUserConfig("subscriber", &UserConfig{AllNewQuestions: true})
+ for _, userID := range cache.NewQuestionSubscriberIDs() {
+ if userID == "subscriber" {
+ return
+ }
+ }
+ t.Fatal("subscriber missing from snapshot")
+}
diff --git a/user-center-wecom/notification.go
b/user-center-wecom/notification.go
index d22f042..4ce63c8 100644
--- a/user-center-wecom/notification.go
+++ b/user-center-wecom/notification.go
@@ -31,12 +31,7 @@ import (
// GetNewQuestionSubscribers returns the subscribers of the new question
notification
func (uc *UserCenter) GetNewQuestionSubscribers() (userIDs []string) {
- for userID, conf := range uc.UserConfigCache.userConfigMapping {
- if conf.AllNewQuestions {
- userIDs = append(userIDs, userID)
- }
- }
- return userIDs
+ return uc.UserConfigCache.NewQuestionSubscriberIDs()
}
// Notify sends a notification to the user
diff --git a/user-center-wecom/user_config.go b/user-center-wecom/user_config.go
index 2a7cabe..5f61061 100644
--- a/user-center-wecom/user_config.go
+++ b/user-center-wecom/user_config.go
@@ -38,7 +38,7 @@ type UserConfig struct {
type UserConfigCache struct {
// key: userID value: user config
userConfigMapping map[string]*UserConfig
- sync.Mutex
+ sync.RWMutex
}
func NewUserConfigCache() *UserConfigCache {
@@ -54,6 +54,19 @@ func (ucc *UserConfigCache) SetUserConfig(userID string,
config *UserConfig) {
ucc.userConfigMapping[userID] = config
}
+func (ucc *UserConfigCache) NewQuestionSubscriberIDs() []string {
+ ucc.RLock()
+ defer ucc.RUnlock()
+
+ userIDs := make([]string, 0)
+ for userID, conf := range ucc.userConfigMapping {
+ if conf.AllNewQuestions {
+ userIDs = append(userIDs, userID)
+ }
+ }
+ return userIDs
+}
+
func (uc *UserCenter) UserConfigFields() []plugin.ConfigField {
fields := make([]plugin.ConfigField, 0)
// Show tip for user, if the notification service is disabled
diff --git a/user-center-wecom/user_config_concurrency_test.go
b/user-center-wecom/user_config_concurrency_test.go
new file mode 100644
index 0000000..8ea44c6
--- /dev/null
+++ b/user-center-wecom/user_config_concurrency_test.go
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0.
+ */
+
+package wecom
+
+import (
+ "fmt"
+ "sync"
+ "testing"
+)
+
+func TestUserConfigCacheConcurrentNewQuestionSubscribers(t *testing.T) {
+ cache := NewUserConfigCache()
+ const workers, iterations = 8, 500
+ start := make(chan struct{})
+ var wg sync.WaitGroup
+ for worker := range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for i := range iterations {
+
cache.SetUserConfig(fmt.Sprintf("writer-%d-user-%d", worker, i%32),
&UserConfig{AllNewQuestions: i%2 == 0})
+ }
+ }()
+ }
+ for range workers {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ <-start
+ for range iterations {
+ _ = cache.NewQuestionSubscriberIDs()
+ }
+ }()
+ }
+ close(start)
+ wg.Wait()
+
+ cache.SetUserConfig("subscriber", &UserConfig{AllNewQuestions: true})
+ for _, userID := range cache.NewQuestionSubscriberIDs() {
+ if userID == "subscriber" {
+ return
+ }
+ }
+ t.Fatal("subscriber missing from snapshot")
+}