streadway / amqp

Go client for AMQP 0.9.1
http://godoc.org/github.com/streadway/amqp
BSD 2-Clause "Simplified" License
4.88k stars 621 forks source link

reconnection when channels are established #371

Closed husainaloos closed 5 years ago

husainaloos commented 6 years ago

I know this issue has been hammered many times, but it seems that there are many different situation where re-connection could be an issue.

Consider this case:

type Connection struct {
    conn *amqp.Connection
    //...
}

func (c *Connection) Create() (*Channel, error) {
    //...
}

type Channel struct {
    c *amqp.Channel
    //...
}

and in my main

func main() {
    var conn := NewConnection()
    c1 := conn.Channel()
    c2 := conn.Channel()
    c3 := conn.Channel()
    //start multiple go routines to publish messages using these channels
}

now if the connection goes down. Now if I reconnect by establishing a new connection, then all the channels will never be back up again since they are using the old connection. Is there a way to reconnect while having the channels work again? It appears that Connection would need to keep track of the channels it creates and somehow notify the Channel struct of any changes. Is there a way to avoid this situation?

michaelklishin commented 5 years ago

Recovery with this client is an application developer's responsibility, by design. See examples. You must open new channels on the new recovered connection (and yes, that's not how Java, .NET, Ruby clients go about recovery but it was a conscious design choice. The most popular client, Pika, does the same thing).

sirius1024 commented 5 years ago

@husainaloos Try this extension with reconnect and cluster supported: https://github.com/sirius1024/go-amqp-reconnect