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:
@ -97,6 +97,7 @@ func (r *PushNotificationRegistry) HasHandlers() bool {
|
||||
type PushNotificationProcessor struct {
|
||||
registry *PushNotificationRegistry
|
||||
enabled bool
|
||||
mu sync.RWMutex // Protects enabled field
|
||||
}
|
||||
|
||||
// NewPushNotificationProcessor creates a new push notification processor.
|
||||
@ -109,11 +110,15 @@ func NewPushNotificationProcessor(enabled bool) *PushNotificationProcessor {
|
||||
|
||||
// IsEnabled returns whether push notification processing is enabled.
|
||||
func (p *PushNotificationProcessor) IsEnabled() bool {
|
||||
p.mu.RLock()
|
||||
defer p.mu.RUnlock()
|
||||
return p.enabled
|
||||
}
|
||||
|
||||
// SetEnabled enables or disables push notification processing.
|
||||
func (p *PushNotificationProcessor) SetEnabled(enabled bool) {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
p.enabled = enabled
|
||||
}
|
||||
|
||||
@ -124,7 +129,7 @@ func (p *PushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
|
||||
|
||||
// ProcessPendingNotifications checks for and processes any pending push notifications.
|
||||
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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user