dart_amqp
dart_amqp copied to clipboard
feat: implement heartbeat
Implements #35
I am not 100% sure that this PR implements the spec correctly.
In particular, only channel zero must respond to heartbeats; if you receive a heartbeat on another channel the driver must signal a connection exception as seen below:
The channel number MUST be zero for all heartbeat frames, and for method, header and body frames
that refer to the Connection class. A peer that receives a non-zero channel number for one of these
frames MUST signal a connection exception with reply code 503 (command invalid).
Another thing that might cause problems is this section:
The peer should make a best effort to send heartbeats at specified intervals. Heartbeats can be sent at
any time. Any sent octet is a valid substitute for a heartbeat, thus heartbeats only have to be sent if no
non-heartbeat AMQP traffic is sent for longer than one heartbeat interval. If a peer detects no incoming
traffic (i.e. received octets) for two heartbeat intervals or longer, it should close the connection without
following the Connection.Close/Close-Ok handshaking, and log an error
The proposed implementation writes the frame when it gets one from the server. However, what if the server stops sending heartbeats (the code doesn't check for that) or what the server stops sending heartbeats while the client is blocked waiting for a message to be dequeued. According to my interpretation of the spec, if the client does not send a heartbeat the server can close the connection.
Perhaps setting up a timer to send out heartbeats would be a better solution?