Documentation
¶
Index ¶
- func PublishRouted[T any](c *Client, ctx context.Context, channel, msgType string, data T) error
- func PublishTyped[T any](c *Client, ctx context.Context, channel string, message T) error
- func SendRequest[T any](c *Client, ctx context.Context, queue string, message T) error
- func StreamAdd[T any](sp *StreamPublisher, ctx context.Context, msg T) (string, error)
- type Client
- func (c *Client) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
- func (c *Client) BRPop(ctx context.Context, 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) Decr(ctx context.Context, key string) (int64, error)
- func (c *Client) Del(ctx context.Context, keys ...string) (int64, error)
- func (c *Client) Do(ctx context.Context, cmd string, args ...interface{}) (interface{}, error)
- func (c *Client) Exists(ctx context.Context, keys ...string) (int64, error)
- func (c *Client) Expire(ctx context.Context, key string, expiration time.Duration) (bool, error)
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) HGet(ctx context.Context, key, field string) (string, error)
- func (c *Client) HGetAll(ctx context.Context, key string) (map[string]string, error)
- func (c *Client) HSet(ctx context.Context, key, field string, value interface{}) error
- func (c *Client) Hash(name string) *HashPublisher
- func (c *Client) Incr(ctx context.Context, key string) (int64, error)
- func (c *Client) LPop(ctx context.Context, key string) (string, error)
- func (c *Client) LPush(ctx context.Context, 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) NewTxGroup() *TxGroup
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Publish(ctx context.Context, channel string, message interface{}) (int64, error)
- func (c *Client) RPop(ctx context.Context, key string) (string, error)
- func (c *Client) RPush(ctx context.Context, key string, values ...interface{}) (int64, error)
- func (c *Client) Raw() *redis.Client
- func (c *Client) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) error
- type Codec
- type Envelope
- type FaultSet
- func (fs *FaultSet) Add(ctx context.Context, code int) error
- func (fs *FaultSet) All(ctx context.Context) ([]string, error)
- func (fs *FaultSet) Clear(ctx context.Context) error
- func (fs *FaultSet) Has(ctx context.Context, code int) (bool, error)
- func (fs *FaultSet) Remove(ctx context.Context, code int) error
- func (fs *FaultSet) SetMany(ctx context.Context, codes []int) error
- type HashPublisher
- func (hp *HashPublisher) Channel() string
- func (hp *HashPublisher) Clear(ctx context.Context) error
- func (hp *HashPublisher) Delete(ctx context.Context, field string) error
- func (hp *HashPublisher) Get(ctx context.Context, field string) (string, error)
- func (hp *HashPublisher) GetAll(ctx context.Context) (map[string]string, error)
- func (hp *HashPublisher) Hash() string
- func (hp *HashPublisher) ReplaceAll(ctx context.Context, fields map[string]any) error
- func (hp *HashPublisher) Set(ctx context.Context, field string, value any, opts ...SetOption) error
- func (hp *HashPublisher) SetIfChanged(ctx context.Context, field string, value any) (bool, error)
- func (hp *HashPublisher) SetMany(ctx context.Context, fields map[string]any, opts ...SetOption) error
- func (hp *HashPublisher) SetManyIfChanged(ctx context.Context, fields map[string]any) ([]string, error)
- func (hp *HashPublisher) SetManyPublishOne(ctx context.Context, fields map[string]any, notification string) error
- func (hp *HashPublisher) SetWithTimestamp(ctx context.Context, field string, value any) error
- type HashWatcher
- func (hw *HashWatcher) Fetch(ctx context.Context, field string) (string, error)
- func (hw *HashWatcher) FetchAll(ctx context.Context) (map[string]string, error)
- func (hw *HashWatcher) OnAny(handler func(field, value string) 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(ctx context.Context) 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 TxGroup
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PublishRouted ¶
PublishRouted publishes a message with type routing
func PublishTyped ¶
PublishTyped publishes a typed message using the client's codec
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) BLPop ¶
func (c *Client) BLPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
BLPop blocks until it can remove and return the first element of a list
func (*Client) BRPop ¶
func (c *Client) BRPop(ctx context.Context, timeout time.Duration, keys ...string) ([]string, error)
BRPop blocks until it can remove and return the last element of a list
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
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(ctx context.Context) error
Clear deletes the entire hash and publishes "cleared" notification.
func (*HashPublisher) Delete ¶ added in v0.4.0
func (hp *HashPublisher) Delete(ctx context.Context, field string) error
Delete removes a field from the hash and publishes notification.
func (*HashPublisher) ReplaceAll ¶ added in v0.4.0
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".
func (*HashPublisher) Set ¶
Set atomically sets a hash field and publishes the field name. By default, always publishes. Use NoPublish() to skip publishing.
func (*HashPublisher) SetIfChanged ¶
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(ctx context.Context, fields map[string]any, opts ...SetOption) error
SetMany atomically sets multiple fields and publishes each one. By default, publishes each field. Use NoPublish() to skip publishing.
func (*HashPublisher) SetManyIfChanged ¶
func (hp *HashPublisher) SetManyIfChanged(ctx context.Context, 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(ctx context.Context, fields map[string]any, notification string) 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.
func (*HashPublisher) SetWithTimestamp ¶
SetWithTimestamp atomically sets a field, its timestamp, and publishes.
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) FetchAll ¶
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) 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(ctx context.Context) 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(ctx context.Context, 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 ¶
Add publishes a message to the stream with auto-generated ID. Returns the generated message ID.
func (*StreamPublisher) AddWithID ¶
func (sp *StreamPublisher) AddWithID(ctx context.Context, id string, values map[string]any) (string, error)
AddWithID publishes a message with a specific 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