push
v9.24.1
Documentation
pkg.go.dev ↗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.
Constants
ReasonHandlerNil (4 declarations)
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
ProcessorTypeProcessor (3 declarations)
const (
// ProcessorTypes
ProcessorTypeProcessor = ProcessorType("processor")
ProcessorTypeVoidProcessor = ProcessorType("void_processor")
ProcessorTypeCustom = ProcessorType("custom")
)
ProcessorOperationProcess (4 declarations)
const (
// ProcessorOperations
ProcessorOperationProcess = ProcessorOperation("process")
ProcessorOperationRegister = ProcessorOperation("register")
ProcessorOperationUnregister = ProcessorOperation("unregister")
ProcessorOperationUnknown = ProcessorOperation("unknown")
)
Variables
ErrHandlerNil
var ( // ErrHandlerNil is returned when attempting to register a nil handler ErrHandlerNil = errors.New(ReasonHandlerNil) )
Common error variables for reuse
Functions
ErrHandlerExists
func ErrHandlerExists(pushNotificationName string) error
ErrHandlerExists creates an error for when attempting to overwrite an existing handler
ErrProtectedHandler
func ErrProtectedHandler(pushNotificationName string) error
ErrProtectedHandler creates an error for when attempting to unregister a protected handler
ErrVoidProcessorRegister
func ErrVoidProcessorRegister(pushNotificationName string) error
ErrVoidProcessorRegister creates an error for when attempting to register a handler on void processor
ErrVoidProcessorUnregister
func ErrVoidProcessorUnregister(pushNotificationName string) error
ErrVoidProcessorUnregister creates an error for when attempting to unregister a handler on void processor
IsHandlerExistsError
func IsHandlerExistsError(err error) bool
IsHandlerExistsError checks if an error is due to attempting to overwrite an existing handler. This function works correctly even when the error is wrapped.
IsHandlerNilError
func IsHandlerNilError(err error) bool
IsHandlerNilError checks if an error is due to a nil handler
IsProtectedHandlerError
func IsProtectedHandlerError(err error) bool
IsProtectedHandlerError checks if an error is due to attempting to unregister a protected handler. This function works correctly even when the error is wrapped.
IsVoidProcessorError
func IsVoidProcessorError(err error) bool
IsVoidProcessorError checks if an error is due to void processor operations. This function works correctly even when the error is wrapped.
Types
HandlerError
type HandlerError struct {
Operation ProcessorOperation
PushNotificationName string
Reason string
Err error
}
HandlerError represents errors related to handler operations
NewHandlerError
func NewHandlerError(operation ProcessorOperation, pushNotificationName, reason string, err error) *HandlerError
NewHandlerError creates a new HandlerError
HandlerError.Error
func (e *HandlerError) Error() string
HandlerError.Unwrap
func (e *HandlerError) Unwrap() error
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.
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.
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.
Processor
type Processor struct {
// contains filtered or unexported fields
}
Processor handles push notifications with a registry of handlers
NewProcessor
func NewProcessor() *Processor
NewProcessor creates a new push notification processor
Processor.GetHandler
func (p *Processor) GetHandler(pushNotificationName string) NotificationHandler
GetHandler returns the handler for a specific push notification name
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.
Processor.ProcessPendingNotificationsBuffered
func (p *Processor) ProcessPendingNotificationsBuffered( ctx context.Context, handlerCtx NotificationHandlerContext, rd *proto.Reader, ) error
ProcessPendingNotificationsBuffered processes one pending push notification and then continues only through frames already buffered by that read. It is used by callers that have already established socket readiness and must not wait for another frame after draining the current batch.
After the first notification it starts a push frame only when the whole frame is buffered. Callers run it under one hard read deadline for the whole call, and handler time counts against it: a partly buffered frame started after slow handlers could need a socket read after the deadline passed, time out mid-frame, and retire a healthy connection. Such a frame is left untouched for the next drain. A partly buffered attribute is still read here: the pool's put-path check would parse it without a deadline.
Processor.RegisterHandler
func (p *Processor) RegisterHandler(pushNotificationName string, handler NotificationHandler, protected bool) error
RegisterHandler registers a handler for a specific push notification name
Processor.UnregisterHandler
func (p *Processor) UnregisterHandler(pushNotificationName string) error
UnregisterHandler removes a handler for a specific push notification name
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
NewProcessorError
func NewProcessorError(processorType ProcessorType, operation ProcessorOperation, pushNotificationName, reason string, err error) *ProcessorError
NewProcessorError creates a new ProcessorError
ProcessorError.Error
func (e *ProcessorError) Error() string
ProcessorError.Unwrap
func (e *ProcessorError) Unwrap() error
ProcessorOperation
type ProcessorOperation string
ProcessorOperation represents the operation being performed by the processor defined as a custom type for better readability and easier maintenance
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
Registry
type Registry struct {
// contains filtered or unexported fields
}
Registry manages push notification handlers
NewRegistry
func NewRegistry() *Registry
NewRegistry creates a new push notification registry
Registry.GetHandler
func (r *Registry) GetHandler(pushNotificationName string) NotificationHandler
GetHandler returns the handler for a specific push notification name
Registry.RegisterHandler
func (r *Registry) RegisterHandler(pushNotificationName string, handler NotificationHandler, protected bool) error
RegisterHandler registers a handler for a specific push notification name
Registry.UnregisterHandler
func (r *Registry) UnregisterHandler(pushNotificationName string) error
UnregisterHandler removes a handler for a specific push notification name
VoidProcessor
type VoidProcessor struct{}
VoidProcessor discards all push notifications without processing them
NewVoidProcessor
func NewVoidProcessor() *VoidProcessor
NewVoidProcessor creates a new void push notification processor
VoidProcessor.GetHandler
func (v *VoidProcessor) GetHandler(_ string) NotificationHandler
GetHandler returns nil for void processor since it doesn't maintain handlers
VoidProcessor.ProcessPendingNotifications
func (v *VoidProcessor) ProcessPendingNotifications(ctx 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.
VoidProcessor.RegisterHandler
func (v *VoidProcessor) RegisterHandler(pushNotificationName string, _ NotificationHandler, _ bool) error
RegisterHandler returns an error for void processor since it doesn't maintain handlers
VoidProcessor.UnregisterHandler
func (v *VoidProcessor) UnregisterHandler(pushNotificationName string) error
UnregisterHandler returns an error for void processor since it doesn't maintain handlers