Skip to content

Commit

Permalink
adding documentation for notifyClose best pratices (#60)
Browse files Browse the repository at this point in the history
* adding documentation for notifyClose best pratices

* adding documentation for notifyClose best pratices

* fix doc

Co-authored-by: Daniele Palaia <dpalaia@dpalaia-a02.vmware.com>
  • Loading branch information
DanielePalaia and Daniele Palaia authored Apr 1, 2022
1 parent 7220a80 commit a9a4167
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,43 @@ encounters an amqp:// scheme.
SSL/TLS in RabbitMQ is documented here: http://www.rabbitmq.com/ssl.html
Best practises for Connections and Channels notifications.
In order to be notified when a connection or channel gets closed both the structures offer the possibility to register channels using the `notifyClose` function like:
notifyConnClose := make(chan *amqp.Error)
conn.NotifyClose(notifyConnClose)
No errors will be sent in case of a graceful connection close.
In case of a non-graceful close, because of a network issue of forced disconnection from the UI, the error will be notified synchronously by the library.
You can see that in the shutdown function of connection and channel (see connection.go and channel.go)
```
if err != nil {
for _, c := range c.closes {
c <- err
}
}
```
The error is sent synchronously to the channel so that the flow will wait until the channel will be consumed by the caller.
To avoid deadlocks it is necessary to consume the messages from the channels.
This could be done inside a different goroutine with a select listening on the two channels inside a for loop like:
```
go func() {
for notifyConnClose != nil || notifyChanClose != nil {
select {
case err, ok := <-notifyConnClose:
if !(ok) {
notifyConnClose = nil
} else {
fmt.Printf("connection closed, error %s", err)
}
case err, ok := <-notifyChanClose:
if !(ok) {
notifyChanClose = nil
} else {
fmt.Printf("channel closed, error %s", err)
}
}
}
}()
```
*/

package amqp091

0 comments on commit a9a4167

Please sign in to comment.