|
| 1 | +package redis |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/redis/go-redis/v9/internal" |
| 8 | + "github.com/redis/go-redis/v9/internal/proto" |
| 9 | +) |
| 10 | + |
| 11 | +// Registry manages push notification handlers |
| 12 | +type Registry struct { |
| 13 | + handlers map[string]PushNotificationHandler |
| 14 | + protected map[string]bool |
| 15 | +} |
| 16 | + |
| 17 | +// NewRegistry creates a new push notification registry |
| 18 | +func NewRegistry() *Registry { |
| 19 | + return &Registry{ |
| 20 | + handlers: make(map[string]PushNotificationHandler), |
| 21 | + protected: make(map[string]bool), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// RegisterHandler registers a handler for a specific push notification name |
| 26 | +func (r *Registry) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error { |
| 27 | + if handler == nil { |
| 28 | + return fmt.Errorf("handler cannot be nil") |
| 29 | + } |
| 30 | + |
| 31 | + // Check if handler already exists and is protected |
| 32 | + if existingProtected, exists := r.protected[pushNotificationName]; exists && existingProtected { |
| 33 | + return fmt.Errorf("cannot overwrite protected handler for push notification: %s", pushNotificationName) |
| 34 | + } |
| 35 | + |
| 36 | + r.handlers[pushNotificationName] = handler |
| 37 | + r.protected[pushNotificationName] = protected |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// GetHandler returns the handler for a specific push notification name |
| 42 | +func (r *Registry) GetHandler(pushNotificationName string) PushNotificationHandler { |
| 43 | + return r.handlers[pushNotificationName] |
| 44 | +} |
| 45 | + |
| 46 | +// UnregisterHandler removes a handler for a specific push notification name |
| 47 | +func (r *Registry) UnregisterHandler(pushNotificationName string) error { |
| 48 | + // Check if handler is protected |
| 49 | + if protected, exists := r.protected[pushNotificationName]; exists && protected { |
| 50 | + return fmt.Errorf("cannot unregister protected handler for push notification: %s", pushNotificationName) |
| 51 | + } |
| 52 | + |
| 53 | + delete(r.handlers, pushNotificationName) |
| 54 | + delete(r.protected, pushNotificationName) |
| 55 | + return nil |
| 56 | +} |
| 57 | + |
| 58 | +// GetRegisteredPushNotificationNames returns all registered push notification names |
| 59 | +func (r *Registry) GetRegisteredPushNotificationNames() []string { |
| 60 | + names := make([]string, 0, len(r.handlers)) |
| 61 | + for name := range r.handlers { |
| 62 | + names = append(names, name) |
| 63 | + } |
| 64 | + return names |
| 65 | +} |
| 66 | + |
| 67 | +// Processor handles push notifications with a registry of handlers |
| 68 | +type Processor struct { |
| 69 | + registry *Registry |
| 70 | +} |
| 71 | + |
| 72 | +// NewProcessor creates a new push notification processor |
| 73 | +func NewProcessor() *Processor { |
| 74 | + return &Processor{ |
| 75 | + registry: NewRegistry(), |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +// GetHandler returns the handler for a specific push notification name |
| 80 | +func (p *Processor) GetHandler(pushNotificationName string) PushNotificationHandler { |
| 81 | + return p.registry.GetHandler(pushNotificationName) |
| 82 | +} |
| 83 | + |
| 84 | +// RegisterHandler registers a handler for a specific push notification name |
| 85 | +func (p *Processor) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error { |
| 86 | + return p.registry.RegisterHandler(pushNotificationName, handler, protected) |
| 87 | +} |
| 88 | + |
| 89 | +// UnregisterHandler removes a handler for a specific push notification name |
| 90 | +func (p *Processor) UnregisterHandler(pushNotificationName string) error { |
| 91 | + return p.registry.UnregisterHandler(pushNotificationName) |
| 92 | +} |
| 93 | + |
| 94 | +// ProcessPendingNotifications checks for and processes any pending push notifications |
| 95 | +func (p *Processor) ProcessPendingNotifications(ctx context.Context, handlerCtx PushNotificationHandlerContext, rd *proto.Reader) error { |
| 96 | + if rd == nil { |
| 97 | + return nil |
| 98 | + } |
| 99 | + |
| 100 | + for { |
| 101 | + // Check if there's data available to read |
| 102 | + replyType, err := rd.PeekReplyType() |
| 103 | + if err != nil { |
| 104 | + // No more data available or error reading |
| 105 | + break |
| 106 | + } |
| 107 | + |
| 108 | + // Only process push notifications (arrays starting with >) |
| 109 | + if replyType != proto.RespPush { |
| 110 | + break |
| 111 | + } |
| 112 | + |
| 113 | + // Read the push notification |
| 114 | + reply, err := rd.ReadReply() |
| 115 | + if err != nil { |
| 116 | + internal.Logger.Printf(ctx, "push: error reading push notification: %v", err) |
| 117 | + break |
| 118 | + } |
| 119 | + |
| 120 | + // Convert to slice of interfaces |
| 121 | + notification, ok := reply.([]interface{}) |
| 122 | + if !ok { |
| 123 | + continue |
| 124 | + } |
| 125 | + |
| 126 | + // Handle the notification directly |
| 127 | + if len(notification) > 0 { |
| 128 | + // Extract the notification type (first element) |
| 129 | + if notificationType, ok := notification[0].(string); ok { |
| 130 | + // Skip notifications that should be handled by other systems |
| 131 | + if shouldSkipNotification(notificationType) { |
| 132 | + continue |
| 133 | + } |
| 134 | + |
| 135 | + // Get the handler for this notification type |
| 136 | + if handler := p.registry.GetHandler(notificationType); handler != nil { |
| 137 | + // Handle the notification |
| 138 | + handler.HandlePushNotification(ctx, handlerCtx, notification) |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// shouldSkipNotification checks if a notification type should be ignored by the push notification |
| 148 | +// processor and handled by other specialized systems instead (pub/sub, streams, keyspace, etc.). |
| 149 | +func shouldSkipNotification(notificationType string) bool { |
| 150 | + switch notificationType { |
| 151 | + // Pub/Sub notifications - handled by pub/sub system |
| 152 | + case "message", // Regular pub/sub message |
| 153 | + "pmessage", // Pattern pub/sub message |
| 154 | + "subscribe", // Subscription confirmation |
| 155 | + "unsubscribe", // Unsubscription confirmation |
| 156 | + "psubscribe", // Pattern subscription confirmation |
| 157 | + "punsubscribe", // Pattern unsubscription confirmation |
| 158 | + "smessage", // Sharded pub/sub message (Redis 7.0+) |
| 159 | + "ssubscribe", // Sharded subscription confirmation |
| 160 | + "sunsubscribe": // Sharded unsubscription confirmation |
| 161 | + return true |
| 162 | + default: |
| 163 | + return false |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// VoidProcessor discards all push notifications without processing them |
| 168 | +type VoidProcessor struct{} |
| 169 | + |
| 170 | +// NewVoidProcessor creates a new void push notification processor |
| 171 | +func NewVoidProcessor() *VoidProcessor { |
| 172 | + return &VoidProcessor{} |
| 173 | +} |
| 174 | + |
| 175 | +// GetHandler returns nil for void processor since it doesn't maintain handlers |
| 176 | +func (v *VoidProcessor) GetHandler(pushNotificationName string) PushNotificationHandler { |
| 177 | + return nil |
| 178 | +} |
| 179 | + |
| 180 | +// RegisterHandler returns an error for void processor since it doesn't maintain handlers |
| 181 | +func (v *VoidProcessor) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error { |
| 182 | + return fmt.Errorf("cannot register push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName) |
| 183 | +} |
| 184 | + |
| 185 | +// UnregisterHandler returns an error for void processor since it doesn't maintain handlers |
| 186 | +func (v *VoidProcessor) UnregisterHandler(pushNotificationName string) error { |
| 187 | + return fmt.Errorf("cannot unregister push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName) |
| 188 | +} |
| 189 | + |
| 190 | +// ProcessPendingNotifications for VoidProcessor does nothing since push notifications |
| 191 | +// are only available in RESP3 and this processor is used for RESP2 connections. |
| 192 | +// This avoids unnecessary buffer scanning overhead. |
| 193 | +func (v *VoidProcessor) ProcessPendingNotifications(ctx context.Context, handlerCtx PushNotificationHandlerContext, rd *proto.Reader) error { |
| 194 | + // VoidProcessor is used for RESP2 connections where push notifications are not available. |
| 195 | + // Since push notifications only exist in RESP3, we can safely skip all processing |
| 196 | + // to avoid unnecessary buffer scanning overhead. |
| 197 | + return nil |
| 198 | +} |
0 commit comments