2026-04-25 15:54:55 +08:00
|
|
|
|
package rabbitmq
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"log"
|
|
|
|
|
|
|
|
|
|
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
DLXExchange = "dlx.events"
|
|
|
|
|
|
MaxRetryCount = 3
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-10-14 14:02:15 +08:00
|
|
|
|
// DeclareDLX 声明死信交换机和对应的死信队列(保留原有签名)。
|
2026-04-25 15:54:55 +08:00
|
|
|
|
func DeclareDLX(ch *amqp.Channel, queueName string) error {
|
|
|
|
|
|
if ch == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2026-10-14 14:02:15 +08:00
|
|
|
|
return declareDLX(ch, queueName)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// declareDLX 是 DeclareDLX 的接口版本,供拓扑重放路径复用。
|
|
|
|
|
|
func declareDLX(d declarer, queueName string) error {
|
|
|
|
|
|
if d == nil {
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if err := d.ExchangeDeclare(
|
2026-04-25 15:54:55 +08:00
|
|
|
|
DLXExchange, "topic", true, false, false, false, nil,
|
|
|
|
|
|
); err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
dlxQueue := queueName + ".dlx"
|
2026-10-14 14:02:15 +08:00
|
|
|
|
_, err := d.QueueDeclare(
|
2026-07-15 11:54:01 +08:00
|
|
|
|
dlxQueue, true, false, false, false, nil, // 死信队列不设置 DLX
|
2026-04-25 15:54:55 +08:00
|
|
|
|
)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2026-10-14 14:02:15 +08:00
|
|
|
|
if err := d.QueueBind(dlxQueue, "#", DLXExchange, false, nil); err != nil {
|
2026-04-25 15:54:55 +08:00
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
log.Printf("DLX ready: exchange=%s queue=%s", DLXExchange, dlxQueue)
|
|
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 13:12:05 +08:00
|
|
|
|
// GetRetryCount 返回这条消息被【拒绝过】的次数。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 两个坑:
|
|
|
|
|
|
// 1. 不读 deaths[0]。真实链路里最新一条往往是重试队列的 "expired"(TTL 到期),
|
|
|
|
|
|
// 那是"延迟结束",不是"处理失败"。只数 reason == "rejected"。
|
|
|
|
|
|
// 2. 不假定 count 是 int64。amqp091-go 在不同路径上会给出 int32/int64/float64,
|
|
|
|
|
|
// 断言成 int64 会在真实消息上静默返回 0——计数恒为 0 就意味着"永远不重试耗尽"。
|
2026-04-25 15:54:55 +08:00
|
|
|
|
func GetRetryCount(d amqp.Delivery) int {
|
|
|
|
|
|
deaths, ok := d.Headers["x-death"].([]interface{})
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return 0
|
|
|
|
|
|
}
|
2026-10-30 13:12:05 +08:00
|
|
|
|
total := 0
|
|
|
|
|
|
for _, raw := range deaths {
|
|
|
|
|
|
entry, ok := raw.(amqp.Table)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if reason, _ := entry["reason"].(string); reason != "rejected" {
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
total += intField(entry["count"])
|
|
|
|
|
|
}
|
|
|
|
|
|
return total
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// intField 把 AMQP table 里的整数值统一成 int(库在不同路径上给出不同宽度)。
|
|
|
|
|
|
func intField(v any) int {
|
|
|
|
|
|
switch n := v.(type) {
|
|
|
|
|
|
case int:
|
|
|
|
|
|
return n
|
|
|
|
|
|
case int32:
|
|
|
|
|
|
return int(n)
|
|
|
|
|
|
case int64:
|
|
|
|
|
|
return int(n)
|
|
|
|
|
|
case uint32:
|
|
|
|
|
|
return int(n)
|
|
|
|
|
|
case uint64:
|
|
|
|
|
|
return int(n)
|
|
|
|
|
|
case float64:
|
|
|
|
|
|
return int(n)
|
|
|
|
|
|
default:
|
2026-04-25 15:54:55 +08:00
|
|
|
|
return 0
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|