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

109 Views Asked by At

I have this func, which is then called in the loop when the connections and channels are init. So if one connection goes down, how do i kill all the go channels?

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))
            }
        }
    }
}

I had thoughts of doing something like signal.Notify(), but I can't figure out how do it.

0

There are 0 best solutions below