Skip to content

Commit 3473c1e

Browse files
committed
fix: simplify api
1 parent f7948b5 commit 3473c1e

File tree

7 files changed

+26
-1521
lines changed

7 files changed

+26
-1521
lines changed

internal/pool/conn.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"time"
99

1010
"github.com/redis/go-redis/v9/internal/proto"
11+
"github.com/redis/go-redis/v9/internal/pushnotif"
1112
)
1213

1314
var noDeadline = time.Time{}
@@ -27,8 +28,8 @@ type Conn struct {
2728
onClose func() error
2829

2930
// Push notification processor for handling push notifications on this connection
30-
// Uses the same interface as defined in pool.go to avoid duplication
31-
PushNotificationProcessor PushNotificationProcessorInterface
31+
// This is set when the connection is created and is a reference to the processor
32+
PushNotificationProcessor pushnotif.ProcessorInterface
3233
}
3334

3435
func NewConn(netConn net.Conn) *Conn {

internal/pool/pool.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"time"
1010

1111
"github.com/redis/go-redis/v9/internal"
12-
"github.com/redis/go-redis/v9/internal/proto"
12+
"github.com/redis/go-redis/v9/internal/pushnotif"
1313
)
1414

1515
var (
@@ -24,8 +24,6 @@ var (
2424
ErrPoolTimeout = errors.New("redis: connection pool timeout")
2525
)
2626

27-
28-
2927
var timers = sync.Pool{
3028
New: func() interface{} {
3129
t := time.NewTimer(time.Hour)
@@ -62,12 +60,6 @@ type Pooler interface {
6260
Close() error
6361
}
6462

65-
// PushNotificationProcessorInterface defines the interface for push notification processors.
66-
// This matches the main PushNotificationProcessorInterface to avoid duplication while preventing circular imports.
67-
type PushNotificationProcessorInterface interface {
68-
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
69-
}
70-
7163
type Options struct {
7264
Dialer func(context.Context) (net.Conn, error)
7365

@@ -82,9 +74,8 @@ type Options struct {
8274
ConnMaxLifetime time.Duration
8375

8476
// Push notification processor for connections
85-
// This interface matches PushNotificationProcessorInterface to avoid duplication
86-
// while preventing circular imports
87-
PushNotificationProcessor PushNotificationProcessorInterface
77+
// This is an interface to avoid circular imports
78+
PushNotificationProcessor pushnotif.ProcessorInterface
8879

8980
// Protocol version for optimization (3 = RESP3 with push notifications, 2 = RESP2 without)
9081
Protocol int

internal/pushnotif/processor.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,7 @@ func (p *Processor) UnregisterHandler(pushNotificationName string) error {
3838
return p.registry.UnregisterHandler(pushNotificationName)
3939
}
4040

41-
// GetRegistryForTesting returns the push notification registry for testing.
42-
// This method should only be used by tests.
43-
func (p *Processor) GetRegistryForTesting() *Registry {
44-
return p.registry
45-
}
41+
4642

4743
// ProcessPendingNotifications checks for and processes any pending push notifications.
4844
func (p *Processor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
@@ -82,8 +78,17 @@ func (p *Processor) ProcessPendingNotifications(ctx context.Context, rd *proto.R
8278
continue
8379
}
8480

85-
// Handle the notification
86-
p.registry.HandleNotification(ctx, notification)
81+
// Handle the notification directly
82+
if len(notification) > 0 {
83+
// Extract the notification type (first element)
84+
if notificationType, ok := notification[0].(string); ok {
85+
// Get the handler for this notification type
86+
if handler := p.registry.GetHandler(notificationType); handler != nil {
87+
// Handle the notification
88+
handler.HandlePushNotification(ctx, notification)
89+
}
90+
}
91+
}
8792
}
8893

8994
return nil
@@ -108,11 +113,7 @@ func (v *VoidProcessor) RegisterHandler(pushNotificationName string, handler Han
108113
return fmt.Errorf("cannot register push notification handler '%s': push notifications are disabled (using void processor)", pushNotificationName)
109114
}
110115

111-
// GetRegistryForTesting returns nil for void processor since it doesn't maintain handlers.
112-
// This method should only be used by tests.
113-
func (v *VoidProcessor) GetRegistryForTesting() *Registry {
114-
return nil
115-
}
116+
116117

117118
// ProcessPendingNotifications for VoidProcessor does nothing since push notifications
118119
// are only available in RESP3 and this processor is used when they're disabled.

internal/pushnotif/registry.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package pushnotif
22

33
import (
4-
"context"
54
"fmt"
65
"sync"
76
)
@@ -82,25 +81,4 @@ func (r *Registry) GetRegisteredPushNotificationNames() []string {
8281
return names
8382
}
8483

85-
// HandleNotification attempts to handle a push notification using registered handlers.
86-
// Returns true if a handler was found and successfully processed the notification.
87-
func (r *Registry) HandleNotification(ctx context.Context, notification []interface{}) bool {
88-
if len(notification) == 0 {
89-
return false
90-
}
91-
92-
// Extract the notification type (first element)
93-
notificationType, ok := notification[0].(string)
94-
if !ok {
95-
return false
96-
}
9784

98-
// Get the handler for this notification type
99-
handler := r.GetHandler(notificationType)
100-
if handler == nil {
101-
return false
102-
}
103-
104-
// Handle the notification
105-
return handler.HandlePushNotification(ctx, notification)
106-
}

0 commit comments

Comments
 (0)