mirror of
https://github.com/redis/go-redis.git
synced 2025-07-28 06:42:00 +03:00
feat: add VoidPushNotificationProcessor for disabled push notifications
- Add VoidPushNotificationProcessor that reads and discards push notifications - Create PushNotificationProcessorInterface for consistent behavior - Always provide a processor (real or void) instead of nil - VoidPushNotificationProcessor properly cleans RESP3 push notifications from buffer - Remove all nil checks throughout codebase for cleaner, safer code - Update tests to expect VoidPushNotificationProcessor when disabled Benefits: - Eliminates nil pointer risks throughout the codebase - Follows null object pattern for safer operation - Properly handles RESP3 push notifications even when disabled - Consistent interface regardless of push notification settings - Cleaner code without defensive nil checks everywhere
This commit is contained in:
@ -104,6 +104,15 @@ func (r *PushNotificationRegistry) HasHandlers() bool {
|
||||
return len(r.handlers) > 0
|
||||
}
|
||||
|
||||
// PushNotificationProcessorInterface defines the interface for push notification processors.
|
||||
type PushNotificationProcessorInterface interface {
|
||||
IsEnabled() bool
|
||||
SetEnabled(enabled bool)
|
||||
GetRegistry() *PushNotificationRegistry
|
||||
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
|
||||
RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error
|
||||
}
|
||||
|
||||
// PushNotificationProcessor handles the processing of push notifications from Redis.
|
||||
type PushNotificationProcessor struct {
|
||||
registry *PushNotificationRegistry
|
||||
@ -233,3 +242,62 @@ func (info *PushNotificationInfo) String() string {
|
||||
}
|
||||
return info.Name
|
||||
}
|
||||
|
||||
// VoidPushNotificationProcessor is a no-op processor that discards all push notifications.
|
||||
// Used when push notifications are disabled to avoid nil checks throughout the codebase.
|
||||
type VoidPushNotificationProcessor struct{}
|
||||
|
||||
// NewVoidPushNotificationProcessor creates a new void push notification processor.
|
||||
func NewVoidPushNotificationProcessor() *VoidPushNotificationProcessor {
|
||||
return &VoidPushNotificationProcessor{}
|
||||
}
|
||||
|
||||
// IsEnabled always returns false for void processor.
|
||||
func (v *VoidPushNotificationProcessor) IsEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// SetEnabled is a no-op for void processor.
|
||||
func (v *VoidPushNotificationProcessor) SetEnabled(enabled bool) {
|
||||
// No-op: void processor is always disabled
|
||||
}
|
||||
|
||||
// GetRegistry returns nil for void processor since it doesn't maintain handlers.
|
||||
func (v *VoidPushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
|
||||
return nil
|
||||
}
|
||||
|
||||
// ProcessPendingNotifications reads and discards any pending push notifications.
|
||||
func (v *VoidPushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
|
||||
// Read and discard any pending push notifications to clean the buffer
|
||||
for {
|
||||
// Peek at the next reply type to see if it's a push notification
|
||||
replyType, err := rd.PeekReplyType()
|
||||
if err != nil {
|
||||
// No more data available or error peeking
|
||||
break
|
||||
}
|
||||
|
||||
// Check if this is a RESP3 push notification
|
||||
if replyType == '>' { // RespPush
|
||||
// Read and discard the push notification
|
||||
_, err := rd.ReadReply()
|
||||
if err != nil {
|
||||
internal.Logger.Printf(ctx, "push: error reading push notification to discard: %v", err)
|
||||
break
|
||||
}
|
||||
// Continue to check for more push notifications
|
||||
} else {
|
||||
// Not a push notification, stop processing
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterHandler is a no-op for void processor, always returns nil.
|
||||
func (v *VoidPushNotificationProcessor) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
|
||||
// No-op: void processor doesn't register handlers
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user