wagslane / go-rabbitmq

A wrapper of streadway/amqp that provides reconnection logic and sane defaults
https://blog.boot.dev/golang/connecting-to-rabbitmq-in-golang-easy/
MIT License
772 stars 126 forks source link

runs consumer reconnection by any rabbit error #65

Closed fortyanov closed 2 years ago

fortyanov commented 2 years ago

I think that may be good idea to run consumer reconnection by any error because there is situations when internet connection to rabbit service breaks for a while and after that consumer not working anymore. It might be worth adding some field for set up this behavior to the consumer settings fields, but I'm not sure.

wagslane commented 2 years ago

Sorry this won't work, this will attempt to reconnect if the error is a client error, something I don't think we want to do. That said, if you can give me an example of some client errors we should be reconnecting on Id be happy to change my mind

fortyanov commented 2 years ago

If I disable network connection between consumer and container with rabbit Im getting this error in channel.go:62 Screenshot from 2022-03-11 16-48-28 after that all consuming goroutines closing gorabbit: rabbit consumer goroutine closed and consuming service never stands back, event if network connection with rabbit returns. May be my solution is too rough, but I dont find another way out of this problem.

wagslane commented 2 years ago

Got it @fortyanov ! Can you try this instead then. If it solves the issue I'll be happy to merge a PR with this code:

// If the connection close is triggered by the Server, a reconnection takes place
        if err != nil && err.Server {
            chManager.logger.Printf("attempting to reconnect to amqp server after close")
            chManager.reconnectLoop()
            chManager.logger.Printf("successfully reconnected to amqp server after close")
            chManager.notifyCancelOrClose <- err
        } else if err != nil && errors.Is(err, io.EOF) {
            chManager.logger.Printf("attempting to reconnect to amqp server after eof")
            chManager.reconnectLoop()
            chManager.logger.Printf("successfully reconnected to amqp server after eof")
            chManager.notifyCancelOrClose <- err
        } else if err != nil {
            chManager.logger.Printf("not attempting to reconnect to amqp server because closure was initiated by the client")
        } else if err == nil {
            chManager.logger.Printf("amqp channel closed gracefully")
        }
fortyanov commented 2 years ago

@wagslane I checked your solution and seems like understand what you mean. errors.Is(err, io.EOF) not works because of type github.com/rabbitmq/amqp091-go.Error not an io.Error so i just makes check by field Reason, and check solution again. It works fine.

wagslane commented 2 years ago

Perfect, looks good to me! Thanks

wagslane commented 2 years ago

oh @fortyanov looks like you need to pull the latest changes, you have a naming error in the function: https://github.com/wagslane/go-rabbitmq/runs/5554809975?check_suite_focus=true

fortyanov commented 2 years ago

@wagslane done