From 685ce618a94e6b8ca914b173047270c71604b562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 19 Feb 2024 15:04:14 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E9=80=BB=E8=BE=91=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mq/rabbitmq.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/mq/rabbitmq.go b/mq/rabbitmq.go index 14c7211..c64ff84 100644 --- a/mq/rabbitmq.go +++ b/mq/rabbitmq.go @@ -40,7 +40,16 @@ func Init() { // Receive // @description: 接收消息 -func Receive() (err error) { +func Receive(retry ...int) (err error) { + var retryCount int + if len(retry) > 0 { + retryCount = retry[0] + } + // 重试次数超过100次,退出 + if retryCount > 100 { + log.Panicf("获取消息失败次数过多") + } + // 创建交换机 if err = channel.ExchangeDeclare( exchangeName, @@ -101,7 +110,7 @@ func Receive() (err error) { msg, ok := <-messages if !ok { log.Errorf("获取消息失败") - return Receive() + return Receive(retryCount + 1) } log.Debugf("收到消息: %s", msg.Body) if err = message.Message(msg.Body); err != nil {