push

package
v9.15.0-beta.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 26, 2025 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package push provides push notifications for Redis. This is an EXPERIMENTAL API for handling push notifications from Redis. It is not yet stable and may change in the future. Although this is in a public package, in its current form public use is not advised. Pending push notifications should be processed before executing any readReply from the connection as per RESP3 specification push notifications can be sent at any time.

Index

Constants

View Source
const (
	// HandlerReasons
	ReasonHandlerNil       = "handler cannot be nil"
	ReasonHandlerExists    = "cannot overwrite existing handler"
	ReasonHandlerProtected = "handler is protected"

	// ProcessorReasons
	ReasonPushNotificationsDisabled = "push notifications are disabled"
)

Error reason constants

View Source
const (
	// ProcessorTypes
	ProcessorTypeProcessor     = ProcessorType("processor")
	ProcessorTypeVoidProcessor = ProcessorType("void_processor")
	ProcessorTypeCustom        = ProcessorType("custom")
)
View Source
const (
	// ProcessorOperations
	ProcessorOperationProcess    = ProcessorOperation("process")
	ProcessorOperationRegister   = ProcessorOperation("register")
	ProcessorOperationUnregister = ProcessorOperation("unregister")
	ProcessorOperationUnknown    = ProcessorOperation("unknown")
)

Variables

View Source
var (
	// ErrHandlerNil is returned when attempting to register a nil handler
	ErrHandlerNil = errors.New(ReasonHandlerNil)
)

Common error variables for reuse

Functions

func ErrHandlerExists

func ErrHandlerExists(pushNotificationName string) error

ErrHandlerExists creates an error for when attempting to overwrite an existing handler

func ErrProtectedHandler

func ErrProtectedHandler(pushNotificationName string) error

ErrProtectedHandler creates an error for when attempting to unregister a protected handler

func ErrVoidProcessorRegister

func ErrVoidProcessorRegister(pushNotificationName string) error

ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor

func ErrVoidProcessorUnregister

func ErrVoidProcessorUnregister(pushNotificationName string) error

ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor

func IsHandlerExistsError

func IsHandlerExistsError(err error) bool

IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler

func IsHandlerNilError

func IsHandlerNilError(err error) bool

IsHandlerNilError checks if an error is due to a nil handler

func IsProtectedHandlerError

func IsProtectedHandlerError(err error) bool

IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler

func IsVoidProcessorError

func IsVoidProcessorError(err error) bool

IsVoidProcessorError checks if an error is due to void processor operations

Types

type HandlerError

type HandlerError struct {
	Operation            ProcessorOperation
	PushNotificationName string
	Reason               string
	Err                  error
}

HandlerError represents errors related to handler operations

func NewHandlerError

func NewHandlerError(operation ProcessorOperation, pushNotificationName, reason string, err error) *HandlerError

NewHandlerError creates a new HandlerError

func (*HandlerError) Error

func (e *HandlerError) Error() string

func (*HandlerError) Unwrap

func (e *HandlerError) Unwrap() error

type NotificationHandler

type NotificationHandler interface {
	// HandlePushNotification processes a push notification with context information.
	// The handlerCtx provides information about the client, connection pool, and connection
	// on which the notification was received, allowing handlers to make informed decisions.
	// Returns an error if the notification could not be handled.
	HandlePushNotification(ctx context.Context, handlerCtx NotificationHandlerContext, notification []interface{}) error
}

NotificationHandler defines the interface for push notification handlers.

type NotificationHandlerContext

type NotificationHandlerContext struct {
	// Client is the Redis client instance that received the notification.
	// It is interface to both allow for future expansion and to avoid
	// circular dependencies. The developer is responsible for type assertion.
	// It can be one of the following types:
	// - *redis.baseClient
	// - *redis.Client
	// - *redis.ClusterClient
	// - *redis.Conn
	Client interface{}

	// ConnPool is the connection pool from which the connection was obtained.
	// It is interface to both allow for future expansion and to avoid
	// circular dependencies. The developer is responsible for type assertion.
	// It can be one of the following types:
	// - *pool.ConnPool
	// - *pool.SingleConnPool
	// - *pool.StickyConnPool
	ConnPool interface{}

	// PubSub is the PubSub instance that received the notification.
	// It is interface to both allow for future expansion and to avoid
	// circular dependencies. The developer is responsible for type assertion.
	// It can be one of the following types:
	// - *redis.PubSub
	PubSub interface{}

	// Conn is the specific connection on which the notification was received.
	// It is interface to both allow for future expansion and to avoid
	// circular dependencies. The developer is responsible for type assertion.
	// It can be one of the following types:
	// - *pool.Conn
	Conn interface{}

	// IsBlocking indicates if the notification was received on a blocking connection.
	IsBlocking bool
}

NotificationHandlerContext provides context information about where a push notification was received. This struct allows handlers to make informed decisions based on the source of the notification with strongly typed access to different client types using concrete types.

type NotificationProcessor

type NotificationProcessor interface {
	// GetHandler returns the handler for a specific push notification name.
	GetHandler(pushNotificationName string) NotificationHandler
	// ProcessPendingNotifications checks for and processes any pending push notifications.
	// To be used when it is known that there are notifications on the socket.
	// It will try to read from the socket and if it is empty - it may block.
	ProcessPendingNotifications(ctx context.Context, handlerCtx NotificationHandlerContext, rd *proto.Reader) error
	// RegisterHandler registers a handler for a specific push notification name.
	RegisterHandler(pushNotificationName string, handler NotificationHandler, protected bool) error
	// UnregisterHandler removes a handler for a specific push notification name.
	UnregisterHandler(pushNotificationName string) error
}

NotificationProcessor defines the interface for push notification processors.

type Processor

type Processor struct {
	// contains filtered or unexported fields
}

Processor handles push notifications with a registry of handlers

func NewProcessor

func NewProcessor() *Processor

NewProcessor creates a new push notification processor

func (*Processor) GetHandler

func (p *Processor) GetHandler(pushNotificationName string) NotificationHandler

GetHandler returns the handler for a specific push notification name

func (*Processor) ProcessPendingNotifications

func (p *Processor) ProcessPendingNotifications(ctx context.Context, handlerCtx NotificationHandlerContext, rd *proto.Reader) error

ProcessPendingNotifications checks for and processes any pending push notifications This method should be called by the client in WithReader before reading the reply It will try to read from the socket and if it is empty - it may block.

func (*Processor) RegisterHandler

func (p *Processor) RegisterHandler(pushNotificationName string, handler NotificationHandler, protected bool) error

RegisterHandler registers a handler for a specific push notification name

func (*Processor) UnregisterHandler

func (p *Processor) UnregisterHandler(pushNotificationName string) error

UnregisterHandler removes a handler for a specific push notification name

type ProcessorError

type ProcessorError struct {
	ProcessorType        ProcessorType      // "processor", "void_processor"
	Operation            ProcessorOperation // "process", "register", "unregister"
	PushNotificationName string             // Name of the push notification involved
	Reason               string
	Err                  error
}

ProcessorError represents errors related to processor operations

func NewProcessorError

func NewProcessorError(processorType ProcessorType, operation ProcessorOperation, pushNotificationName, reason string, err error) *ProcessorError

NewProcessorError creates a new ProcessorError

func (*ProcessorError) Error

func (e *ProcessorError) Error() string

func (*ProcessorError) Unwrap

func (e *ProcessorError) Unwrap() error

type ProcessorOperation

type ProcessorOperation string

ProcessorOperation represents the operation being performed by the processor defined as a custom type for better readability and easier maintenance

type ProcessorType

type ProcessorType string

ProcessorType represents the type of processor involved in the error defined as a custom type for better readability and easier maintenance

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry manages push notification handlers

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new push notification registry

func (*Registry) GetHandler

func (r *Registry) GetHandler(pushNotificationName string) NotificationHandler

GetHandler returns the handler for a specific push notification name

func (*Registry) RegisterHandler

func (r *Registry) RegisterHandler(pushNotificationName string, handler NotificationHandler, protected bool) error

RegisterHandler registers a handler for a specific push notification name

func (*Registry) UnregisterHandler

func (r *Registry) UnregisterHandler(pushNotificationName string) error

UnregisterHandler removes a handler for a specific push notification name

type VoidProcessor

type VoidProcessor struct{}

VoidProcessor discards all push notifications without processing them

func NewVoidProcessor

func NewVoidProcessor() *VoidProcessor

NewVoidProcessor creates a new void push notification processor

func (*VoidProcessor) GetHandler

func (v *VoidProcessor) GetHandler(_ string) NotificationHandler

GetHandler returns nil for void processor since it doesn't maintain handlers

func (*VoidProcessor) ProcessPendingNotifications

func (v *VoidProcessor) ProcessPendingNotifications(_ context.Context, handlerCtx NotificationHandlerContext, rd *proto.Reader) error

ProcessPendingNotifications for VoidProcessor does nothing since push notifications are only available in RESP3 and this processor is used for RESP2 connections. This avoids unnecessary buffer scanning overhead. It does however read and discard all push notifications from the buffer to avoid them being interpreted as a reply. This method should be called by the client in WithReader before reading the reply to be sure there are no buffered push notifications. It will try to read from the socket and if it is empty - it may block.

func (*VoidProcessor) RegisterHandler

func (v *VoidProcessor) RegisterHandler(pushNotificationName string, _ NotificationHandler, _ bool) error

RegisterHandler returns an error for void processor since it doesn't maintain handlers

func (*VoidProcessor) UnregisterHandler

func (v *VoidProcessor) UnregisterHandler(pushNotificationName string) error

UnregisterHandler returns an error for void processor since it doesn't maintain handlers

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL