From 5f250fa7cd0ae456fae07b3654cb30ae5fcfee5e Mon Sep 17 00:00:00 2001 From: huanghaosheng <386998068@qq.com> Date: Fri, 30 Oct 2026 13:12:05 +0800 Subject: [PATCH] =?UTF-8?q?fix(mq):=20GetRetryCount=20=E5=8F=AA=E7=BB=9F?= =?UTF-8?q?=E8=AE=A1=20rejected=EF=BC=8C=E5=B9=B6=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E9=9D=9E=20int64=20=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原实现取 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 累加"。 --- backend/internal/middleware/rabbitmq/dlx.go | 47 +++++++++++--- .../middleware/rabbitmq/retrycount_test.go | 64 +++++++++++++++++++ 2 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 backend/internal/middleware/rabbitmq/retrycount_test.go diff --git a/backend/internal/middleware/rabbitmq/dlx.go b/backend/internal/middleware/rabbitmq/dlx.go index eafdd3d..5ea462e 100644 --- a/backend/internal/middleware/rabbitmq/dlx.go +++ b/backend/internal/middleware/rabbitmq/dlx.go @@ -43,19 +43,48 @@ func declareDLX(d declarer, queueName string) error { 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 { deaths, ok := d.Headers["x-death"].([]interface{}) - if !ok || len(deaths) == 0 { - return 0 - } - death, ok := deaths[0].(amqp.Table) if !ok { return 0 } - count, ok := death["count"].(int64) - if !ok { - return 0 + 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: + return 0 } - return int(count) } diff --git a/backend/internal/middleware/rabbitmq/retrycount_test.go b/backend/internal/middleware/rabbitmq/retrycount_test.go new file mode 100644 index 0000000..fe3aaa2 --- /dev/null +++ b/backend/internal/middleware/rabbitmq/retrycount_test.go @@ -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) + } + }) + } +}