1
0
mirror of https://github.com/redis/go-redis.git synced 2025-07-29 17:41:15 +03:00

fix: address pr review

This commit is contained in:
Nedyalko Dyakov
2025-06-27 18:07:13 +03:00
parent 075b9309c6
commit f7948b5c5c
6 changed files with 61 additions and 77 deletions

View File

@ -27,9 +27,8 @@ type Conn struct {
onClose func() error
// Push notification processor for handling push notifications on this connection
PushNotificationProcessor interface {
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
}
// Uses the same interface as defined in pool.go to avoid duplication
PushNotificationProcessor PushNotificationProcessorInterface
}
func NewConn(netConn net.Conn) *Conn {

View File

@ -24,6 +24,8 @@ var (
ErrPoolTimeout = errors.New("redis: connection pool timeout")
)
var timers = sync.Pool{
New: func() interface{} {
t := time.NewTimer(time.Hour)
@ -60,6 +62,12 @@ type Pooler interface {
Close() error
}
// PushNotificationProcessorInterface defines the interface for push notification processors.
// This matches the main PushNotificationProcessorInterface to avoid duplication while preventing circular imports.
type PushNotificationProcessorInterface interface {
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
}
type Options struct {
Dialer func(context.Context) (net.Conn, error)
@ -74,9 +82,12 @@ type Options struct {
ConnMaxLifetime time.Duration
// Push notification processor for connections
PushNotificationProcessor interface {
ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error
}
// This interface matches PushNotificationProcessorInterface to avoid duplication
// while preventing circular imports
PushNotificationProcessor PushNotificationProcessorInterface
// Protocol version for optimization (3 = RESP3 with push notifications, 2 = RESP2 without)
Protocol int
}
type lastDialErrorWrap struct {
@ -390,8 +401,8 @@ func (p *ConnPool) popIdle() (*Conn, error) {
func (p *ConnPool) Put(ctx context.Context, cn *Conn) {
if cn.rd.Buffered() > 0 {
// Check if this might be push notification data
if cn.PushNotificationProcessor != nil {
// Try to process pending push notifications before discarding connection
if cn.PushNotificationProcessor != nil && p.cfg.Protocol == 3 {
// Only process for RESP3 clients (push notifications only available in RESP3)
err := cn.PushNotificationProcessor.ProcessPendingNotifications(ctx, cn.rd)
if err != nil {
internal.Logger.Printf(ctx, "push: error processing pending notifications: %v", err)
@ -553,9 +564,9 @@ func (p *ConnPool) isHealthyConn(cn *Conn) bool {
// Check connection health, but be aware of push notifications
if err := connCheck(cn.netConn); err != nil {
// If there's unexpected data and we have push notification support,
// it might be push notifications
if err == errUnexpectedRead && cn.PushNotificationProcessor != nil {
// Try to process any pending push notifications
// it might be push notifications (only for RESP3)
if err == errUnexpectedRead && cn.PushNotificationProcessor != nil && p.cfg.Protocol == 3 {
// Try to process any pending push notifications (only for RESP3)
ctx := context.Background()
if procErr := cn.PushNotificationProcessor.ProcessPendingNotifications(ctx, cn.rd); procErr != nil {
internal.Logger.Printf(ctx, "push: error processing pending notifications during health check: %v", procErr)

View File

@ -114,35 +114,12 @@ func (v *VoidProcessor) GetRegistryForTesting() *Registry {
return nil
}
// ProcessPendingNotifications reads and discards any pending push notifications.
// ProcessPendingNotifications for VoidProcessor does nothing since push notifications
// are only available in RESP3 and this processor is used when they're disabled.
// This avoids unnecessary buffer scanning overhead.
func (v *VoidProcessor) ProcessPendingNotifications(ctx context.Context, rd *proto.Reader) error {
// Check for nil reader
if rd == nil {
return nil
}
// Read and discard any pending push notifications to clean the buffer
for {
// Peek at the next reply type to see if it's a push notification
replyType, err := rd.PeekReplyType()
if err != nil {
// No more data available or error reading
break
}
// Push notifications use RespPush type in RESP3
if replyType != proto.RespPush {
break
}
// Read and discard the push notification
_, err = rd.ReadReply()
if err != nil {
return fmt.Errorf("failed to read push notification for discarding: %w", err)
}
// Notification discarded - continue to next one
}
// VoidProcessor is used when push notifications are disabled (typically RESP2 or disabled RESP3).
// Since push notifications only exist in RESP3, we can safely skip all processing
// to avoid unnecessary buffer scanning overhead.
return nil
}