Skip to content

Commit d780401

Browse files
committed
refactor(push): simplify handler context
1 parent 84123b1 commit d780401

File tree

5 files changed

+199
-352
lines changed

5 files changed

+199
-352
lines changed

pubsub.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,11 @@ func (c *PubSub) processPendingPushNotificationWithReader(ctx context.Context, c
555555
func (c *PubSub) pushNotificationHandlerContext(cn *pool.Conn) push.NotificationHandlerContext {
556556
// PubSub doesn't have a client or connection pool, so we pass nil for those
557557
// PubSub connections are blocking
558-
return push.HandlerContext{}
559-
return push.NewNotificationHandlerContext(nil, nil, c, cn, true)
558+
return push.NotificationHandlerContext{
559+
PubSub: c,
560+
Conn: cn,
561+
IsBlocking: true,
562+
}
560563
}
561564

562565
type ChannelOption func(c *channel)

push/handler_context.go

Lines changed: 13 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,38 @@ import (
55
)
66

77
// NotificationHandlerContext provides context information about where a push notification was received.
8-
// This interface allows handlers to make informed decisions based on the source of the notification
8+
// This struct allows handlers to make informed decisions based on the source of the notification
99
// with strongly typed access to different client types using concrete types.
10-
type NotificationHandlerContext interface {
11-
// GetClient returns the Redis client instance that received the notification.
12-
// Returns nil if no client context is available.
10+
type NotificationHandlerContext struct {
11+
// Client is the Redis client instance that received the notification.
1312
// It is interface to both allow for future expansion and to avoid
1413
// circular dependencies. The developer is responsible for type assertion.
1514
// It can be one of the following types:
15+
// - *redis.baseClient
1616
// - *redis.Client
1717
// - *redis.ClusterClient
1818
// - *redis.Conn
19-
GetClient() interface{}
19+
Client interface{}
2020

21-
// GetConnPool returns the connection pool from which the connection was obtained.
22-
// Returns nil if no connection pool context is available.
21+
// ConnPool is the connection pool from which the connection was obtained.
2322
// It is interface to both allow for future expansion and to avoid
2423
// circular dependencies. The developer is responsible for type assertion.
2524
// It can be one of the following types:
2625
// - *pool.ConnPool
2726
// - *pool.SingleConnPool
2827
// - *pool.StickyConnPool
29-
GetConnPool() interface{}
28+
ConnPool interface{}
3029

31-
// GetPubSub returns the PubSub instance that received the notification.
32-
// Returns nil if this is not a PubSub connection.
30+
// PubSub is the PubSub instance that received the notification.
3331
// It is interface to both allow for future expansion and to avoid
3432
// circular dependencies. The developer is responsible for type assertion.
3533
// It can be one of the following types:
3634
// - *redis.PubSub
37-
GetPubSub() interface{}
35+
PubSub interface{}
3836

39-
// GetConn returns the specific connection on which the notification was received.
40-
// Returns nil if no connection context is available.
41-
GetConn() *pool.Conn
37+
// Conn is the specific connection on which the notification was received.
38+
Conn *pool.Conn
4239

43-
// IsBlocking returns true if the notification was received on a blocking connection.
44-
IsBlocking() bool
45-
}
46-
47-
// pushNotificationHandlerContext is the concrete implementation of PushNotificationHandlerContext interface
48-
type pushNotificationHandlerContext struct {
49-
client interface{}
50-
connPool interface{}
51-
pubSub interface{}
52-
conn *pool.Conn
53-
isBlocking bool
54-
}
55-
56-
// NewNotificationHandlerContext creates a new push.NotificationHandlerContext instance
57-
func NewNotificationHandlerContext(client, connPool, pubSub interface{}, conn *pool.Conn, isBlocking bool) NotificationHandlerContext {
58-
return &pushNotificationHandlerContext{
59-
client: client,
60-
connPool: connPool,
61-
pubSub: pubSub,
62-
conn: conn,
63-
isBlocking: isBlocking,
64-
}
65-
}
66-
67-
// GetClient returns the Redis client instance that received the notification
68-
func (h *pushNotificationHandlerContext) GetClient() interface{} {
69-
return h.client
70-
}
71-
72-
// GetConnPool returns the connection pool from which the connection was obtained
73-
func (h *pushNotificationHandlerContext) GetConnPool() interface{} {
74-
return h.connPool
75-
}
76-
77-
func (h *pushNotificationHandlerContext) GetPubSub() interface{} {
78-
return h.pubSub
79-
}
80-
81-
// GetConn returns the specific connection on which the notification was received
82-
func (h *pushNotificationHandlerContext) GetConn() *pool.Conn {
83-
return h.conn
84-
}
85-
86-
// IsBlocking returns true if the notification was received on a blocking connection
87-
func (h *pushNotificationHandlerContext) IsBlocking() bool {
88-
return h.isBlocking
40+
// IsBlocking indicates if the notification was received on a blocking connection.
41+
IsBlocking bool
8942
}

0 commit comments

Comments
 (0)