Skip to content

Commit

Permalink
修复:关闭时,需要判断是否为nil
Browse files Browse the repository at this point in the history
  • Loading branch information
steden committed Dec 19, 2023
1 parent 34007b1 commit 5de21be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
6 changes: 2 additions & 4 deletions healthCheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ type healthCheck struct {

func (receiver *healthCheck) Check() (string, error) {
manager := newManager(receiver.config)

defer manager.Close()

// 连接
err := manager.Open()
defer func(conn *amqp.Connection) {
_ = conn.Close()
}(manager.conn)

// 创建channel
var c *amqp.Channel
Expand Down
16 changes: 12 additions & 4 deletions rabbitConsumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func (receiver *rabbitConsumer) Subscribe(queueName string, routingKey string, p
})
entryMqConsumer.End()
}
_ = chl.Close()
if chl != nil {
_ = chl.Close()
}
// 关闭后,重新调用自己
receiver.Subscribe(queueName, routingKey, prefetchCount, consumerHandle)
}(chl)
Expand Down Expand Up @@ -87,7 +89,9 @@ func (receiver *rabbitConsumer) SubscribeAck(queueName string, routingKey string
}
entryMqConsumer.End()
}
_ = chl.Close()
if chl != nil {
_ = chl.Close()
}
// 关闭后,重新调用自己
receiver.SubscribeAck(queueName, routingKey, prefetchCount, consumerHandle)
}(chl)
Expand Down Expand Up @@ -241,13 +245,17 @@ func (receiver *rabbitConsumer) createAndBindQueue(chl *amqp.Channel, queueName,

// 创建队列
if err = receiver.manager.CreateQueue(chl, queueName, receiver.manager.config.IsDurable, receiver.manager.config.AutoDelete, nil); err != nil {
_ = chl.Close()
if chl != nil {
_ = chl.Close()
}
return nil, err
}

// 绑定队列
if err = receiver.manager.BindQueue(chl, queueName, routingKey, receiver.manager.config.Exchange, nil); err != nil {
_ = chl.Close()
if chl != nil {
_ = chl.Close()
}
return nil, err
}
}
Expand Down
7 changes: 7 additions & 0 deletions rabbitManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ func (receiver *rabbitManager) Open() error {
return nil
}

// Close 关闭
func (receiver *rabbitManager) Close() {
if receiver.conn != nil {
_ = receiver.conn.Close()
}
}

// CreateChannel 创建通道
func (receiver *rabbitManager) CreateChannel() (*amqp.Channel, error) {
var err error
Expand Down

0 comments on commit 5de21be

Please sign in to comment.