@@ -18,36 +18,47 @@ type PushNotificationHandler interface {
1818
1919// PushNotificationRegistry manages handlers for different types of push notifications.
2020type PushNotificationRegistry struct {
21- mu sync.RWMutex
22- handlers map [string ]PushNotificationHandler // command -> single handler
21+ mu sync.RWMutex
22+ handlers map [string ]PushNotificationHandler // pushNotificationName -> single handler
23+ protected map [string ]bool // pushNotificationName -> protected flag
2324}
2425
2526// NewPushNotificationRegistry creates a new push notification registry.
2627func NewPushNotificationRegistry () * PushNotificationRegistry {
2728 return & PushNotificationRegistry {
28- handlers : make (map [string ]PushNotificationHandler ),
29+ handlers : make (map [string ]PushNotificationHandler ),
30+ protected : make (map [string ]bool ),
2931 }
3032}
3133
32- // RegisterHandler registers a handler for a specific push notification command.
33- // Returns an error if a handler is already registered for this command.
34- func (r * PushNotificationRegistry ) RegisterHandler (command string , handler PushNotificationHandler ) error {
34+ // RegisterHandler registers a handler for a specific push notification name.
35+ // Returns an error if a handler is already registered for this push notification name.
36+ // If protected is true, the handler cannot be unregistered.
37+ func (r * PushNotificationRegistry ) RegisterHandler (pushNotificationName string , handler PushNotificationHandler , protected bool ) error {
3538 r .mu .Lock ()
3639 defer r .mu .Unlock ()
3740
38- if _ , exists := r .handlers [command ]; exists {
39- return fmt .Errorf ("handler already registered for command : %s" , command )
41+ if _ , exists := r .handlers [pushNotificationName ]; exists {
42+ return fmt .Errorf ("handler already registered for push notification : %s" , pushNotificationName )
4043 }
41- r .handlers [command ] = handler
44+ r .handlers [pushNotificationName ] = handler
45+ r .protected [pushNotificationName ] = protected
4246 return nil
4347}
4448
45- // UnregisterHandler removes the handler for a specific push notification command.
46- func (r * PushNotificationRegistry ) UnregisterHandler (command string ) {
49+ // UnregisterHandler removes the handler for a specific push notification name.
50+ // Returns an error if the handler is protected.
51+ func (r * PushNotificationRegistry ) UnregisterHandler (pushNotificationName string ) error {
4752 r .mu .Lock ()
4853 defer r .mu .Unlock ()
4954
50- delete (r .handlers , command )
55+ if r .protected [pushNotificationName ] {
56+ return fmt .Errorf ("cannot unregister protected handler for push notification: %s" , pushNotificationName )
57+ }
58+
59+ delete (r .handlers , pushNotificationName )
60+ delete (r .protected , pushNotificationName )
61+ return nil
5162}
5263
5364// HandleNotification processes a push notification by calling the registered handler.
@@ -56,8 +67,8 @@ func (r *PushNotificationRegistry) HandleNotification(ctx context.Context, notif
5667 return false
5768 }
5869
59- // Extract command from notification
60- command , ok := notification [0 ].(string )
70+ // Extract push notification name from notification
71+ pushNotificationName , ok := notification [0 ].(string )
6172 if ! ok {
6273 return false
6374 }
@@ -66,23 +77,23 @@ func (r *PushNotificationRegistry) HandleNotification(ctx context.Context, notif
6677 defer r .mu .RUnlock ()
6778
6879 // Call specific handler
69- if handler , exists := r .handlers [command ]; exists {
80+ if handler , exists := r .handlers [pushNotificationName ]; exists {
7081 return handler .HandlePushNotification (ctx , notification )
7182 }
7283
7384 return false
7485}
7586
76- // GetRegisteredCommands returns a list of commands that have registered handlers.
77- func (r * PushNotificationRegistry ) GetRegisteredCommands () []string {
87+ // GetRegisteredPushNotificationNames returns a list of push notification names that have registered handlers.
88+ func (r * PushNotificationRegistry ) GetRegisteredPushNotificationNames () []string {
7889 r .mu .RLock ()
7990 defer r .mu .RUnlock ()
8091
81- commands := make ([]string , 0 , len (r .handlers ))
82- for command := range r .handlers {
83- commands = append (commands , command )
92+ names := make ([]string , 0 , len (r .handlers ))
93+ for name := range r .handlers {
94+ names = append (names , name )
8495 }
85- return commands
96+ return names
8697}
8798
8899// HasHandlers returns true if there are any handlers registered.
@@ -176,13 +187,14 @@ func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Cont
176187 return nil
177188}
178189
179- // RegisterHandler is a convenience method to register a handler for a specific command.
180- // Returns an error if a handler is already registered for this command.
181- func (p * PushNotificationProcessor ) RegisterHandler (command string , handler PushNotificationHandler ) error {
182- return p .registry .RegisterHandler (command , handler )
190+ // RegisterHandler is a convenience method to register a handler for a specific push notification name.
191+ // Returns an error if a handler is already registered for this push notification name.
192+ // If protected is true, the handler cannot be unregistered.
193+ func (p * PushNotificationProcessor ) RegisterHandler (pushNotificationName string , handler PushNotificationHandler , protected bool ) error {
194+ return p .registry .RegisterHandler (pushNotificationName , handler , protected )
183195}
184196
185- // Redis Cluster push notification commands
197+ // Redis Cluster push notification names
186198const (
187199 PushNotificationMoving = "MOVING"
188200 PushNotificationMigrating = "MIGRATING"
@@ -193,8 +205,8 @@ const (
193205
194206// PushNotificationInfo contains metadata about a push notification.
195207type PushNotificationInfo struct {
196- Command string
197- Args []interface {}
208+ Name string
209+ Args []interface {}
198210}
199211
200212// ParsePushNotificationInfo extracts information from a push notification.
@@ -203,14 +215,14 @@ func ParsePushNotificationInfo(notification []interface{}) *PushNotificationInfo
203215 return nil
204216 }
205217
206- command , ok := notification [0 ].(string )
218+ name , ok := notification [0 ].(string )
207219 if ! ok {
208220 return nil
209221 }
210222
211223 return & PushNotificationInfo {
212- Command : command ,
213- Args : notification [1 :],
224+ Name : name ,
225+ Args : notification [1 :],
214226 }
215227}
216228
@@ -219,5 +231,5 @@ func (info *PushNotificationInfo) String() string {
219231 if info == nil {
220232 return "<nil>"
221233 }
222- return info .Command
234+ return info .Name
223235}
0 commit comments