1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-28 06:42:00 +03:00

fix: resolve data race in PushNotificationProcessor

- Add sync.RWMutex to PushNotificationProcessor struct
- Protect enabled field access with read/write locks in IsEnabled() and SetEnabled()
- Use thread-safe IsEnabled() method in ProcessPendingNotifications()
- Fix concurrent access to enabled field that was causing data races

This resolves the race condition between goroutines calling IsEnabled() and
SetEnabled() concurrently, ensuring thread-safe access to the enabled field.
This commit is contained in:
Nedyalko Dyakov
2025-06-27 00:22:44 +03:00
parent 70231ae4e9
commit 958fb1a760

View File

@ -97,6 +97,7 @@ func (r *PushNotificationRegistry) HasHandlers() bool {
type PushNotificationProcessor struct { type PushNotificationProcessor struct {
registry *PushNotificationRegistry registry *PushNotificationRegistry
enabled bool enabled bool
mu sync.RWMutex // Protects enabled field
} }
// NewPushNotificationProcessor creates a new push notification processor. // NewPushNotificationProcessor creates a new push notification processor.
@ -109,11 +110,15 @@ func NewPushNotificationProcessor(enabled bool) *PushNotificationProcessor {
// IsEnabled returns whether push notification processing is enabled. // IsEnabled returns whether push notification processing is enabled.
func (p *PushNotificationProcessor) IsEnabled() bool { func (p *PushNotificationProcessor) IsEnabled() bool {
p.mu.RLock()
defer p.mu.RUnlock()
return p.enabled return p.enabled
} }
// SetEnabled enables or disables push notification processing. // SetEnabled enables or disables push notification processing.
func (p *PushNotificationProcessor) SetEnabled(enabled bool) { func (p *PushNotificationProcessor) SetEnabled(enabled bool) {
p.mu.Lock()
defer p.mu.Unlock()
p.enabled = enabled p.enabled = enabled
} }
@ -124,7 +129,7 @@ func (p *PushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
// ProcessPendingNotifications checks for and processes any pending push notifications. // ProcessPendingNotifications checks for and processes any pending push notifications.
func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error { func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
if !p.enabled || !p.registry.HasHandlers() { if !p.IsEnabled() || !p.registry.HasHandlers() {
return nil return nil
} }