几个检查 RabbitMq 的 notifyClose 的 goroutine;如果连接断开,如何关闭所有 goroutine?

Several goroutines that check notifyClose for RabbitMq; how do i close all goroutines if the connection drops?

提问人:blackmarllbor0 提问时间:3/23/2023 最后编辑:Jonathan Hallblackmarllbor0 更新时间:3/23/2023 访问量:75

问:

我有这个函数,然后在连接和通道初始化时在循环中调用它。那么,如果一个连接断开,我该如何杀死所有的 go 通道?

func (c Connects) connectingBalancing(
    conn connect,
    channel *amqp.Channel,
    consumer Consumer,
) {
    type chanErr chan *amqp.Error
    var notifyConnClose chanErr

    if conn.err != nil {
        notifyConnClose = conn.err
    } else {
        notifyConnClose = conn.conn.NotifyClose(make(chanErr))
    }

    notifyChanClose := channel.NotifyClose(make(chanErr))

    for notifyConnClose != nil || notifyChanClose != nil {
        select {
        case err, ok := <-notifyConnClose:
            if !ok {
                notifyConnClose = nil
            } else {
                fmt.Println("connection closed, error", err)
            }
        case err, ok := <-notifyChanClose:
            if !ok {
                notifyChanClose = nil
            } else {
                fmt.Println("connection closed, error", err)
                channelStatus = false

                time.Sleep(time.Second * 1)

                newCn, err := conn.conn.Channel()
                if err != nil {
                    log.Println(err)
                }

                if err := c.createChannel(newCn, consumer); err != nil {
                    log.Println(err)
                }

                channel = newCn
                notifyChanClose = channel.NotifyClose(make(chanErr))
            }
        }
    }
}

我曾想过做类似的事情,但我不知道该怎么做。signal.Notify()

多线程 Go RabbitMQ 连接 通知

评论


答: 暂无答案