From 1331fb995731591c03b3597ef7983223c018f87c Mon Sep 17 00:00:00 2001 From: Nedyalko Dyakov Date: Thu, 26 Jun 2025 21:30:27 +0300 Subject: [PATCH] fix: remove unused fields and ensure push notifications work in cloned clients - Remove unused Timestamp and Source fields from PushNotificationInfo - Add pushProcessor to newConn function to ensure Conn instances have push notifications - Add push notification methods to Conn type for consistency - Ensure cloned clients and Conn instances preserve push notification functionality This fixes issues where: 1. PushNotificationInfo had unused fields causing confusion 2. Conn instances created via client.Conn() lacked push notification support 3. All client types now consistently support push notifications --- push_notifications.go | 6 ++---- redis.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/push_notifications.go b/push_notifications.go index ec251ed2..b49e6cfe 100644 --- a/push_notifications.go +++ b/push_notifications.go @@ -225,10 +225,8 @@ const ( // PushNotificationInfo contains metadata about a push notification. type PushNotificationInfo struct { - Command string - Args []interface{} - Timestamp int64 - Source string + Command string + Args []interface{} } // ParsePushNotificationInfo extracts information from a push notification. diff --git a/redis.go b/redis.go index 67188875..c45ba953 100644 --- a/redis.go +++ b/redis.go @@ -982,6 +982,11 @@ func newConn(opt *Options, connPool pool.Pooler, parentHooks *hooksMixin) *Conn c.hooksMixin = parentHooks.clone() } + // Set push notification processor if available in options + if opt.PushNotificationProcessor != nil { + c.pushProcessor = opt.PushNotificationProcessor + } + c.cmdable = c.Process c.statefulCmdable = c.Process c.initHooks(hooks{ @@ -1000,6 +1005,29 @@ func (c *Conn) Process(ctx context.Context, cmd Cmder) error { return err } +// RegisterPushNotificationHandler registers a handler for a specific push notification command. +// Returns an error if a handler is already registered for this command. +func (c *Conn) RegisterPushNotificationHandler(command string, handler PushNotificationHandler) error { + if c.pushProcessor != nil { + return c.pushProcessor.RegisterHandler(command, handler) + } + return nil +} + +// RegisterPushNotificationHandlerFunc registers a function as a handler for a specific push notification command. +// Returns an error if a handler is already registered for this command. +func (c *Conn) RegisterPushNotificationHandlerFunc(command string, handlerFunc func(ctx context.Context, notification []interface{}) bool) error { + if c.pushProcessor != nil { + return c.pushProcessor.RegisterHandlerFunc(command, handlerFunc) + } + return nil +} + +// GetPushNotificationProcessor returns the push notification processor. +func (c *Conn) GetPushNotificationProcessor() *PushNotificationProcessor { + return c.pushProcessor +} + func (c *Conn) Pipelined(ctx context.Context, fn func(Pipeliner) error) ([]Cmder, error) { return c.Pipeline().Pipelined(ctx, fn) }