1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-18 00:20:57 +03:00

feat: add pub/sub message filtering to push notification processor

- Add isPubSubMessage() function to identify pub/sub message types
- Filter out pub/sub messages in ProcessPendingNotifications
- Allow pub/sub system to handle its own messages without interference
- Process only cluster/system push notifications (MOVING, MIGRATING, etc.)
- Add comprehensive test coverage for filtering logic

Pub/sub message types filtered:
- message (regular pub/sub)
- pmessage (pattern pub/sub)
- subscribe/unsubscribe (subscription management)
- psubscribe/punsubscribe (pattern subscription management)
- smessage (sharded pub/sub, Redis 7.0+)

Benefits:
- Clear separation of concerns between pub/sub and push notifications
- Prevents interference between the two messaging systems
- Ensures pub/sub messages reach their intended handlers
- Eliminates message loss due to incorrect interception
- Improved system reliability and performance
- Better resource utilization and message flow

Implementation:
- Efficient O(1) switch statement for message type lookup
- Case-sensitive matching for precise filtering
- Early return to skip unnecessary processing
- Maintains processing of other notifications in same batch
- Applied to all processing points (WithReader, Pool.Put, isHealthyConn)

Test coverage:
- TestIsPubSubMessage - Function correctness and edge cases
- TestPubSubFiltering - End-to-end integration testing
- Mixed message scenarios and handler verification
This commit is contained in:
Nedyalko Dyakov
2025-06-27 23:20:25 +03:00
parent b6e712b41a
commit f66518cf3a
4 changed files with 199 additions and 24 deletions

View File

@ -38,8 +38,6 @@ func (p *Processor) UnregisterHandler(pushNotificationName string) error {
return p.registry.UnregisterHandler(pushNotificationName)
}
// ProcessPendingNotifications checks for and processes any pending push notifications.
func (p *Processor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
// Check for nil reader
@ -66,6 +64,17 @@ func (p *Processor) ProcessPendingNotifications(ctx context.Context, rd *proto.R
break
}
notificationName, err := rd.PeekPushNotificationName()
if err != nil {
// Error reading - continue to next iteration
break
}
// Skip pub/sub messages - they should be handled by the pub/sub system
if isPubSubMessage(notificationName) {
break
}
// Try to read the push notification
reply, err := rd.ReadReply()
if err != nil {
@ -94,6 +103,23 @@ func (p *Processor) ProcessPendingNotifications(ctx context.Context, rd *proto.R
return nil
}
// isPubSubMessage checks if a notification type is a pub/sub message that should be ignored
// by the push notification processor and handled by the pub/sub system instead.
func isPubSubMessage(notificationType string) bool {
switch notificationType {
case "message", // Regular pub/sub message
"pmessage", // Pattern pub/sub message
"subscribe", // Subscription confirmation
"unsubscribe", // Unsubscription confirmation
"psubscribe", // Pattern subscription confirmation
"punsubscribe", // Pattern unsubscription confirmation
"smessage": // Sharded pub/sub message (Redis 7.0+)
return true
default:
return false
}
}
// VoidProcessor discards all push notifications without processing them.
type VoidProcessor struct{}
@ -119,8 +145,6 @@ func (v *VoidProcessor) UnregisterHandler(pushNotificationName string) error {
return fmt.Errorf("cannot unregister push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName)
}
// ProcessPendingNotifications for VoidProcessor does nothing since push notifications
// are only available in RESP3 and this processor is used when they're disabled.
// This avoids unnecessary buffer scanning overhead.