Documentation
¶
Index ¶
- Variables
- func PublishRouted[T any](c *Client, channel, msgType string, data T) error
- func PublishTyped[T any](c *Client, channel string, message T, opts ...SetOption) error
- func SendRequest[T any](c *Client, queue string, message T) error
- func StreamAdd[T any](sp *StreamPublisher, msg T) (string, error)
- type Client
- func (c *Client) BLPop(timeout time.Duration, keys ...string) ([]string, error)
- func (c *Client) BRPop(timeout time.Duration, keys ...string) ([]string, error)
- func (c *Client) Close() error
- func (c *Client) CloseWithTimeout(timeout time.Duration) error
- func (c *Client) Codec() Codec
- func (c *Client) Connected() bool
- func (c *Client) Context() context.Context
- func (c *Client) Decr(key string) (int64, error)
- func (c *Client) Del(keys ...string) (int64, error)
- func (c *Client) Do(cmd string, args ...interface{}) (interface{}, error)
- func (c *Client) Exists(keys ...string) (int64, error)
- func (c *Client) Expire(key string, expiration time.Duration) (bool, error)
- func (c *Client) Get(key string) (string, error)
- func (c *Client) HGet(key, field string) (string, error)
- func (c *Client) HGetAll(key string) (map[string]string, error)
- func (c *Client) HSet(key, field string, value interface{}) error
- func (c *Client) Hash(name string) *HashPublisher
- func (c *Client) Incr(key string) (int64, error)
- func (c *Client) LPop(key string) (string, error)
- func (c *Client) LPush(key string, values ...interface{}) (int64, error)
- func (c *Client) Logger() *slog.Logger
- func (c *Client) NewFaultSet(setKey, channel, payload string) *FaultSet
- func (c *Client) NewHashPublisher(hash string) *HashPublisher
- func (c *Client) NewHashPublisherWithChannel(hash, channel string) *HashPublisher
- func (c *Client) NewHashWatcher(hash string) *HashWatcher
- func (c *Client) NewHashWatcherWithChannel(hash, channel string) *HashWatcher
- func (c *Client) NewRouter(channel string) *Router
- func (c *Client) NewStreamConsumer(stream string, opts ...StreamConsumerOption) *StreamConsumer
- func (c *Client) NewStreamPublisher(stream string, opts ...StreamPublisherOption) *StreamPublisher
- func (c *Client) NewTx() *Tx
- func (c *Client) NewTxGroup() *TxGroup
- func (c *Client) NewTxWithContext(ctx context.Context) *Tx
- func (c *Client) Ping() error
- func (c *Client) Publish(channel string, message interface{}, opts ...SetOption) (int64, error)
- func (c *Client) RPop(key string) (string, error)
- func (c *Client) RPush(key string, values ...interface{}) (int64, error)
- func (c *Client) Raw() *redis.Client
- func (c *Client) Set(key string, value interface{}, expiration time.Duration) error
- func (c *Client) WithContext(ctx context.Context) *ContextClient
- type Codec
- type ContextClient
- type Envelope
- type FaultSet
- type HashPublisher
- func (hp *HashPublisher) Channel() string
- func (hp *HashPublisher) Clear(opts ...SetOption) error
- func (hp *HashPublisher) Delete(field string, opts ...SetOption) error
- func (hp *HashPublisher) Get(field string) (string, error)
- func (hp *HashPublisher) GetAll() (map[string]string, error)
- func (hp *HashPublisher) Hash() string
- func (hp *HashPublisher) ReplaceAll(fields map[string]any, opts ...SetOption) error
- func (hp *HashPublisher) Set(field string, value any, opts ...SetOption) error
- func (hp *HashPublisher) SetIfChanged(field string, value any) (bool, error)
- func (hp *HashPublisher) SetMany(fields map[string]any, opts ...SetOption) error
- func (hp *HashPublisher) SetManyIfChanged(fields map[string]any) ([]string, error)
- func (hp *HashPublisher) SetManyPublishOne(fields map[string]any, notification string, opts ...SetOption) error
- func (hp *HashPublisher) SetWithTimestamp(field string, value any, opts ...SetOption) error
- type HashWatcher
- func (hw *HashWatcher) Fetch(field string) (string, error)
- func (hw *HashWatcher) FetchAll() (map[string]string, error)
- func (hw *HashWatcher) OnAny(handler func(field, value string) error) *HashWatcher
- func (hw *HashWatcher) OnEvent(event string, handler func() error) *HashWatcher
- func (hw *HashWatcher) OnField(field string, handler func(value string) error) *HashWatcher
- func (hw *HashWatcher) SetDebounce(d time.Duration) *HashWatcher
- func (hw *HashWatcher) Start() error
- func (hw *HashWatcher) StartWithSync() error
- func (hw *HashWatcher) Stop() error
- type JSONCodec
- type Option
- func WithAddress(addr string) Option
- func WithCodec(codec Codec) Option
- func WithDialTimeout(d time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMaxRetries(n int) Option
- func WithOnConnect(fn func()) Option
- func WithOnDisconnect(fn func(error)) Option
- func WithPoolSize(n int) Option
- func WithPort(port int) Option
- func WithRetryInterval(d time.Duration) Option
- func WithURL(rawURL string) Option
- type QueueHandler
- type Router
- type SetOption
- type StreamConsumer
- type StreamConsumerOption
- type StreamPublisher
- type StreamPublisherOption
- type StringCodec
- type Subscription
- type Tx
- func (tx *Tx) Exec() error
- func (tx *Tx) FaultAdd(fs *FaultSet, code int)
- func (tx *Tx) FaultRemove(fs *FaultSet, code int)
- func (tx *Tx) FaultUpdate(fs *FaultSet, adds, removes []int)
- func (tx *Tx) HashSetManyIfChanged(hp *HashPublisher, fields map[string]any) []string
- func (tx *Tx) StreamAdd(sp *StreamPublisher, values map[string]any)
- type TxGroup
Constants ¶
This section is empty.
Variables ¶
var ErrNil = redis.Nil
ErrNil is returned when a key or field does not exist in Redis.
Functions ¶
func PublishRouted ¶
PublishRouted publishes a message with type routing
func PublishTyped ¶
PublishTyped publishes a typed message using the client's codec. By default async; use Sync() for synchronous operation.
func SendRequest ¶
SendRequest sends a typed message to a queue
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the main Redis IPC client
func (*Client) CloseWithTimeout ¶
CloseWithTimeout shuts down the client with a maximum wait time
func (*Client) Hash ¶ added in v0.6.0
func (c *Client) Hash(name string) *HashPublisher
Hash returns a cached HashPublisher for the given hash name. Publishers are created on first access and reused thereafter. This is more ergonomic than NewHashPublisher when you don't need to store the publisher in a struct field.
func (*Client) NewFaultSet ¶
NewFaultSet creates a fault set manager. Example: NewFaultSet("battery:0:fault", "battery:0", "fault")
func (*Client) NewHashPublisher ¶
func (c *Client) NewHashPublisher(hash string) *HashPublisher
NewHashPublisher creates a publisher for a hash. By convention, the channel name equals the hash name.
func (*Client) NewHashPublisherWithChannel ¶
func (c *Client) NewHashPublisherWithChannel(hash, channel string) *HashPublisher
NewHashPublisherWithChannel creates a publisher with a custom channel name.
func (*Client) NewHashWatcher ¶
func (c *Client) NewHashWatcher(hash string) *HashWatcher
NewHashWatcher creates a watcher for a hash. By convention, the channel name equals the hash name.
func (*Client) NewHashWatcherWithChannel ¶
func (c *Client) NewHashWatcherWithChannel(hash, channel string) *HashWatcher
NewHashWatcherWithChannel creates a watcher with a custom channel name.
func (*Client) NewStreamConsumer ¶
func (c *Client) NewStreamConsumer(stream string, opts ...StreamConsumerOption) *StreamConsumer
NewStreamConsumer creates a consumer for a Redis stream.
func (*Client) NewStreamPublisher ¶
func (c *Client) NewStreamPublisher(stream string, opts ...StreamPublisherOption) *StreamPublisher
NewStreamPublisher creates a publisher for a Redis stream.
func (*Client) NewTxGroup ¶
NewTxGroup creates a new transaction group
func (*Client) NewTxWithContext ¶ added in v0.11.0
NewTxWithContext creates a new transaction with a specific context.
func (*Client) Publish ¶
Publish publishes a message to a channel. By default, this is async (fire-and-forget) for performance. Use Sync() option when you need to confirm the publish succeeded or need the subscriber count.
func (*Client) WithContext ¶ added in v0.7.0
func (c *Client) WithContext(ctx context.Context) *ContextClient
WithContext returns a context-scoped client wrapper. Use this when you need a custom context (e.g., with timeout) for operations.
type ContextClient ¶ added in v0.7.0
type ContextClient struct {
*Client
// contains filtered or unexported fields
}
ContextClient wraps a Client with a specific context for operations.
func (*ContextClient) Context ¶ added in v0.7.0
func (cc *ContextClient) Context() context.Context
Context returns the custom context for this wrapper.
type Envelope ¶
type Envelope struct {
Type string `json:"type"`
Data json.RawMessage `json:"data"`
}
Envelope is the standard message format for routed messages
type FaultSet ¶
type FaultSet struct {
// contains filtered or unexported fields
}
FaultSet manages a Redis set of fault codes with pub/sub notification.
type HashPublisher ¶
type HashPublisher struct {
// contains filtered or unexported fields
}
HashPublisher provides atomic HSET + PUBLISH with change detection. This matches the LibreScoot pattern where: - State is stored in a Redis hash - Changes are notified via pub/sub with the field name as payload - Consumers fetch the value using HGET
func (*HashPublisher) Channel ¶
func (hp *HashPublisher) Channel() string
Channel returns the channel name.
func (*HashPublisher) Clear ¶ added in v0.4.0
func (hp *HashPublisher) Clear(opts ...SetOption) error
Clear deletes the entire hash and publishes "cleared" notification. By default, this is async (fire-and-forget). Use Sync() for confirmation.
func (*HashPublisher) Delete ¶ added in v0.4.0
func (hp *HashPublisher) Delete(field string, opts ...SetOption) error
Delete removes a field from the hash and publishes notification. By default, this is async (fire-and-forget). Use Sync() for confirmation.
func (*HashPublisher) Get ¶
func (hp *HashPublisher) Get(field string) (string, error)
Get retrieves a field value from the hash.
func (*HashPublisher) GetAll ¶
func (hp *HashPublisher) GetAll() (map[string]string, error)
GetAll retrieves all fields from the hash.
func (*HashPublisher) ReplaceAll ¶ added in v0.4.0
func (hp *HashPublisher) ReplaceAll(fields map[string]any, opts ...SetOption) error
ReplaceAll atomically deletes all fields and sets new ones. This solves the race condition in DEL + HMSET + PUBLISH patterns. If fields is nil or empty, just deletes and publishes "cleared". By default, this is async (fire-and-forget). Use Sync() for confirmation.
func (*HashPublisher) Set ¶
func (hp *HashPublisher) Set(field string, value any, opts ...SetOption) error
Set atomically sets a hash field and publishes the field name. By default, this is async (fire-and-forget) for performance. Use Sync() option to wait for Redis confirmation. Use NoPublish() to skip publishing.
func (*HashPublisher) SetIfChanged ¶
func (hp *HashPublisher) SetIfChanged(field string, value any) (bool, error)
SetIfChanged atomically sets a hash field and publishes only if the value changed. Returns true if the value was changed and published.
func (*HashPublisher) SetMany ¶
func (hp *HashPublisher) SetMany(fields map[string]any, opts ...SetOption) error
SetMany atomically sets multiple fields and publishes each one. By default, this is async (fire-and-forget) for performance. Use Sync() option to wait for Redis confirmation. Use NoPublish() to skip publishing.
func (*HashPublisher) SetManyIfChanged ¶
func (hp *HashPublisher) SetManyIfChanged(fields map[string]any) ([]string, error)
SetManyIfChanged atomically sets multiple fields, publishing only changed ones. Returns the list of fields that were actually changed.
func (*HashPublisher) SetManyPublishOne ¶ added in v0.5.0
func (hp *HashPublisher) SetManyPublishOne(fields map[string]any, notification string, opts ...SetOption) error
SetManyPublishOne sets multiple fields but publishes only a single notification. Useful for batch updates where you only want one notification at the end. The notification parameter is published as the message payload. By default, this is async (fire-and-forget). Use Sync() for confirmation.
func (*HashPublisher) SetWithTimestamp ¶
func (hp *HashPublisher) SetWithTimestamp(field string, value any, opts ...SetOption) error
SetWithTimestamp atomically sets a field, its timestamp, and publishes. By default, this is async (fire-and-forget). Use Sync() for confirmation.
type HashWatcher ¶
type HashWatcher struct {
// contains filtered or unexported fields
}
HashWatcher subscribes to a channel and fetches hash values on notification. This matches the LibreScoot consumer pattern.
func OnFieldTyped ¶
func OnFieldTyped[T any](hw *HashWatcher, field string, handler func(T) error) *HashWatcher
OnFieldTyped registers a typed handler for a specific field. The value is JSON-decoded into the type T.
func (*HashWatcher) Fetch ¶
func (hw *HashWatcher) Fetch(field string) (string, error)
Fetch retrieves a specific field from the hash.
func (*HashWatcher) FetchAll ¶
func (hw *HashWatcher) FetchAll() (map[string]string, error)
FetchAll retrieves all current values from the hash. Useful for initial state sync.
func (*HashWatcher) OnAny ¶
func (hw *HashWatcher) OnAny(handler func(field, value string) error) *HashWatcher
OnAny registers a catch-all handler for any field not explicitly handled.
func (*HashWatcher) OnEvent ¶ added in v0.8.0
func (hw *HashWatcher) OnEvent(event string, handler func() error) *HashWatcher
OnEvent registers a handler for an event-only message. Unlike OnField, this does not fetch from the hash - it fires immediately when the message is received. Use for notifications that don't have corresponding hash state.
func (*HashWatcher) OnField ¶
func (hw *HashWatcher) OnField(field string, handler func(value string) error) *HashWatcher
OnField registers a handler for a specific field. When a message is received with this field name, the handler is called with the fetched value from the hash.
func (*HashWatcher) SetDebounce ¶
func (hw *HashWatcher) SetDebounce(d time.Duration) *HashWatcher
SetDebounce sets a debounce duration for handler calls. When set, rapid updates to the same field are coalesced - only the last value is passed to the handler after the quiet period expires.
func (*HashWatcher) Start ¶
func (hw *HashWatcher) Start() error
Start begins listening for messages and fetching values.
func (*HashWatcher) StartWithSync ¶
func (hw *HashWatcher) StartWithSync() error
StartWithSync subscribes, fetches current state, calls handlers, then processes messages. This ensures no messages are missed between initial fetch and subscribe. Order: Subscribe (messages buffer) → HGETALL → call handlers → process buffered messages
type Option ¶
type Option func(*options)
Option configures the client
func WithDialTimeout ¶
WithDialTimeout sets the connection timeout
func WithMaxRetries ¶
WithMaxRetries sets the maximum number of retries for commands
func WithOnConnect ¶
func WithOnConnect(fn func()) Option
WithOnConnect sets a callback invoked when connection is established
func WithOnDisconnect ¶
WithOnDisconnect sets a callback invoked when connection is lost
func WithRetryInterval ¶
WithRetryInterval sets the connection monitoring interval
type QueueHandler ¶
type QueueHandler[T any] struct { // contains filtered or unexported fields }
QueueHandler processes messages from a Redis list
func HandleRequests ¶
func HandleRequests[T any](c *Client, queue string, handler func(T) error) *QueueHandler[T]
HandleRequests creates a typed queue handler
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router provides message-type based routing for a channel
type SetOption ¶ added in v0.5.0
type SetOption func(*setConfig)
SetOption configures Set/SetMany behavior
type StreamConsumer ¶
type StreamConsumer struct {
// contains filtered or unexported fields
}
StreamConsumer consumes messages from a Redis stream using XREAD.
func StreamHandle ¶
func StreamHandle[T any](sc *StreamConsumer, handler func(id string, msg T) error) *StreamConsumer
StreamHandle registers a typed handler for stream messages.
func (*StreamConsumer) Handle ¶
func (sc *StreamConsumer) Handle(handler func(id string, values map[string]string) error) *StreamConsumer
Handle sets the message handler function.
func (*StreamConsumer) Running ¶
func (sc *StreamConsumer) Running() bool
Running returns whether the consumer is currently running.
func (*StreamConsumer) Start ¶
func (sc *StreamConsumer) Start(startID string) error
Start begins consuming messages from the stream. startID can be:
- "$" to read only new messages
- "0" to read from the beginning
- A specific message ID to read after that ID
- ">" for consumer groups (only undelivered messages)
func (*StreamConsumer) Stop ¶
func (sc *StreamConsumer) Stop()
Stop stops the consumer (by cancelling the context passed to Start). Note: The consumer stops when the context passed to Start is cancelled.
type StreamConsumerOption ¶
type StreamConsumerOption func(*streamConsumerOptions)
StreamConsumerOption configures a StreamConsumer
func WithBlockTimeout ¶
func WithBlockTimeout(d time.Duration) StreamConsumerOption
WithBlockTimeout sets the XREAD block timeout
func WithConsumerGroup ¶
func WithConsumerGroup(group, consumer string) StreamConsumerOption
WithConsumerGroup configures consumer group mode (XREADGROUP)
type StreamPublisher ¶
type StreamPublisher struct {
// contains filtered or unexported fields
}
StreamPublisher publishes messages to a Redis stream using XADD.
func (*StreamPublisher) Add ¶
func (sp *StreamPublisher) Add(values map[string]any) (string, error)
Add publishes a message to the stream with auto-generated ID. Returns the generated message ID.
func (*StreamPublisher) Stream ¶
func (sp *StreamPublisher) Stream() string
Stream returns the stream name.
type StreamPublisherOption ¶
type StreamPublisherOption func(*streamPublisherOptions)
StreamPublisherOption configures a StreamPublisher
func WithMaxLen ¶
func WithMaxLen(n int64) StreamPublisherOption
WithMaxLen sets the maximum stream length (MAXLEN option)
type Subscription ¶
type Subscription[T any] struct { // contains filtered or unexported fields }
Subscription is a typed channel subscription
func (*Subscription[T]) Unsubscribe ¶
func (s *Subscription[T]) Unsubscribe() error
Unsubscribe stops the subscription
type Tx ¶ added in v0.11.0
type Tx struct {
// contains filtered or unexported fields
}
Tx groups operations from multiple redis-ipc types into one atomic pipeline (MULTI/EXEC). Cache updates are deferred and only applied after a successful Exec.
func (*Tx) Exec ¶ added in v0.11.0
Exec executes all queued operations atomically via MULTI/EXEC. On success, applies deferred cache updates. On failure, the cache is unchanged.
func (*Tx) FaultRemove ¶ added in v0.11.0
FaultRemove queues SREM and PUBLISH for a fault code.
func (*Tx) FaultUpdate ¶ added in v0.11.0
FaultUpdate queues a batch of adds and removes with a single PUBLISH.
func (*Tx) HashSetManyIfChanged ¶ added in v0.11.0
func (tx *Tx) HashSetManyIfChanged(hp *HashPublisher, fields map[string]any) []string
HashSetManyIfChanged queues HSET for all fields and PUBLISH for each changed field. Returns the list of fields that differ from the cached state. The cache is only updated if Exec succeeds.