fix(mq): GetRetryCount 只统计 rejected,并兼容非 int64 计数
原实现取 deaths[0] 并把 count 断言成 int64,在真实链路上两条都会让结果恒为 0: - deaths[0] 常常是重试队列的 "expired" 条目(TTL 到期),它表示"延迟结束", 不是"处理失败";把它算进来也会让计数含义失真。 - amqp091-go 在不同路径上给出的 count 宽度不同(int32/int64/float64), 断言 int64 失败就静默返回 0。 计数恒为 0 的后果不是"少重试几次",而是"重试永远不耗尽"——任何依赖它的 重试上限判定都会失效。改为遍历所有 x-death 条目、只累加 reason=="rejected", 并用 intField 统一宽度。 守护测试 TestGetRetryCount_CountsOnlyRejected 覆盖 7 种 x-death 形状, 含"rejected 与 expired 混合时只累加 rejected"与"多条 rejected 累加"。
This commit is contained in:
@@ -43,19 +43,48 @@ func declareDLX(d declarer, queueName string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRetryCount 从 AMQP x-death header 中提取当前消息已被重试的次数
|
// GetRetryCount 返回这条消息被【拒绝过】的次数。
|
||||||
|
//
|
||||||
|
// 两个坑:
|
||||||
|
// 1. 不读 deaths[0]。真实链路里最新一条往往是重试队列的 "expired"(TTL 到期),
|
||||||
|
// 那是"延迟结束",不是"处理失败"。只数 reason == "rejected"。
|
||||||
|
// 2. 不假定 count 是 int64。amqp091-go 在不同路径上会给出 int32/int64/float64,
|
||||||
|
// 断言成 int64 会在真实消息上静默返回 0——计数恒为 0 就意味着"永远不重试耗尽"。
|
||||||
func GetRetryCount(d amqp.Delivery) int {
|
func GetRetryCount(d amqp.Delivery) int {
|
||||||
deaths, ok := d.Headers["x-death"].([]interface{})
|
deaths, ok := d.Headers["x-death"].([]interface{})
|
||||||
if !ok || len(deaths) == 0 {
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
death, ok := deaths[0].(amqp.Table)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
count, ok := death["count"].(int64)
|
total := 0
|
||||||
|
for _, raw := range deaths {
|
||||||
|
entry, ok := raw.(amqp.Table)
|
||||||
if !ok {
|
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:
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
return int(count)
|
|
||||||
}
|
}
|
||||||
|
|||||||
64
backend/internal/middleware/rabbitmq/retrycount_test.go
Normal file
64
backend/internal/middleware/rabbitmq/retrycount_test.go
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
package rabbitmq
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
amqp "github.com/rabbitmq/amqp091-go"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestGetRetryCount_CountsOnlyRejected 守护两个曾经让计数恒为 0 的坑:
|
||||||
|
// 读 deaths[0](真实链路里常常是重试队列的 expired),以及把 count 断言成 int64。
|
||||||
|
func TestGetRetryCount_CountsOnlyRejected(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
name string
|
||||||
|
headers amqp.Table
|
||||||
|
want int
|
||||||
|
}{
|
||||||
|
{"无 x-death", amqp.Table{}, 0},
|
||||||
|
{"x-death 类型不对", amqp.Table{"x-death": "nope"}, 0},
|
||||||
|
{
|
||||||
|
name: "只有 expired 不算重试",
|
||||||
|
headers: amqp.Table{"x-death": []interface{}{
|
||||||
|
amqp.Table{"queue": "like.events.retry", "reason": "expired", "count": int64(1)},
|
||||||
|
}},
|
||||||
|
want: 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "int64 计数的 rejected",
|
||||||
|
headers: amqp.Table{"x-death": []interface{}{
|
||||||
|
amqp.Table{"queue": "like.events", "reason": "rejected", "count": int64(2)},
|
||||||
|
}},
|
||||||
|
want: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "int32 计数的 rejected",
|
||||||
|
headers: amqp.Table{"x-death": []interface{}{
|
||||||
|
amqp.Table{"queue": "like.events", "reason": "rejected", "count": int32(1)},
|
||||||
|
}},
|
||||||
|
want: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "rejected 与 expired 混合时只累加 rejected",
|
||||||
|
headers: amqp.Table{"x-death": []interface{}{
|
||||||
|
amqp.Table{"queue": "like.events.retry", "reason": "expired", "count": int64(3)},
|
||||||
|
amqp.Table{"queue": "like.events", "reason": "rejected", "count": int64(3)},
|
||||||
|
}},
|
||||||
|
want: 3,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "多条 rejected 累加",
|
||||||
|
headers: amqp.Table{"x-death": []interface{}{
|
||||||
|
amqp.Table{"queue": "a", "reason": "rejected", "count": int64(1)},
|
||||||
|
amqp.Table{"queue": "b", "reason": "rejected", "count": int64(2)},
|
||||||
|
}},
|
||||||
|
want: 3,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range cases {
|
||||||
|
t.Run(c.name, func(t *testing.T) {
|
||||||
|
if got := GetRetryCount(amqp.Delivery{Headers: c.headers}); got != c.want {
|
||||||
|
t.Fatalf("GetRetryCount = %d, want %d", got, c.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user