Files
VLoop/backend/internal/middleware/rabbitmq/topology.go
huanghaosheng 87f6e91ed1 refactor(rabbitmq): 抽出可测试的拓扑声明层
把"交换机 + 队列 + 绑定键 + 死信队列"的声明逻辑从 rabbitMQ.go 抽成独立的
Topology 类型与 DeclareTopology/DeclareTopologies,并引入声明所需的最小接口
declarer(*amqp.Channel 天然满足)。

两处动机:其一,后续连接管理器需要在每次重连后重放同一批拓扑,声明逻辑必须
可复用、可枚举;其二,声明顺序(交换机 → 队列 → 绑定)与队列上的
x-dead-letter-exchange 参数是重构中最容易静默丢失的两件事,抽出接口后即可用
假实现断言它们,无需真实 broker。

DeclareDLX 新增 declareDLX(declarer, ...) 供拓扑重放复用;DeclareTopic 与
DeclareDLX 均保留原有签名,现有调用点无需改动。
2026-10-14 14:02:15 +08:00

104 lines
3.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package rabbitmq
import (
"errors"
"fmt"
"log"
amqp "github.com/rabbitmq/amqp091-go"
)
// Topology 描述一组"交换机 + 队列 + 绑定键",是可重复声明的幂等单元。
//
// 连接管理器会在每一代连接上重放已登记的拓扑,因此 broker 侧拓扑丢失
// (数据卷重建、队列被手工删除、集群节点替换)之后也能自动恢复——
// 这一条与"连接重连"同等重要:只重连不重放拓扑,
// 会退化为"连接健康但队列不存在"的 404 死循环。
type Topology struct {
Exchange string
Queue string
BindingKey string
}
// declarer 是 *amqp.Channel 上拓扑声明所需方法的最小集合。
//
// 抽出接口的唯一目的是可测性:声明顺序与 DLX 参数是"重构时最容易静默丢失"
// 的两件事,有了它就能在没有 broker 的情况下断言它们。
type declarer interface {
ExchangeDeclare(name, kind string, durable, autoDelete, internal, noWait bool, args amqp.Table) error
QueueDeclare(name string, durable, autoDelete, exclusive, noWait bool, args amqp.Table) (amqp.Queue, error)
QueueBind(name, key, exchange string, noWait bool, args amqp.Table) error
}
// DeclareTopology 声明一组拓扑:交换机 → 队列(带 DLX)→ 绑定 → 死信队列。
//
// 注意:d 为 nil 的判断只能拦住"nil 接口"。若调用方传入类型化的 nil
// (如 var ch *amqp.Channel = nil),此处无法识别——因此 *amqp.Channel
// 的入口(DeclareTopic)保留了自己的 nil 检查。
func DeclareTopology(d declarer, t Topology) error {
if d == nil {
return errors.New("channel is not initialized")
}
if t.Exchange == "" || t.Queue == "" || t.BindingKey == "" {
return errors.New("exchange/queue/bindingKey is required")
}
if err := d.ExchangeDeclare(
t.Exchange,
"topic",
true, // durable
false, // autoDelete
false, // internal
false, // noWait
nil,
); err != nil {
return err
}
q, err := d.QueueDeclare(
t.Queue,
true, // durable
false, // autoDelete
false, // exclusive
false, // noWait
amqp.Table{"x-dead-letter-exchange": DLXExchange},
)
if err != nil {
return err
}
if err := d.QueueBind(
q.Name,
t.BindingKey,
t.Exchange,
false, // noWait
nil,
); err != nil {
return err
}
// DLX 只声明一次即可;重复声明幂等。失败降级为告警(与改造前行为一致)。
if err := declareDLX(d, t.Queue); err != nil {
log.Printf("DLX declare failed for %s: %v", t.Queue, err)
}
return nil
}
// DeclareTopologies 按登记顺序重放拓扑。顺序有意义:交换机先于队列先于绑定。
func DeclareTopologies(d declarer, topo []Topology) error {
for _, t := range topo {
if err := DeclareTopology(d, t); err != nil {
return fmt.Errorf("declare %s/%s: %w", t.Exchange, t.Queue, err)
}
}
return nil
}
// DeclareTopic 保留原有签名,既有调用点无需改动。
func DeclareTopic(ch *amqp.Channel, exchange string, queue string, bindingKey string) error {
if ch == nil {
return errors.New("channel is not initialized")
}
return DeclareTopology(ch, Topology{Exchange: exchange, Queue: queue, BindingKey: bindingKey})
}