2026-10-14 14:03:08 +08:00
|
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
"errors"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// RunConsumer 在连接管理器上运行一个消费循环。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
//
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 连接断开期间阻塞在 WaitReady;恢复后重建 channel、重设 QoS、重新 Consume。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
// fn 应在 deliveries channel 关闭或 ctx 取消时返回。
|
|
|
|
|
|
func RunConsumer(ctx context.Context, mgr *RabbitMQ, name string, prefetch int, fn func(*amqp.Channel) error) {
|
|
|
|
|
|
if mgr == nil {
|
|
|
|
|
|
log.Printf("%s: rabbitmq manager is nil, consumer disabled", name)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if fn == nil {
|
|
|
|
|
|
log.Printf("%s: consume func is nil, consumer disabled", name)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
opts := mgr.opts
|
|
|
|
|
|
backoff := opts.MinBackoff
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 消费前先确保拓扑存在,覆盖"连接建立早于拓扑登记"的启动顺序。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
if err := mgr.EnsureTopology(ctx); err != nil && ctx.Err() == nil {
|
|
|
|
|
|
log.Printf("%s: 初始拓扑声明失败: %v", name, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for ctx.Err() == nil {
|
|
|
|
|
|
sess, err := mgr.Session(ctx)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
if ctx.Err() != nil || errors.Is(err, ErrManagerClosed) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Printf("%s: 获取 channel 失败: %v(%v 后重试)", name, err, backoff)
|
|
|
|
|
|
if !sleepCtx(ctx, backoff) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
backoff = nextBackoff(backoff, opts)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// QoS 是 channel 级状态,每次重建 channel 后都必须重设。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
if prefetch > 0 {
|
|
|
|
|
|
if err := sess.Ch.Qos(prefetch, 0, false); err != nil {
|
|
|
|
|
|
log.Printf("%s: QoS 设置失败: %v", name, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
log.Printf("%s started, consuming", name)
|
|
|
|
|
|
runErr := fn(sess.Ch)
|
|
|
|
|
|
_ = sess.Ch.Close()
|
|
|
|
|
|
|
|
|
|
|
|
if ctx.Err() != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 不能只看 sess.Done:库先派发 NotifyClose、后关闭 deliveries,
|
|
|
|
|
|
// 那一瞬 sess.Done 往往还没关闭。直接复查连接状态更准确。
|
2026-10-14 14:03:08 +08:00
|
|
|
|
connLost := false
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-sess.Done:
|
|
|
|
|
|
connLost = true
|
|
|
|
|
|
default:
|
|
|
|
|
|
connLost = !mgr.Healthy()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if connLost {
|
|
|
|
|
|
if err := mgr.WaitReady(ctx); err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
backoff = opts.MinBackoff
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-31 10:08:03 +08:00
|
|
|
|
// 连接仍健康 → channel 级异常(队列或绑定丢失、声明冲突等)
|
2026-10-14 14:03:08 +08:00
|
|
|
|
if runErr != nil {
|
|
|
|
|
|
log.Printf("%s: %v;连接仍健康,重放拓扑后重试", name, runErr)
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := mgr.EnsureTopology(ctx); err != nil && ctx.Err() == nil {
|
|
|
|
|
|
log.Printf("%s: 重放拓扑失败: %v", name, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if !sleepCtx(ctx, backoff) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
backoff = nextBackoff(backoff, opts)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func sleepCtx(ctx context.Context, d time.Duration) bool {
|
|
|
|
|
|
t := time.NewTimer(d)
|
|
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
select {
|
|
|
|
|
|
case <-t.C:
|
|
|
|
|
|
return true
|
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|