@@ -18,36 +18,47 @@ type PushNotificationHandler interface {
18
18
19
19
// PushNotificationRegistry manages handlers for different types of push notifications.
20
20
type 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
23
24
}
24
25
25
26
// NewPushNotificationRegistry creates a new push notification registry.
26
27
func NewPushNotificationRegistry () * PushNotificationRegistry {
27
28
return & PushNotificationRegistry {
28
- handlers : make (map [string ]PushNotificationHandler ),
29
+ handlers : make (map [string ]PushNotificationHandler ),
30
+ protected : make (map [string ]bool ),
29
31
}
30
32
}
31
33
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 {
35
38
r .mu .Lock ()
36
39
defer r .mu .Unlock ()
37
40
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 )
40
43
}
41
- r .handlers [command ] = handler
44
+ r .handlers [pushNotificationName ] = handler
45
+ r .protected [pushNotificationName ] = protected
42
46
return nil
43
47
}
44
48
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 {
47
52
r .mu .Lock ()
48
53
defer r .mu .Unlock ()
49
54
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
51
62
}
52
63
53
64
// HandleNotification processes a push notification by calling the registered handler.
@@ -56,8 +67,8 @@ func (r *PushNotificationRegistry) HandleNotification(ctx context.Context, notif
56
67
return false
57
68
}
58
69
59
- // Extract command from notification
60
- command , ok := notification [0 ].(string )
70
+ // Extract push notification name from notification
71
+ pushNotificationName , ok := notification [0 ].(string )
61
72
if ! ok {
62
73
return false
63
74
}
@@ -66,23 +77,23 @@ func (r *PushNotificationRegistry) HandleNotification(ctx context.Context, notif
66
77
defer r .mu .RUnlock ()
67
78
68
79
// Call specific handler
69
- if handler , exists := r .handlers [command ]; exists {
80
+ if handler , exists := r .handlers [pushNotificationName ]; exists {
70
81
return handler .HandlePushNotification (ctx , notification )
71
82
}
72
83
73
84
return false
74
85
}
75
86
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 {
78
89
r .mu .RLock ()
79
90
defer r .mu .RUnlock ()
80
91
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 )
84
95
}
85
- return commands
96
+ return names
86
97
}
87
98
88
99
// HasHandlers returns true if there are any handlers registered.
@@ -176,13 +187,14 @@ func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Cont
176
187
return nil
177
188
}
178
189
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 )
183
195
}
184
196
185
- // Redis Cluster push notification commands
197
+ // Redis Cluster push notification names
186
198
const (
187
199
PushNotificationMoving = "MOVING"
188
200
PushNotificationMigrating = "MIGRATING"
@@ -193,8 +205,8 @@ const (
193
205
194
206
// PushNotificationInfo contains metadata about a push notification.
195
207
type PushNotificationInfo struct {
196
- Command string
197
- Args []interface {}
208
+ Name string
209
+ Args []interface {}
198
210
}
199
211
200
212
// ParsePushNotificationInfo extracts information from a push notification.
@@ -203,14 +215,14 @@ func ParsePushNotificationInfo(notification []interface{}) *PushNotificationInfo
203
215
return nil
204
216
}
205
217
206
- command , ok := notification [0 ].(string )
218
+ name , ok := notification [0 ].(string )
207
219
if ! ok {
208
220
return nil
209
221
}
210
222
211
223
return & PushNotificationInfo {
212
- Command : command ,
213
- Args : notification [1 :],
224
+ Name : name ,
225
+ Args : notification [1 :],
214
226
}
215
227
}
216
228
@@ -219,5 +231,5 @@ func (info *PushNotificationInfo) String() string {
219
231
if info == nil {
220
232
return "<nil>"
221
233
}
222
- return info .Command
234
+ return info .Name
223
235
}
0 commit comments