Skip to content

Commit 91805bc

Browse files
committed
refactor: remove handlerWrapper and use separate maps in registry
- Remove unnecessary handlerWrapper complexity from push notifications - Use separate maps for handlers and protection status in registry - Store handlers directly without indirection layer - Maintain same instance identity for registered/retrieved handlers - Preserve all protected handler functionality with cleaner implementation Changes: - internal/pushnotif/registry.go: Use separate handlers and protected maps - push_notifications.go: Remove handlerWrapper, store handlers directly - Maintain thread-safe operations with simplified code structure Benefits: - Reduced memory overhead (no wrapper objects) - Direct handler storage without type conversion - Cleaner, more maintainable code - Same functionality with better performance - Eliminated unnecessary complexity layer - Preserved all existing behavior and safety guarantees
1 parent ada72ce commit 91805bc

File tree

3 files changed

+18
-34
lines changed

3 files changed

+18
-34
lines changed

internal/pushnotif/registry.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@ import (
88

99
// Registry manages push notification handlers.
1010
type Registry struct {
11-
mu sync.RWMutex
12-
handlers map[string]handlerEntry
11+
mu sync.RWMutex
12+
handlers map[string]Handler
13+
protected map[string]bool
1314
}
1415

1516
// NewRegistry creates a new push notification registry.
1617
func NewRegistry() *Registry {
1718
return &Registry{
18-
handlers: make(map[string]handlerEntry),
19+
handlers: make(map[string]Handler),
20+
protected: make(map[string]bool),
1921
}
2022
}
2123

@@ -30,10 +32,8 @@ func (r *Registry) RegisterHandler(pushNotificationName string, handler Handler,
3032
return fmt.Errorf("handler already registered for push notification: %s", pushNotificationName)
3133
}
3234

33-
r.handlers[pushNotificationName] = handlerEntry{
34-
handler: handler,
35-
protected: protected,
36-
}
35+
r.handlers[pushNotificationName] = handler
36+
r.protected[pushNotificationName] = protected
3737
return nil
3838
}
3939

@@ -43,16 +43,17 @@ func (r *Registry) UnregisterHandler(pushNotificationName string) error {
4343
r.mu.Lock()
4444
defer r.mu.Unlock()
4545

46-
entry, exists := r.handlers[pushNotificationName]
46+
_, exists := r.handlers[pushNotificationName]
4747
if !exists {
4848
return fmt.Errorf("no handler registered for push notification: %s", pushNotificationName)
4949
}
5050

51-
if entry.protected {
51+
if r.protected[pushNotificationName] {
5252
return fmt.Errorf("cannot unregister protected handler for push notification: %s", pushNotificationName)
5353
}
5454

5555
delete(r.handlers, pushNotificationName)
56+
delete(r.protected, pushNotificationName)
5657
return nil
5758
}
5859

@@ -62,11 +63,11 @@ func (r *Registry) GetHandler(pushNotificationName string) Handler {
6263
r.mu.RLock()
6364
defer r.mu.RUnlock()
6465

65-
entry, exists := r.handlers[pushNotificationName]
66+
handler, exists := r.handlers[pushNotificationName]
6667
if !exists {
6768
return nil
6869
}
69-
return entry.handler
70+
return handler
7071
}
7172

7273
// GetRegisteredPushNotificationNames returns a list of all registered push notification names.

internal/pushnotif/types.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,3 @@ type RegistryInterface interface {
2828
GetRegisteredPushNotificationNames() []string
2929
HandleNotification(ctx context.Context, notification []interface{}) bool
3030
}
31-
32-
// handlerEntry represents a registered handler with its protection status.
33-
type handlerEntry struct {
34-
handler Handler
35-
protected bool
36-
}

push_notifications.go

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func NewPushNotificationRegistry() *PushNotificationRegistry {
3535

3636
// RegisterHandler registers a handler for a specific push notification name.
3737
func (r *PushNotificationRegistry) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
38-
return r.registry.RegisterHandler(pushNotificationName, &handlerWrapper{handler}, protected)
38+
return r.registry.RegisterHandler(pushNotificationName, handler, protected)
3939
}
4040

4141
// UnregisterHandler removes a handler for a specific push notification name.
@@ -49,10 +49,8 @@ func (r *PushNotificationRegistry) GetHandler(pushNotificationName string) PushN
4949
if handler == nil {
5050
return nil
5151
}
52-
if wrapper, ok := handler.(*handlerWrapper); ok {
53-
return wrapper.handler
54-
}
55-
return nil
52+
// The handler is already a PushNotificationHandler since we store it directly
53+
return handler.(PushNotificationHandler)
5654
}
5755

5856
// GetRegisteredPushNotificationNames returns a list of all registered push notification names.
@@ -83,15 +81,13 @@ func (p *PushNotificationProcessor) GetHandler(pushNotificationName string) Push
8381
if handler == nil {
8482
return nil
8583
}
86-
if wrapper, ok := handler.(*handlerWrapper); ok {
87-
return wrapper.handler
88-
}
89-
return nil
84+
// The handler is already a PushNotificationHandler since we store it directly
85+
return handler.(PushNotificationHandler)
9086
}
9187

9288
// RegisterHandler registers a handler for a specific push notification name.
9389
func (p *PushNotificationProcessor) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
94-
return p.processor.RegisterHandler(pushNotificationName, &handlerWrapper{handler}, protected)
90+
return p.processor.RegisterHandler(pushNotificationName, handler, protected)
9591
}
9692

9793
// UnregisterHandler removes a handler for a specific push notification name.
@@ -143,14 +139,7 @@ func (v *VoidPushNotificationProcessor) GetRegistryForTesting() *PushNotificatio
143139
return nil
144140
}
145141

146-
// handlerWrapper wraps the public PushNotificationHandler interface to implement the internal Handler interface.
147-
type handlerWrapper struct {
148-
handler PushNotificationHandler
149-
}
150142

151-
func (w *handlerWrapper) HandlePushNotification(ctx context.Context, notification []interface{}) bool {
152-
return w.handler.HandlePushNotification(ctx, notification)
153-
}
154143

155144
// Redis Cluster push notification names
156145
const (

0 commit comments

Comments
 (0)