Azure / amqpnetlite

AMQP 1.0 .NET Library
Apache License 2.0
396 stars 141 forks source link

Is it not necessary to close a link or a session when a connection exception occurs? #574

Closed anttikes closed 8 months ago

anttikes commented 8 months ago

Hi,

I was looking at the example code here which shows a client and a receiver that are able to recover from connection errors. This is something I would like to implement in my side as well.

I was left wondering that the code only closes the connection when an exception occurs at any given level. Is it so that closing a link or a session is not even necessary if a connection exception is detected? What about link or session level exceptions? Should you close the link or the session, and then re-start at that level, trying to re-establish them without attempting to re-create the connection?

xinchen10 commented 8 months ago

This doc describes the common practices for error handling. https://github.com/Azure/amqpnetlite/blob/master/docs/articles/fault_tolerance.md

anttikes commented 8 months ago

Do I understand correctly that the idea is look into the AMQP specification, map the error conditions (and potential extension errors defined by the counterparty) into various link and session states, and from decide if re-creating the link or session is required, or even possible?

It doesn't quite answer the question. Is closing a link or session even necessary? Or is it so that if you close the underlying connection then any links and sessions established on top of it get closed (and disposed of) automatically?

xinchen10 commented 8 months ago

Link level errors are typically caused by unexpected conditions related to message transfers, e.g. failures with writing data to store, broker failover, metadata updates on terminus, etc. In such conditions, recreating the link should reestablish the message transfer channel. The connection should be still Opened.

Connection level errors are typically caused by network connectivity issues, e.g. tcp resets. When a connection is closed, all sessions/links in it are automatically closed. If you subscribe to the Closed event of a session/link, you will be notified. In the event handler, the error condition should be "amqp:connection:forced" and the connection's state should be End.

You could implement different reconnect logic for these two different error conditions. If the errors are infrequent, you could close the connection and recreate everything to keep it simple.

anttikes commented 8 months ago

Thank you, this clarifies the issue.