Documentation
¶
Index ¶
- Constants
- type ChannelIncrbyRequest
- type ChannelType
- type DualBroker
- func (d *DualBroker) BatchIncrby(ctx context.Context, reqs []ChannelIncrbyRequest) (map[string]centrifuge.StreamPosition, error)
- func (d *DualBroker) Close(ctx context.Context) error
- func (d *DualBroker) History(ch string, opts centrifuge.HistoryOptions) (pubs []*centrifuge.Publication, sp centrifuge.StreamPosition, err error)
- func (d *DualBroker) IncrConversationOffset(ctx context.Context, conversationID string) (uint32, error)
- func (d *DualBroker) Publish(ch string, data []byte, opts centrifuge.PublishOptions) (centrifuge.StreamPosition, bool, error)
- func (d *DualBroker) PublishEphemeral(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions) error
- func (d *DualBroker) PublishJoin(ch string, info *centrifuge.ClientInfo) error
- func (d *DualBroker) PublishLeave(ch string, info *centrifuge.ClientInfo) error
- func (d *DualBroker) PublishWithContext(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions) (sp centrifuge.StreamPosition, fromCache bool, err error)
- func (d *DualBroker) PublishWithOffset(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions, ...) error
- func (d *DualBroker) RegisterBrokerEventHandler(handler centrifuge.BrokerEventHandler) error
- func (d *DualBroker) RegisterChannelType(ch string, ct ChannelType)
- func (d *DualBroker) RemoveHistory(ch string) error
- func (d *DualBroker) SetConversationOffset(ctx context.Context, conversationID string, value uint32) error
- func (d *DualBroker) Subscribe(ch string) error
- func (d *DualBroker) TopicBroker() *TopicBroker
- func (d *DualBroker) Unsubscribe(ch string) error
- type DualBrokerConfig
- type HistoryStore
- type HistoryStoreRemover
- type Logger
- type TopicBroker
- func (b *TopicBroker) BatchIncrby(ctx context.Context, reqs []ChannelIncrbyRequest) (map[string]centrifuge.StreamPosition, error)
- func (b *TopicBroker) Close(_ context.Context) error
- func (b *TopicBroker) History(ch string, opts centrifuge.HistoryOptions) (pubs []*centrifuge.Publication, sp centrifuge.StreamPosition, err error)
- func (b *TopicBroker) IncrConversationOffset(ctx context.Context, conversationID string) (uint32, error)
- func (b *TopicBroker) Publish(ch string, data []byte, opts centrifuge.PublishOptions) (centrifuge.StreamPosition, bool, error)
- func (b *TopicBroker) PublishJoin(ch string, info *centrifuge.ClientInfo) error
- func (b *TopicBroker) PublishLeave(ch string, info *centrifuge.ClientInfo) error
- func (b *TopicBroker) PublishWithContext(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions) (sp centrifuge.StreamPosition, fromCache bool, err error)
- func (b *TopicBroker) PublishWithOffset(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions, ...) error
- func (b *TopicBroker) RegisterBrokerEventHandler(handler centrifuge.BrokerEventHandler) error
- func (b *TopicBroker) RemoveHistory(ch string) error
- func (b *TopicBroker) SetConversationOffset(ctx context.Context, conversationID string, value uint32) error
- func (b *TopicBroker) Subscribe(ch string) error
- func (b *TopicBroker) Unsubscribe(ch string) error
- type TopicBrokerConfig
- type TracingConfig
Constants ¶
const ( AttributeChannel = attribute.Key("centrifugeplus.channel") AttributeChannelType = attribute.Key("centrifugeplus.channel_type") AttributeOffset = attribute.Key("centrifugeplus.offset") AttributeEpoch = attribute.Key("centrifugeplus.epoch") AttributeTaskID = attribute.Key("centrifugeplus.task_id") AttributeEnqueued = attribute.Key("centrifugeplus.enqueued") AttributeFromCache = attribute.Key("centrifugeplus.from_cache") AttributeQueue = attribute.Key("centrifugeplus.queue") AttributeMessageType = attribute.Key("centrifugeplus.message_type") AttributeStreamMinOffset = attribute.Key("centrifugeplus.stream_min_offset") )
Span attribute keys for centrifuge-plus tracing.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChannelIncrbyRequest ¶
ChannelIncrbyRequest represents a channel to pre-allocate offsets for. Count specifies how many consecutive offsets to reserve (default 1 if zero). The returned StreamPosition for the channel carries the HIGHEST allocated offset; callers derive the full range as [final - Count + 1, ..., final]. Each channel must appear at most once per BatchIncrby call.
type ChannelType ¶
type ChannelType int
ChannelType defines the type of a channel.
const ( // Live channels are not persisted, messages are only delivered to online subscribers. // Uses centrifuge's built-in RedisBroker. Live ChannelType = iota // Topic channels are persisted and support real-time push. // Messages are stored in history stream and also pushed to asynq for offline recovery. Topic )
func (ChannelType) String ¶
func (ct ChannelType) String() string
String returns the string representation of the ChannelType.
type DualBroker ¶
type DualBroker struct {
// contains filtered or unexported fields
}
DualBroker implements centrifuge.Broker interface. It routes messages to either RedisBroker (Live) or TopicBroker (Topic) based on channel type.
func NewDualBroker ¶
func NewDualBroker(node *centrifuge.Node, config DualBrokerConfig) (*DualBroker, error)
NewDualBroker creates a new DualBroker instance. The node parameter is required for RedisBroker initialization.
func (*DualBroker) BatchIncrby ¶
func (d *DualBroker) BatchIncrby(ctx context.Context, reqs []ChannelIncrbyRequest) (map[string]centrifuge.StreamPosition, error)
BatchIncrby batch pre-allocates offsets for multiple channels. Routes to TopicBroker.
func (*DualBroker) Close ¶
func (d *DualBroker) Close(ctx context.Context) error
Close closes both brokers, aggregating any errors.
func (*DualBroker) History ¶
func (d *DualBroker) History(ch string, opts centrifuge.HistoryOptions) (pubs []*centrifuge.Publication, sp centrifuge.StreamPosition, err error)
History returns publications for channel.
func (*DualBroker) IncrConversationOffset ¶
func (d *DualBroker) IncrConversationOffset(ctx context.Context, conversationID string) (uint32, error)
IncrConversationOffset 为指定会话分配下一个 conversation offset
func (*DualBroker) Publish ¶
func (d *DualBroker) Publish(ch string, data []byte, opts centrifuge.PublishOptions) (centrifuge.StreamPosition, bool, error)
Publish publishes data to channel.
func (*DualBroker) PublishEphemeral ¶
func (d *DualBroker) PublishEphemeral(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions) error
PublishEphemeral 强制走 liveBroker,纯 PUB/SUB,不持久化到 stream。 用于 typing 等瞬态事件:丢失可接受,不需要 offset 和 recovery。
func (*DualBroker) PublishJoin ¶
func (d *DualBroker) PublishJoin(ch string, info *centrifuge.ClientInfo) error
PublishJoin publishes Join message to channel.
func (*DualBroker) PublishLeave ¶
func (d *DualBroker) PublishLeave(ch string, info *centrifuge.ClientInfo) error
PublishLeave publishes Leave message to channel.
func (*DualBroker) PublishWithContext ¶
func (d *DualBroker) PublishWithContext(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions) (sp centrifuge.StreamPosition, fromCache bool, err error)
PublishWithContext is like Publish but accepts a context for distributed tracing.
func (*DualBroker) PublishWithOffset ¶
func (d *DualBroker) PublishWithOffset(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions, sp centrifuge.StreamPosition) error
PublishWithOffset publishes a message using a pre-allocated offset. Routes to TopicBroker.
func (*DualBroker) RegisterBrokerEventHandler ¶
func (d *DualBroker) RegisterBrokerEventHandler(handler centrifuge.BrokerEventHandler) error
RegisterBrokerEventHandler is called once when Broker is set to Node.
func (*DualBroker) RegisterChannelType ¶
func (d *DualBroker) RegisterChannelType(ch string, ct ChannelType)
RegisterChannelType registers the channel type for a given channel.
func (*DualBroker) RemoveHistory ¶
func (d *DualBroker) RemoveHistory(ch string) error
RemoveHistory removes history from channel.
func (*DualBroker) SetConversationOffset ¶
func (d *DualBroker) SetConversationOffset(ctx context.Context, conversationID string, value uint32) error
SetConversationOffset 设置指定会话的 offset 值,用于 fork 等场景初始化计数器
func (*DualBroker) Subscribe ¶
func (d *DualBroker) Subscribe(ch string) error
Subscribe subscribes node to channel. 注意:接口签名不含 context 参数(与 centrifuge.NodeBroker 保持一致), 此处 context.Background() 仅用于 tracing span 创建,不影响业务逻辑。
func (*DualBroker) TopicBroker ¶
func (d *DualBroker) TopicBroker() *TopicBroker
TopicBroker returns the underlying TopicBroker used for Topic mode channels.
func (*DualBroker) Unsubscribe ¶
func (d *DualBroker) Unsubscribe(ch string) error
Unsubscribe unsubscribes node from channel. 注意:接口签名不含 context 参数(与 centrifuge.NodeBroker 保持一致), 此处 context.Background() 仅用于 tracing span 创建,不影响业务逻辑。
type DualBrokerConfig ¶
type DualBrokerConfig struct {
// Live is the configuration for Live mode channels (using RedisBroker).
Live centrifuge.RedisBrokerConfig
// Topic is the configuration for Topic mode channels (using TopicBroker).
Topic TopicBrokerConfig
}
DualBrokerConfig is the configuration for DualBroker.
type HistoryStore ¶
type HistoryStore interface {
// Query retrieves publications from the given channel since the specified offset.
// latestOffset 是当前 stream 的最新 offset,用于 fillGapPublications 补齐尾部缺口,
// 保证恢复结果满足 centrifuge 的连续性检查(末条 pub.Offset == latestOffset)。
Query(ctx context.Context, channel string, sinceOffset uint32, latestOffset uint32) ([]*centrifuge.Publication, error)
}
HistoryStore is the interface for querying message history. Implementations are provided by the integrator.
type HistoryStoreRemover ¶
type HistoryStoreRemover interface {
// RemoveHistory removes all history for the given channel.
RemoveHistory(channel string) error
}
HistoryStoreRemover is an optional interface that HistoryStore implementations can implement to support removing history for a channel.
type Logger ¶
type Logger interface {
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
Logger defines the logging interface used by AsynqBroker.
type TopicBroker ¶
type TopicBroker struct {
// contains filtered or unexported fields
}
TopicBroker implements centrifuge.Broker interface for Topic mode channels. It uses a "persist first, then push" model: BatchIncrby → DB transaction → PublishWithOffset.
func NewTopicBroker ¶
func NewTopicBroker(config TopicBrokerConfig) (*TopicBroker, error)
NewTopicBroker creates a new TopicBroker instance.
func (*TopicBroker) BatchIncrby ¶
func (b *TopicBroker) BatchIncrby(ctx context.Context, reqs []ChannelIncrbyRequest) (map[string]centrifuge.StreamPosition, error)
BatchIncrby batch pre-allocates offsets for multiple channels. Call this before DB transaction. Returns map[channel]StreamPosition where the offset is the HIGHEST allocated offset for that channel; the full reserved range is [final - Count + 1, ..., final] (Count defaults to 1 if zero). Each channel must appear at most once in reqs.
func (*TopicBroker) Close ¶
func (b *TopicBroker) Close(_ context.Context) error
Close closes the broker and releases resources.
func (*TopicBroker) History ¶
func (b *TopicBroker) History(ch string, opts centrifuge.HistoryOptions) (pubs []*centrifuge.Publication, sp centrifuge.StreamPosition, err error)
History returns publications for channel from HistoryStore. StreamPosition is read from Redis meta key.
func (*TopicBroker) IncrConversationOffset ¶
func (b *TopicBroker) IncrConversationOffset(ctx context.Context, conversationID string) (uint32, error)
IncrConversationOffset 为指定会话分配下一个 offset(Redis INCR) key: {prefix}:conv:offset:{conversationID}
func (*TopicBroker) Publish ¶
func (b *TopicBroker) Publish(ch string, data []byte, opts centrifuge.PublishOptions) (centrifuge.StreamPosition, bool, error)
Publish is a convenience method that internally calls BatchIncrby + PublishWithOffset. For IM scenarios, use BatchIncrby → DB transaction → PublishWithOffset instead.
func (*TopicBroker) PublishJoin ¶
func (b *TopicBroker) PublishJoin(ch string, info *centrifuge.ClientInfo) error
PublishJoin publishes Join message to channel.
func (*TopicBroker) PublishLeave ¶
func (b *TopicBroker) PublishLeave(ch string, info *centrifuge.ClientInfo) error
PublishLeave publishes Leave message to channel.
func (*TopicBroker) PublishWithContext ¶
func (b *TopicBroker) PublishWithContext(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions) (sp centrifuge.StreamPosition, fromCache bool, err error)
PublishWithContext is like Publish but accepts a context for distributed tracing.
func (*TopicBroker) PublishWithOffset ¶
func (b *TopicBroker) PublishWithOffset(ctx context.Context, ch string, data []byte, opts centrifuge.PublishOptions, sp centrifuge.StreamPosition) error
PublishWithOffset publishes a message using a pre-allocated offset. Call this after DB transaction commits. No HINCRBY, no XADD, no Asynq. Returns error if epoch mismatch occurs.
func (*TopicBroker) RegisterBrokerEventHandler ¶
func (b *TopicBroker) RegisterBrokerEventHandler(handler centrifuge.BrokerEventHandler) error
RegisterBrokerEventHandler is called once when Broker is set to Node.
func (*TopicBroker) RemoveHistory ¶
func (b *TopicBroker) RemoveHistory(ch string) error
RemoveHistory removes history from channel.
func (*TopicBroker) SetConversationOffset ¶
func (b *TopicBroker) SetConversationOffset(ctx context.Context, conversationID string, value uint32) error
SetConversationOffset 设置指定会话的 offset 值(Redis SET),用于 fork 等场景初始化计数器
func (*TopicBroker) Subscribe ¶
func (b *TopicBroker) Subscribe(ch string) error
Subscribe subscribes node to channel.
func (*TopicBroker) Unsubscribe ¶
func (b *TopicBroker) Unsubscribe(ch string) error
Unsubscribe unsubscribes node from channel.
type TopicBrokerConfig ¶
type TopicBrokerConfig struct {
// Prefix is the Redis key prefix.
Prefix string
// RedisAddr is the Redis server address (host:port).
RedisAddr string
// RedisPassword is the Redis password (optional).
RedisPassword string
// RedisDB is the Redis database number.
RedisDB int
// HistoryStore is the implementation for querying message history.
HistoryStore HistoryStore
// HistoryMetaTTL is the TTL for the meta key (epoch and top_offset).
// If 0, no TTL is set.
HistoryMetaTTL time.Duration
// Logger is the logger used by the broker.
// If nil, a default logger backed by log.Printf is used.
Logger Logger
// Tracing configures distributed tracing.
// When disabled, all tracing operations are no-ops with zero overhead.
Tracing TracingConfig
}
TopicBrokerConfig is the configuration for TopicBroker.
type TracingConfig ¶
type TracingConfig struct {
// Enabled controls whether tracing is active.
Enabled bool
// Provider is the OpenTelemetry TracerProvider.
// If nil and Enabled is true, a no-op fallback is used.
Provider trace.TracerProvider
}
TracingConfig configures distributed tracing for centrifuge-plus. When disabled, all tracing operations use a no-op tracer with zero overhead.