2525
2626class Connection (Stateful ):
2727 """AMQP Connection"""
28+ __slots__ = [
29+ 'heartbeat' , 'parameters' , '_channel0' , '_channels' , '_io'
30+ ]
2831
2932 def __init__ (self , hostname , username , password , port = 5672 , ** kwargs ):
3033 """
@@ -56,10 +59,12 @@ def __init__(self, hostname, username, password, port=5672, **kwargs):
5659 'ssl_options' : kwargs .get ('ssl_options' , {})
5760 }
5861 self ._validate_parameters ()
59- self .heartbeat = Heartbeat (self .parameters [ 'heartbeat' ])
60- self . _io = IO ( self . parameters , on_read = self ._read_buffer )
62+ self ._io = IO (self .parameters , exceptions = self . _exceptions ,
63+ on_read = self ._read_buffer )
6164 self ._channel0 = Channel0 (self )
6265 self ._channels = {}
66+ self .heartbeat = Heartbeat (self .parameters ['heartbeat' ],
67+ self ._channel0 .send_heartbeat )
6368 if not kwargs .get ('lazy' , False ):
6469 self .open ()
6570
@@ -116,7 +121,7 @@ def open(self):
116121 LOGGER .debug ('Connection Opening' )
117122 self .set_state (self .OPENING )
118123 self ._exceptions = []
119- self ._io .open (self . _exceptions )
124+ self ._io .open ()
120125 self ._send_handshake ()
121126 self ._wait_for_connection_to_open ()
122127 self .heartbeat .start (self ._exceptions )
@@ -180,6 +185,7 @@ def write_frame(self, channel_id, frame_out):
180185 :return:
181186 """
182187 frame_data = pamqp_frame .marshal (frame_out , channel_id )
188+ self .heartbeat .register_write ()
183189 self ._io .write_to_socket (frame_data )
184190
185191 def write_frames (self , channel_id , multiple_frames ):
@@ -192,6 +198,7 @@ def write_frames(self, channel_id, multiple_frames):
192198 frame_data = EMPTY_BUFFER
193199 for single_frame in multiple_frames :
194200 frame_data += pamqp_frame .marshal (single_frame , channel_id )
201+ self .heartbeat .register_write ()
195202 self ._io .write_to_socket (frame_data )
196203
197204 def _validate_parameters (self ):
@@ -247,7 +254,7 @@ def _read_buffer(self, buffer):
247254 if frame_in is None :
248255 break
249256
250- self .heartbeat .register_beat ()
257+ self .heartbeat .register_read ()
251258 if channel_id == 0 :
252259 self ._channel0 .on_frame (frame_in )
253260 else :
@@ -270,7 +277,7 @@ def _handle_amqp_frame(self, data_in):
270277 pass
271278 except pamqp_spec .AMQPFrameError as why :
272279 LOGGER .error ('AMQPFrameError: %r' , why , exc_info = True )
273- except ( UnicodeDecodeError , ValueError ) as why :
280+ except ValueError as why :
274281 LOGGER .error (why , exc_info = True )
275282 self .exceptions .append (AMQPConnectionError (why ))
276283 return data_in , None , None
0 commit comments