Skip to content

Commit d3f6197

Browse files
committed
feat: add GetHandler method and improve push notification API encapsulation
- Add GetHandler() method to PushNotificationProcessorInterface for better encapsulation - Add GetPushNotificationHandler() convenience method to Client and SentinelClient - Remove HasHandlers() check from ProcessPendingNotifications to ensure notifications are always consumed - Use PushNotificationProcessorInterface in internal pool package for proper abstraction - Maintain GetRegistry() for backward compatibility and testing - Update pubsub to use GetHandler() instead of GetRegistry() for cleaner code Benefits: - Better API encapsulation - no need to expose entire registry - Cleaner interface - direct access to specific handlers - Always consume push notifications from reader regardless of handler presence - Proper abstraction in internal pool package - Backward compatibility maintained - Consistent behavior across all processor types
1 parent ad16b21 commit d3f6197

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

push_notifications.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,17 +96,23 @@ func (r *PushNotificationRegistry) GetRegisteredPushNotificationNames() []string
9696
return names
9797
}
9898

99-
// HasHandlers returns true if there are any handlers registered.
100-
func (r *PushNotificationRegistry) HasHandlers() bool {
99+
// GetHandler returns the handler for a specific push notification name.
100+
// Returns nil if no handler is registered for the given name.
101+
func (r *PushNotificationRegistry) GetHandler(pushNotificationName string) PushNotificationHandler {
101102
r.mu.RLock()
102103
defer r.mu.RUnlock()
103104

104-
return len(r.handlers) > 0
105+
handler, exists := r.handlers[pushNotificationName]
106+
if !exists {
107+
return nil
108+
}
109+
return handler
105110
}
106111

107112
// PushNotificationProcessorInterface defines the interface for push notification processors.
108113
type PushNotificationProcessorInterface interface {
109-
GetRegistry() *PushNotificationRegistry
114+
GetHandler(pushNotificationName string) PushNotificationHandler
115+
GetRegistry() *PushNotificationRegistry // For backward compatibility and testing
110116
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
111117
RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error
112118
}
@@ -123,16 +129,20 @@ func NewPushNotificationProcessor() *PushNotificationProcessor {
123129
}
124130
}
125131

126-
// GetRegistry returns the push notification registry.
132+
// GetHandler returns the handler for a specific push notification name.
133+
// Returns nil if no handler is registered for the given name.
134+
func (p *PushNotificationProcessor) GetHandler(pushNotificationName string) PushNotificationHandler {
135+
return p.registry.GetHandler(pushNotificationName)
136+
}
137+
138+
// GetRegistry returns the push notification registry for internal use.
139+
// This method is primarily for testing and internal operations.
127140
func (p *PushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
128141
return p.registry
129142
}
130143

131144
// ProcessPendingNotifications checks for and processes any pending push notifications.
132145
func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
133-
if !p.registry.HasHandlers() {
134-
return nil
135-
}
136146

137147
// Check if there are any buffered bytes that might contain push notifications
138148
if rd.Buffered() == 0 {
@@ -233,6 +243,11 @@ func NewVoidPushNotificationProcessor() *VoidPushNotificationProcessor {
233243
return &VoidPushNotificationProcessor{}
234244
}
235245

246+
// GetHandler returns nil for void processor since it doesn't maintain handlers.
247+
func (v *VoidPushNotificationProcessor) GetHandler(pushNotificationName string) PushNotificationHandler {
248+
return nil
249+
}
250+
236251
// GetRegistry returns nil for void processor since it doesn't maintain handlers.
237252
func (v *VoidPushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
238253
return nil

push_notifications_test.go

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,7 @@ func TestPushNotificationRegistry(t *testing.T) {
2828
registry := redis.NewPushNotificationRegistry()
2929

3030
// Test initial state
31-
if registry.HasHandlers() {
32-
t.Error("Registry should not have handlers initially")
33-
}
31+
// Registry starts empty (no need to check HasHandlers anymore)
3432

3533
commands := registry.GetRegisteredPushNotificationNames()
3634
if len(commands) != 0 {
@@ -49,10 +47,7 @@ func TestPushNotificationRegistry(t *testing.T) {
4947
t.Fatalf("Failed to register handler: %v", err)
5048
}
5149

52-
if !registry.HasHandlers() {
53-
t.Error("Registry should have handlers after registration")
54-
}
55-
50+
// Verify handler was registered by checking registered names
5651
commands = registry.GetRegisteredPushNotificationNames()
5752
if len(commands) != 1 || commands[0] != "TEST_COMMAND" {
5853
t.Errorf("Expected ['TEST_COMMAND'], got %v", commands)
@@ -803,7 +798,6 @@ func TestPushNotificationRegistryConcurrency(t *testing.T) {
803798
registry.HandleNotification(context.Background(), notification)
804799

805800
// Check registry state
806-
registry.HasHandlers()
807801
registry.GetRegisteredPushNotificationNames()
808802
}
809803
}(i)
@@ -815,10 +809,6 @@ func TestPushNotificationRegistryConcurrency(t *testing.T) {
815809
}
816810

817811
// Verify registry is still functional
818-
if !registry.HasHandlers() {
819-
t.Error("Registry should have handlers after concurrent operations")
820-
}
821-
822812
commands := registry.GetRegisteredPushNotificationNames()
823813
if len(commands) == 0 {
824814
t.Error("Registry should have registered commands after concurrent operations")

redis.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,12 @@ func (c *Client) GetPushNotificationProcessor() PushNotificationProcessorInterfa
837837
return c.pushProcessor
838838
}
839839

840+
// GetPushNotificationHandler returns the handler for a specific push notification name.
841+
// Returns nil if no handler is registered for the given name.
842+
func (c *Client) GetPushNotificationHandler(pushNotificationName string) PushNotificationHandler {
843+
return c.pushProcessor.GetHandler(pushNotificationName)
844+
}
845+
840846
type PoolStats pool.Stats
841847

842848
// PoolStats returns connection pool stats.

sentinel.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,12 @@ func (c *SentinelClient) GetPushNotificationProcessor() PushNotificationProcesso
522522
return c.pushProcessor
523523
}
524524

525+
// GetPushNotificationHandler returns the handler for a specific push notification name.
526+
// Returns nil if no handler is registered for the given name.
527+
func (c *SentinelClient) GetPushNotificationHandler(pushNotificationName string) PushNotificationHandler {
528+
return c.pushProcessor.GetHandler(pushNotificationName)
529+
}
530+
525531
// RegisterPushNotificationHandler registers a handler for a specific push notification name.
526532
// Returns an error if a handler is already registered for this push notification name.
527533
// If protected is true, the handler cannot be unregistered.

0 commit comments

Comments
 (0)