Documentation
¶
Overview ¶
Package mq 提供消息队列支持,用于 enhance 框架。
该模块提供统一的消息队列抽象接口,支持多种消息中间件集成。 包含消息发送、消费、消息转换等消息中间件功能。
架构设计 ¶
- Queue: 消息队列接口,定义统一的消息操作
- Message: 消息对象,包含消息头、消息体等
- MessageHandler: 消息处理器函数类型
- QueueOption: 队列配置选项函数
核心功能 ¶
- 消息发送: 支持同步和异步消息发送
- 消息消费: 支持消息监听和自动消费
- 消息确认: 支持消息确认(Ack/Nack)和重试机制
- 死信队列: 支持死信队列处理失败消息
- 对象池: 使用 sync.Pool 复用 Message 对象,减少 GC 压力
使用方式 ¶
创建消息队列:
queue := mq.NewInMemoryQueue("my-queue")
发送消息:
msg := mq.AcquireMessage()
msg.Body = []byte(`{"userId": 123}`)
err := queue.Send(msg)
消费消息:
queue.Consume(func(msg *mq.Message) error {
// 处理消息
msg.Ack()
return nil
})
集成后端 ¶
具体实现位于 starter 子包:
- starter/kafka: Apache Kafka 集成
- starter/rabbitmq: RabbitMQ 集成
Package mq 提供消息队列支持,用于 enhance 框架。
Index ¶
- Constants
- func ReleaseMessage(msg *Message)
- type BaseQueue
- type InMemoryQueue
- func (q *InMemoryQueue) Close() error
- func (q *InMemoryQueue) Consume(handler MessageHandler) error
- func (q *InMemoryQueue) Purge() error
- func (q *InMemoryQueue) Receive() (*Message, error)
- func (q *InMemoryQueue) ReceiveWithTimeout(timeout time.Duration) (*Message, error)
- func (q *InMemoryQueue) Send(msg *Message) error
- func (q *InMemoryQueue) Size() int
- func (q *InMemoryQueue) StopConsuming()
- type Message
- type MessageConsumer
- type MessageHandler
- type MessagePublisher
- type MessageQueueFactory
- type MessageTemplate
- func (t *MessageTemplate) Purge() error
- func (t *MessageTemplate) Receive() (*Message, error)
- func (t *MessageTemplate) ReceiveWithTimeout(timeout time.Duration) (*Message, error)
- func (t *MessageTemplate) Send(body []byte) error
- func (t *MessageTemplate) SendWithHeaders(body []byte, headers map[string]string) error
- func (t *MessageTemplate) Size() int
- type Queue
- type QueueOption
Constants ¶
const ( // DefaultMaxRetries 默认最大重试次数。 DefaultMaxRetries = 3 // DefaultReceiveTimeout 默认接收超时时间。 DefaultReceiveTimeout = 1 * time.Second )
默认配置常量。
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseQueue ¶
type BaseQueue struct {
// contains filtered or unexported fields
}
BaseQueue 基础队列结构。
type InMemoryQueue ¶
type InMemoryQueue struct {
BaseQueue
// contains filtered or unexported fields
}
InMemoryQueue 内存消息队列实现。
func NewInMemoryQueue ¶
func NewInMemoryQueue(name string, opts ...QueueOption) *InMemoryQueue
NewInMemoryQueue 创建内存消息队列。
func (*InMemoryQueue) Consume ¶
func (q *InMemoryQueue) Consume(handler MessageHandler) error
Consume 实现 Queue 接口
func (*InMemoryQueue) Receive ¶
func (q *InMemoryQueue) Receive() (*Message, error)
Receive 实现 Queue 接口
func (*InMemoryQueue) ReceiveWithTimeout ¶
func (q *InMemoryQueue) ReceiveWithTimeout(timeout time.Duration) (*Message, error)
ReceiveWithTimeout 实现 Queue 接口
func (*InMemoryQueue) StopConsuming ¶
func (q *InMemoryQueue) StopConsuming()
StopConsuming 实现 Queue 接口
type Message ¶
type Message struct {
// ID 消息 ID。
ID string
// Body 消息体。
Body []byte
// Headers 消息头。
Headers map[string]string
// Timestamp 消息时间戳。
Timestamp time.Time
// QueueName 队列名称。
QueueName string
// RetryCount 重试次数。
RetryCount int
// MaxRetries 最大重试次数。
MaxRetries int
// contains filtered or unexported fields
}
Message 消息对象。
表示队列中的一条消息,支持并发安全的 Ack/Nack 操作。 使用 atomic.Int32 保证 acknowledged 状态的线程安全。
type MessageConsumer ¶
type MessageConsumer struct {
// contains filtered or unexported fields
}
MessageConsumer 消息消费者
func NewMessageConsumer ¶
func NewMessageConsumer(queue Queue, handler MessageHandler) *MessageConsumer
NewMessageConsumer 创建消息消费者
type MessagePublisher ¶
type MessagePublisher struct {
// contains filtered or unexported fields
}
MessagePublisher 消息发布者
func NewMessagePublisher ¶
func NewMessagePublisher(queue Queue) *MessagePublisher
NewMessagePublisher 创建消息发布者
func (*MessagePublisher) Publish ¶
func (p *MessagePublisher) Publish(body []byte, headers map[string]string) error
Publish 发布消息
func (*MessagePublisher) PublishJSON ¶
func (p *MessagePublisher) PublishJSON(body []byte) error
PublishJSON 发布 JSON 消息
type MessageQueueFactory ¶
type MessageQueueFactory struct {
// contains filtered or unexported fields
}
MessageQueueFactory 消息队列工厂
使用 sync.Map 优化并发访问,避免全局锁竞争。
func NewMessageQueueFactory ¶
func NewMessageQueueFactory() *MessageQueueFactory
NewMessageQueueFactory 创建消息队列工厂
func (*MessageQueueFactory) CreateInMemoryQueue ¶
func (f *MessageQueueFactory) CreateInMemoryQueue(name string, opts ...QueueOption) Queue
CreateInMemoryQueue 创建内存消息队列
func (*MessageQueueFactory) DeleteQueue ¶
func (f *MessageQueueFactory) DeleteQueue(name string) error
DeleteQueue 删除队列
func (*MessageQueueFactory) GetQueue ¶
func (f *MessageQueueFactory) GetQueue(name string) (Queue, error)
GetQueue 获取队列
func (*MessageQueueFactory) ListQueues ¶
func (f *MessageQueueFactory) ListQueues() []string
ListQueues 列出所有队列
type MessageTemplate ¶
type MessageTemplate struct {
// contains filtered or unexported fields
}
MessageTemplate 消息模板
提供便捷的消息发送和接收方法
func NewMessageTemplate ¶
func NewMessageTemplate(queue Queue) *MessageTemplate
NewMessageTemplate 创建消息模板
func (*MessageTemplate) Receive ¶
func (t *MessageTemplate) Receive() (*Message, error)
Receive 接收消息
func (*MessageTemplate) ReceiveWithTimeout ¶
func (t *MessageTemplate) ReceiveWithTimeout(timeout time.Duration) (*Message, error)
ReceiveWithTimeout 带超时接收消息
func (*MessageTemplate) SendWithHeaders ¶
func (t *MessageTemplate) SendWithHeaders(body []byte, headers map[string]string) error
SendWithHeaders 发送带消息头的消息
type Queue ¶
type Queue interface {
// Send 发送消息。
Send(msg *Message) error
// Receive 接收消息。
Receive() (*Message, error)
// ReceiveWithTimeout 带超时接收消息。
ReceiveWithTimeout(timeout time.Duration) (*Message, error)
// Consume 消费消息(持续监听)。
Consume(handler MessageHandler) error
// StopConsuming 停止消费。
StopConsuming()
// Purge 清空队列。
Purge() error
// Close 关闭队列。
Close() error
// Name 获取队列名称。
Name() string
// Size 获取队列大小。
Size() int
}
Queue 消息队列接口。
type QueueOption ¶
type QueueOption func(*BaseQueue)
QueueOption 队列配置选项函数。
func WithDeadLetterQueue ¶
func WithDeadLetterQueue(dlq Queue) QueueOption
WithDeadLetterQueue 设置死信队列。