|
| 1 | +package pushnotif |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/redis/go-redis/v9/internal/proto" |
| 8 | +) |
| 9 | + |
| 10 | +// Processor handles push notifications with a registry of handlers. |
| 11 | +type Processor struct { |
| 12 | + registry *Registry |
| 13 | +} |
| 14 | + |
| 15 | +// NewProcessor creates a new push notification processor. |
| 16 | +func NewProcessor() *Processor { |
| 17 | + return &Processor{ |
| 18 | + registry: NewRegistry(), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// GetHandler returns the handler for a specific push notification name. |
| 23 | +// Returns nil if no handler is registered for the given name. |
| 24 | +func (p *Processor) GetHandler(pushNotificationName string) Handler { |
| 25 | + return p.registry.GetHandler(pushNotificationName) |
| 26 | +} |
| 27 | + |
| 28 | +// RegisterHandler registers a handler for a specific push notification name. |
| 29 | +// Returns an error if a handler is already registered for this push notification name. |
| 30 | +// If protected is true, the handler cannot be unregistered. |
| 31 | +func (p *Processor) RegisterHandler(pushNotificationName string, handler Handler, protected bool) error { |
| 32 | + return p.registry.RegisterHandler(pushNotificationName, handler, protected) |
| 33 | +} |
| 34 | + |
| 35 | +// UnregisterHandler removes a handler for a specific push notification name. |
| 36 | +// Returns an error if the handler is protected or doesn't exist. |
| 37 | +func (p *Processor) UnregisterHandler(pushNotificationName string) error { |
| 38 | + return p.registry.UnregisterHandler(pushNotificationName) |
| 39 | +} |
| 40 | + |
| 41 | +// GetRegistryForTesting returns the push notification registry for testing. |
| 42 | +// This method should only be used by tests. |
| 43 | +func (p *Processor) GetRegistryForTesting() *Registry { |
| 44 | + return p.registry |
| 45 | +} |
| 46 | + |
| 47 | +// ProcessPendingNotifications checks for and processes any pending push notifications. |
| 48 | +func (p *Processor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error { |
| 49 | + // Check for nil reader |
| 50 | + if rd == nil { |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + // Check if there are any buffered bytes that might contain push notifications |
| 55 | + if rd.Buffered() == 0 { |
| 56 | + return nil |
| 57 | + } |
| 58 | + |
| 59 | + // Process all available push notifications |
| 60 | + for { |
| 61 | + // Peek at the next reply type to see if it's a push notification |
| 62 | + replyType, err := rd.PeekReplyType() |
| 63 | + if err != nil { |
| 64 | + // No more data available or error reading |
| 65 | + break |
| 66 | + } |
| 67 | + |
| 68 | + // Push notifications use RespPush type in RESP3 |
| 69 | + if replyType != proto.RespPush { |
| 70 | + break |
| 71 | + } |
| 72 | + |
| 73 | + // Try to read the push notification |
| 74 | + reply, err := rd.ReadReply() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("failed to read push notification: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + // Convert to slice of interfaces |
| 80 | + notification, ok := reply.([]interface{}) |
| 81 | + if !ok { |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + // Handle the notification |
| 86 | + p.registry.HandleNotification(ctx, notification) |
| 87 | + } |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +// VoidProcessor discards all push notifications without processing them. |
| 93 | +type VoidProcessor struct{} |
| 94 | + |
| 95 | +// NewVoidProcessor creates a new void push notification processor. |
| 96 | +func NewVoidProcessor() *VoidProcessor { |
| 97 | + return &VoidProcessor{} |
| 98 | +} |
| 99 | + |
| 100 | +// GetHandler returns nil for void processor since it doesn't maintain handlers. |
| 101 | +func (v *VoidProcessor) GetHandler(pushNotificationName string) Handler { |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +// RegisterHandler returns an error for void processor since it doesn't maintain handlers. |
| 106 | +func (v *VoidProcessor) RegisterHandler(pushNotificationName string, handler Handler, protected bool) error { |
| 107 | + return fmt.Errorf("void push notification processor does not support handler registration") |
| 108 | +} |
| 109 | + |
| 110 | +// GetRegistryForTesting returns nil for void processor since it doesn't maintain handlers. |
| 111 | +// This method should only be used by tests. |
| 112 | +func (v *VoidProcessor) GetRegistryForTesting() *Registry { |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +// ProcessPendingNotifications reads and discards any pending push notifications. |
| 117 | +func (v *VoidProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error { |
| 118 | + // Check for nil reader |
| 119 | + if rd == nil { |
| 120 | + return nil |
| 121 | + } |
| 122 | + |
| 123 | + // Read and discard any pending push notifications to clean the buffer |
| 124 | + for { |
| 125 | + // Peek at the next reply type to see if it's a push notification |
| 126 | + replyType, err := rd.PeekReplyType() |
| 127 | + if err != nil { |
| 128 | + // No more data available or error reading |
| 129 | + break |
| 130 | + } |
| 131 | + |
| 132 | + // Push notifications use RespPush type in RESP3 |
| 133 | + if replyType != proto.RespPush { |
| 134 | + break |
| 135 | + } |
| 136 | + |
| 137 | + // Read and discard the push notification |
| 138 | + _, err = rd.ReadReply() |
| 139 | + if err != nil { |
| 140 | + return fmt.Errorf("failed to read push notification for discarding: %w", err) |
| 141 | + } |
| 142 | + |
| 143 | + // Notification discarded - continue to next one |
| 144 | + } |
| 145 | + |
| 146 | + return nil |
| 147 | +} |
0 commit comments