benjamin-hodgson / asynqp

An AMQP library for asyncio
MIT License
84 stars 29 forks source link

how do I check connection status before publish #107

Open hritik09 opened 6 years ago

JustinTArthur commented 5 years ago

There is now a connection close callback that can be used to track when the lib knows the connection has been closed:

connect(
    host='localhost',
    port=5672,
    username='guest', password='guest',
    virtual_host='/',
    on_connection_close=stop_publishing_things  # <----
)

If you're worried about the other side having closed the connection without you knowing, you'd have to send something that would trigger a connection reset response or that would time out before you got your expected response. The only way to do this at the app level would be using the AMQP heartbeat command. At the moment, asynqp does not expose this in a convenient way for awaiting a valid heartbeat response.

This can be done at the transport level using TCP keep-alive, which will allow your on_connection_close callback to be called once the close is detected at the TCP level. You can grab the socket using connection.transport.get_extra_info('socket'), then set the desired keep-alive options using the socket.setsockopt method. Aggressive keepalive settings could be used to discover a closed connection early, but this is still not convenient for knowing for-sure before making a publish, such as from a docker container or serverless runtime that has just been unpaused after a long freeze. At some point, maybe asynqp will expose the heartbeat for this purpose.