diff --git a/mq/rabbitmq.go b/mq/rabbitmq.go index 14c7211d..c64ff84f 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 {