Skip to content

Commit be9b6dd

Browse files
committed
refactor: remove unnecessary enabled field and IsEnabled/SetEnabled methods
- Remove enabled field from PushNotificationProcessor struct - Remove IsEnabled() and SetEnabled() methods from processor interface - Remove enabled parameter from NewPushNotificationProcessor() - Update all interfaces in pool package to remove IsEnabled requirement - Simplify processor logic - if processor exists, it works - VoidPushNotificationProcessor handles disabled case by discarding notifications - Update all tests to use simplified interface without enable/disable logic Benefits: - Simpler, cleaner interface with less complexity - No unnecessary state management for enabled/disabled - VoidPushNotificationProcessor pattern handles disabled case elegantly - Reduced cognitive overhead - processors just work when set - Eliminates redundant enabled checks throughout codebase - More predictable behavior - set processor = it works
1 parent fdfcf94 commit be9b6dd

File tree

7 files changed

+57
-99
lines changed

7 files changed

+57
-99
lines changed

internal/pool/conn.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ type Conn struct {
2828

2929
// Push notification processor for handling push notifications on this connection
3030
PushNotificationProcessor interface {
31-
IsEnabled() bool
3231
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
3332
}
3433
}

internal/pool/pool.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ type Options struct {
7575

7676
// Push notification processor for connections
7777
PushNotificationProcessor interface {
78-
IsEnabled() bool
7978
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
8079
}
8180
}
@@ -391,7 +390,7 @@ func (p *ConnPool) popIdle() (*Conn, error) {
391390
func (p *ConnPool) Put(ctx context.Context, cn *Conn) {
392391
if cn.rd.Buffered() > 0 {
393392
// Check if this might be push notification data
394-
if cn.PushNotificationProcessor != nil && cn.PushNotificationProcessor.IsEnabled() {
393+
if cn.PushNotificationProcessor != nil {
395394
// Try to process pending push notifications before discarding connection
396395
err := cn.PushNotificationProcessor.ProcessPendingNotifications(ctx, cn.rd)
397396
if err != nil {
@@ -555,7 +554,7 @@ func (p *ConnPool) isHealthyConn(cn *Conn) bool {
555554
if err := connCheck(cn.netConn); err != nil {
556555
// If there's unexpected data and we have push notification support,
557556
// it might be push notifications
558-
if err == errUnexpectedRead && cn.PushNotificationProcessor != nil && cn.PushNotificationProcessor.IsEnabled() {
557+
if err == errUnexpectedRead && cn.PushNotificationProcessor != nil {
559558
// Try to process any pending push notifications
560559
ctx := context.Background()
561560
if procErr := cn.PushNotificationProcessor.ProcessPendingNotifications(ctx, cn.rd); procErr != nil {

pubsub.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,16 @@ func (c *PubSub) newMessage(reply interface{}) (interface{}, error) {
435435
}, nil
436436
default:
437437
// Try to handle as generic push notification
438-
if c.pushProcessor.IsEnabled() {
439-
ctx := c.getContext()
440-
registry := c.pushProcessor.GetRegistry()
441-
if registry != nil {
442-
handled := registry.HandleNotification(ctx, reply)
443-
if handled {
444-
// Return a special message type to indicate it was handled
445-
return &PushNotificationMessage{
446-
Command: kind,
447-
Args: reply[1:],
448-
}, nil
449-
}
438+
ctx := c.getContext()
439+
registry := c.pushProcessor.GetRegistry()
440+
if registry != nil {
441+
handled := registry.HandleNotification(ctx, reply)
442+
if handled {
443+
// Return a special message type to indicate it was handled
444+
return &PushNotificationMessage{
445+
Command: kind,
446+
Args: reply[1:],
447+
}, nil
450448
}
451449
}
452450
return nil, fmt.Errorf("redis: unsupported pubsub message: %q", kind)

push_notification_coverage_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ func TestConnectionPoolPushNotificationIntegration(t *testing.T) {
5656
t.Error("Connection should have push notification processor assigned")
5757
}
5858

59-
if !cn.PushNotificationProcessor.IsEnabled() {
60-
t.Error("Connection push notification processor should be enabled")
61-
}
59+
// Connection should have a processor (no need to check IsEnabled anymore)
6260

6361
// Test ProcessPendingNotifications method
6462
emptyReader := proto.NewReader(bytes.NewReader([]byte{}))
@@ -156,8 +154,9 @@ func TestConnPushNotificationMethods(t *testing.T) {
156154
t.Error("Conn should have push notification processor")
157155
}
158156

159-
if !processor.IsEnabled() {
160-
t.Error("Conn push notification processor should be enabled")
157+
// Processor should have a registry when enabled
158+
if processor.GetRegistry() == nil {
159+
t.Error("Conn push notification processor should have a registry when enabled")
161160
}
162161

163162
// Test RegisterPushNotificationHandler
@@ -218,8 +217,9 @@ func TestConnWithoutPushNotifications(t *testing.T) {
218217
if processor == nil {
219218
t.Error("Conn should always have a push notification processor")
220219
}
221-
if processor.IsEnabled() {
222-
t.Error("Push notification processor should be disabled for RESP2")
220+
// VoidPushNotificationProcessor should have nil registry
221+
if processor.GetRegistry() != nil {
222+
t.Error("VoidPushNotificationProcessor should have nil registry for RESP2")
223223
}
224224

225225
// Test RegisterPushNotificationHandler returns nil (no error)
@@ -242,7 +242,7 @@ func TestConnWithoutPushNotifications(t *testing.T) {
242242
// TestNewConnWithCustomProcessor tests newConn with custom processor in options.
243243
func TestNewConnWithCustomProcessor(t *testing.T) {
244244
// Create custom processor
245-
customProcessor := NewPushNotificationProcessor(true)
245+
customProcessor := NewPushNotificationProcessor()
246246

247247
// Create options with custom processor
248248
opt := &Options{
@@ -377,7 +377,7 @@ func TestPushNotificationInfoStructure(t *testing.T) {
377377
// TestConnectionPoolOptionsIntegration tests that pool options correctly include processor.
378378
func TestConnectionPoolOptionsIntegration(t *testing.T) {
379379
// Create processor
380-
processor := NewPushNotificationProcessor(true)
380+
processor := NewPushNotificationProcessor()
381381

382382
// Create options
383383
opt := &Options{
@@ -401,7 +401,7 @@ func TestConnectionPoolOptionsIntegration(t *testing.T) {
401401

402402
// TestProcessPendingNotificationsEdgeCases tests edge cases in ProcessPendingNotifications.
403403
func TestProcessPendingNotificationsEdgeCases(t *testing.T) {
404-
processor := NewPushNotificationProcessor(true)
404+
processor := NewPushNotificationProcessor()
405405
ctx := context.Background()
406406

407407
// Test with nil reader (should not panic)
@@ -417,10 +417,10 @@ func TestProcessPendingNotificationsEdgeCases(t *testing.T) {
417417
t.Errorf("Should not error with empty reader: %v", err)
418418
}
419419

420-
// Test with disabled processor
421-
disabledProcessor := NewPushNotificationProcessor(false)
422-
err = disabledProcessor.ProcessPendingNotifications(ctx, emptyReader)
420+
// Test with void processor (simulates disabled state)
421+
voidProcessor := NewVoidPushNotificationProcessor()
422+
err = voidProcessor.ProcessPendingNotifications(ctx, emptyReader)
423423
if err != nil {
424-
t.Errorf("Disabled processor should not error: %v", err)
424+
t.Errorf("Void processor should not error: %v", err)
425425
}
426426
}

push_notifications.go

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,6 @@ func (r *PushNotificationRegistry) HasHandlers() bool {
106106

107107
// PushNotificationProcessorInterface defines the interface for push notification processors.
108108
type PushNotificationProcessorInterface interface {
109-
IsEnabled() bool
110-
SetEnabled(enabled bool)
111109
GetRegistry() *PushNotificationRegistry
112110
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
113111
RegisterHandler(pushNotificationName string, handler PushNotificationHandler, protected bool) error
@@ -116,40 +114,23 @@ type PushNotificationProcessorInterface interface {
116114
// PushNotificationProcessor handles the processing of push notifications from Redis.
117115
type PushNotificationProcessor struct {
118116
registry *PushNotificationRegistry
119-
enabled bool
120-
mu sync.RWMutex // Protects enabled field
121117
}
122118

123119
// NewPushNotificationProcessor creates a new push notification processor.
124-
func NewPushNotificationProcessor(enabled bool) *PushNotificationProcessor {
120+
func NewPushNotificationProcessor() *PushNotificationProcessor {
125121
return &PushNotificationProcessor{
126122
registry: NewPushNotificationRegistry(),
127-
enabled: enabled,
128123
}
129124
}
130125

131-
// IsEnabled returns whether push notification processing is enabled.
132-
func (p *PushNotificationProcessor) IsEnabled() bool {
133-
p.mu.RLock()
134-
defer p.mu.RUnlock()
135-
return p.enabled
136-
}
137-
138-
// SetEnabled enables or disables push notification processing.
139-
func (p *PushNotificationProcessor) SetEnabled(enabled bool) {
140-
p.mu.Lock()
141-
defer p.mu.Unlock()
142-
p.enabled = enabled
143-
}
144-
145126
// GetRegistry returns the push notification registry.
146127
func (p *PushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
147128
return p.registry
148129
}
149130

150131
// ProcessPendingNotifications checks for and processes any pending push notifications.
151132
func (p *PushNotificationProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
152-
if !p.IsEnabled() || !p.registry.HasHandlers() {
133+
if !p.registry.HasHandlers() {
153134
return nil
154135
}
155136

@@ -252,16 +233,6 @@ func NewVoidPushNotificationProcessor() *VoidPushNotificationProcessor {
252233
return &VoidPushNotificationProcessor{}
253234
}
254235

255-
// IsEnabled always returns false for void processor.
256-
func (v *VoidPushNotificationProcessor) IsEnabled() bool {
257-
return false
258-
}
259-
260-
// SetEnabled is a no-op for void processor.
261-
func (v *VoidPushNotificationProcessor) SetEnabled(enabled bool) {
262-
// No-op: void processor is always disabled
263-
}
264-
265236
// GetRegistry returns nil for void processor since it doesn't maintain handlers.
266237
func (v *VoidPushNotificationProcessor) GetRegistry() *PushNotificationRegistry {
267238
return nil

push_notifications_test.go

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ func TestPushNotificationRegistry(t *testing.T) {
8787

8888
func TestPushNotificationProcessor(t *testing.T) {
8989
// Test the push notification processor
90-
processor := redis.NewPushNotificationProcessor(true)
90+
processor := redis.NewPushNotificationProcessor()
9191

92-
if !processor.IsEnabled() {
93-
t.Error("Processor should be enabled")
92+
if processor.GetRegistry() == nil {
93+
t.Error("Processor should have a registry")
9494
}
9595

9696
// Test registering handlers
@@ -124,10 +124,9 @@ func TestPushNotificationProcessor(t *testing.T) {
124124
t.Error("Specific handler should have been called")
125125
}
126126

127-
// Test disabling processor
128-
processor.SetEnabled(false)
129-
if processor.IsEnabled() {
130-
t.Error("Processor should be disabled")
127+
// Test that processor always has a registry (no enable/disable anymore)
128+
if processor.GetRegistry() == nil {
129+
t.Error("Processor should always have a registry")
131130
}
132131
}
133132

@@ -146,8 +145,8 @@ func TestClientPushNotificationIntegration(t *testing.T) {
146145
t.Error("Push notification processor should be initialized")
147146
}
148147

149-
if !processor.IsEnabled() {
150-
t.Error("Push notification processor should be enabled")
148+
if processor.GetRegistry() == nil {
149+
t.Error("Push notification processor should have a registry when enabled")
151150
}
152151

153152
// Test registering handlers through client
@@ -187,8 +186,9 @@ func TestClientWithoutPushNotifications(t *testing.T) {
187186
if processor == nil {
188187
t.Error("Push notification processor should never be nil")
189188
}
190-
if processor.IsEnabled() {
191-
t.Error("Push notification processor should be disabled when PushNotifications is false")
189+
// VoidPushNotificationProcessor should have nil registry
190+
if processor.GetRegistry() != nil {
191+
t.Error("VoidPushNotificationProcessor should have nil registry")
192192
}
193193

194194
// Registering handlers should not panic
@@ -215,8 +215,8 @@ func TestPushNotificationEnabledClient(t *testing.T) {
215215
t.Error("Push notification processor should be initialized when enabled")
216216
}
217217

218-
if !processor.IsEnabled() {
219-
t.Error("Push notification processor should be enabled")
218+
if processor.GetRegistry() == nil {
219+
t.Error("Push notification processor should have a registry when enabled")
220220
}
221221

222222
// Test registering a handler
@@ -561,10 +561,10 @@ func TestPushNotificationRegistrySpecificHandlerOnly(t *testing.T) {
561561

562562
func TestPushNotificationProcessorEdgeCases(t *testing.T) {
563563
// Test processor with disabled state
564-
processor := redis.NewPushNotificationProcessor(false)
564+
processor := redis.NewPushNotificationProcessor()
565565

566-
if processor.IsEnabled() {
567-
t.Error("Processor should be disabled")
566+
if processor.GetRegistry() == nil {
567+
t.Error("Processor should have a registry")
568568
}
569569

570570
// Test that disabled processor doesn't process notifications
@@ -587,15 +587,14 @@ func TestPushNotificationProcessorEdgeCases(t *testing.T) {
587587
t.Error("Handler should be called when using registry directly")
588588
}
589589

590-
// Test enabling processor
591-
processor.SetEnabled(true)
592-
if !processor.IsEnabled() {
593-
t.Error("Processor should be enabled after SetEnabled(true)")
590+
// Test that processor always has a registry
591+
if processor.GetRegistry() == nil {
592+
t.Error("Processor should always have a registry")
594593
}
595594
}
596595

597596
func TestPushNotificationProcessorConvenienceMethods(t *testing.T) {
598-
processor := redis.NewPushNotificationProcessor(true)
597+
processor := redis.NewPushNotificationProcessor()
599598

600599
// Test RegisterHandler convenience method
601600
handlerCalled := false
@@ -822,7 +821,7 @@ func TestPushNotificationRegistryConcurrency(t *testing.T) {
822821

823822
func TestPushNotificationProcessorConcurrency(t *testing.T) {
824823
// Test thread safety of the processor
825-
processor := redis.NewPushNotificationProcessor(true)
824+
processor := redis.NewPushNotificationProcessor()
826825

827826
numGoroutines := 5
828827
numOperations := 50
@@ -845,13 +844,7 @@ func TestPushNotificationProcessorConcurrency(t *testing.T) {
845844
notification := []interface{}{command, "data"}
846845
processor.GetRegistry().HandleNotification(context.Background(), notification)
847846

848-
// Toggle processor state occasionally
849-
if j%20 == 0 {
850-
processor.SetEnabled(!processor.IsEnabled())
851-
}
852-
853847
// Access processor state
854-
processor.IsEnabled()
855848
processor.GetRegistry()
856849
}
857850
}(i)
@@ -898,7 +891,7 @@ func TestPushNotificationClientConcurrency(t *testing.T) {
898891
// Access processor
899892
processor := client.GetPushNotificationProcessor()
900893
if processor != nil {
901-
processor.IsEnabled()
894+
processor.GetRegistry()
902895
}
903896
}
904897
}(i)
@@ -929,8 +922,11 @@ func TestPushNotificationConnectionHealthCheck(t *testing.T) {
929922

930923
// Verify push notifications are enabled
931924
processor := client.GetPushNotificationProcessor()
932-
if processor == nil || !processor.IsEnabled() {
933-
t.Fatal("Push notifications should be enabled")
925+
if processor == nil {
926+
t.Fatal("Push notification processor should not be nil")
927+
}
928+
if processor.GetRegistry() == nil {
929+
t.Fatal("Push notification registry should not be nil when enabled")
934930
}
935931

936932
// Register a handler for testing
@@ -959,11 +955,6 @@ func TestPushNotificationConnectionHealthCheck(t *testing.T) {
959955
return
960956
}
961957

962-
if !cn.PushNotificationProcessor.IsEnabled() {
963-
t.Error("Push notification processor should be enabled on connection")
964-
return
965-
}
966-
967958
t.Log("✅ Connection has push notification processor correctly set")
968959
t.Log("✅ Connection health check integration working correctly")
969960
}

redis.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (c *baseClient) _process(ctx context.Context, cmd Cmder, attempt int) (bool
535535
}
536536
if err := cn.WithReader(c.context(ctx), c.cmdTimeout(cmd), func(rd *proto.Reader) error {
537537
// Check for push notifications before reading the command reply
538-
if c.opt.Protocol == 3 && c.pushProcessor.IsEnabled() {
538+
if c.opt.Protocol == 3 {
539539
if err := c.pushProcessor.ProcessPendingNotifications(ctx, rd); err != nil {
540540
internal.Logger.Printf(ctx, "push: error processing push notifications: %v", err)
541541
}
@@ -818,7 +818,7 @@ func (c *Client) initializePushProcessor() {
818818
c.pushProcessor = c.opt.PushNotificationProcessor
819819
} else if c.opt.PushNotifications {
820820
// Create default processor when push notifications are enabled
821-
c.pushProcessor = NewPushNotificationProcessor(true)
821+
c.pushProcessor = NewPushNotificationProcessor()
822822
} else {
823823
// Create void processor when push notifications are disabled
824824
c.pushProcessor = NewVoidPushNotificationProcessor()

0 commit comments

Comments
 (0)