mirror of
https://github.com/redis/go-redis.git
synced 2025-07-29 17:41:15 +03:00
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
This commit is contained in:
@ -8,14 +8,16 @@ import (
|
|||||||
|
|
||||||
// Registry manages push notification handlers.
|
// Registry manages push notification handlers.
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
handlers map[string]handlerEntry
|
handlers map[string]Handler
|
||||||
|
protected map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRegistry creates a new push notification registry.
|
// NewRegistry creates a new push notification registry.
|
||||||
func NewRegistry() *Registry {
|
func NewRegistry() *Registry {
|
||||||
return &Registry{
|
return &Registry{
|
||||||
handlers: make(map[string]handlerEntry),
|
handlers: make(map[string]Handler),
|
||||||
|
protected: make(map[string]bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,10 +32,8 @@ func (r *Registry) RegisterHandler(pushNotificationName string, handler Handler,
|
|||||||
return fmt.Errorf("handler already registered for push notification: %s", pushNotificationName)
|
return fmt.Errorf("handler already registered for push notification: %s", pushNotificationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.handlers[pushNotificationName] = handlerEntry{
|
r.handlers[pushNotificationName] = handler
|
||||||
handler: handler,
|
r.protected[pushNotificationName] = protected
|
||||||
protected: protected,
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,16 +43,17 @@ func (r *Registry) UnregisterHandler(pushNotificationName string) error {
|
|||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
entry, exists := r.handlers[pushNotificationName]
|
_, exists := r.handlers[pushNotificationName]
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("no handler registered for push notification: %s", pushNotificationName)
|
return fmt.Errorf("no handler registered for push notification: %s", pushNotificationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
if entry.protected {
|
if r.protected[pushNotificationName] {
|
||||||
return fmt.Errorf("cannot unregister protected handler for push notification: %s", pushNotificationName)
|
return fmt.Errorf("cannot unregister protected handler for push notification: %s", pushNotificationName)
|
||||||
}
|
}
|
||||||
|
|
||||||
delete(r.handlers, pushNotificationName)
|
delete(r.handlers, pushNotificationName)
|
||||||
|
delete(r.protected, pushNotificationName)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,11 +63,11 @@ func (r *Registry) GetHandler(pushNotificationName string) Handler {
|
|||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
|
|
||||||
entry, exists := r.handlers[pushNotificationName]
|
handler, exists := r.handlers[pushNotificationName]
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return entry.handler
|
return handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRegisteredPushNotificationNames returns a list of all registered push notification names.
|
// GetRegisteredPushNotificationNames returns a list of all registered push notification names.
|
||||||
|
@ -28,9 +28,3 @@ type RegistryInterface interface {
|
|||||||
GetRegisteredPushNotificationNames() []string
|
GetRegisteredPushNotificationNames() []string
|
||||||
HandleNotification(ctx context.Context, notification []interface{}) bool
|
HandleNotification(ctx context.Context, notification []interface{}) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlerEntry represents a registered handler with its protection status.
|
|
||||||
type handlerEntry struct {
|
|
||||||
handler Handler
|
|
||||||
protected bool
|
|
||||||
}
|
|
||||||
|
@ -35,7 +35,7 @@ func NewPushNotificationRegistry() *PushNotificationRegistry {
|
|||||||
|
|
||||||
// RegisterHandler registers a handler for a specific push notification name.
|
// RegisterHandler registers a handler for a specific push notification name.
|
||||||
func (r *PushNotificationRegistry) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
|
func (r *PushNotificationRegistry) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
|
||||||
return r.registry.RegisterHandler(pushNotificationName, &handlerWrapper{handler}, protected)
|
return r.registry.RegisterHandler(pushNotificationName, handler, protected)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnregisterHandler removes a handler for a specific push notification name.
|
// UnregisterHandler removes a handler for a specific push notification name.
|
||||||
@ -49,10 +49,8 @@ func (r *PushNotificationRegistry) GetHandler(pushNotificationName string) PushN
|
|||||||
if handler == nil {
|
if handler == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if wrapper, ok := handler.(*handlerWrapper); ok {
|
// The handler is already a PushNotificationHandler since we store it directly
|
||||||
return wrapper.handler
|
return handler.(PushNotificationHandler)
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRegisteredPushNotificationNames returns a list of all registered push notification names.
|
// GetRegisteredPushNotificationNames returns a list of all registered push notification names.
|
||||||
@ -83,15 +81,13 @@ func (p *PushNotificationProcessor) GetHandler(pushNotificationName string) Push
|
|||||||
if handler == nil {
|
if handler == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if wrapper, ok := handler.(*handlerWrapper); ok {
|
// The handler is already a PushNotificationHandler since we store it directly
|
||||||
return wrapper.handler
|
return handler.(PushNotificationHandler)
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterHandler registers a handler for a specific push notification name.
|
// RegisterHandler registers a handler for a specific push notification name.
|
||||||
func (p *PushNotificationProcessor) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
|
func (p *PushNotificationProcessor) RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error {
|
||||||
return p.processor.RegisterHandler(pushNotificationName, &handlerWrapper{handler}, protected)
|
return p.processor.RegisterHandler(pushNotificationName, handler, protected)
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnregisterHandler removes a handler for a specific push notification name.
|
// UnregisterHandler removes a handler for a specific push notification name.
|
||||||
@ -143,14 +139,7 @@ func (v *VoidPushNotificationProcessor) GetRegistryForTesting() *PushNotificatio
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// handlerWrapper wraps the public PushNotificationHandler interface to implement the internal Handler interface.
|
|
||||||
type handlerWrapper struct {
|
|
||||||
handler PushNotificationHandler
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *handlerWrapper) HandlePushNotification(ctx context.Context, notification []interface{}) bool {
|
|
||||||
return w.handler.HandlePushNotification(ctx, notification)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Redis Cluster push notification names
|
// Redis Cluster push notification names
|
||||||
const (
|
const (
|
||||||
|
Reference in New Issue
Block a user