def connect(self, retries=0): if self.max_retries and retries >= self.max_retries: self.log.error("Aborting after %d retries", retries) raise AMQPConnectionError('Aborting after %d connection failures.' % retries) return # Pick up a host host = self.hosts.pop() self.hosts.insert(0, host) self.client = Client(host, pubacks=self.confirms) host = host.split('@')[-1] self.log.debug('Connecting to node %s' % host) try: promise = self.client.connect() self.client.wait(promise) except socket_error as e: if retries < len(self.hosts): self.log.warning('Cannot connect to host %s: %s', host, e) else: self.log.error('Cannot connect to host %s: %s', host, e) sleep(1) return self.connect(retries + 1) self.log.info('Successfully connected to host: %s', host) # Setup TCP keepalive option self.client.sd.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Keepalive time self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 20) # Keepalive interval self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 2) # Keepalive retry self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 10) self.log.info('Creating channel') # Clear consume_promises each time connecting, since they are related # to the connection object self.consume_promises = [] if self.unacked: self._resend_unacked_messages() if self.unsend: self._resend_unsend_messages() if self.consumers: for queue, callback in self.consumers.items(): self.basic_consume(queue, callback) if self.exchanges: exchanges = self.exchanges self.exchanges = [] for exchange, type in exchanges: self.exchange_declare(exchange, type)
def connect(self): """Initialize a connection with the AMQP server. If it fails, retry at most <max_retries> times. """ retries = 0 self.client = None # Try to connect at most <max_retries> times while self.max_retries == 0 or retries < self.max_retries: retries += 1 # Pick up a host host = self.hosts.pop() self.hosts.insert(0, host) self.client = Client(host, pubacks=self.confirms) host = host.split('@')[-1] self.log.debug('Connecting to node %s' % host) try: promise = self.client.connect() self.client.wait(promise) break except socket_error as e: self.client = None if retries < len(self.hosts): self.log.warning('Cannot connect to host %s: %s', host, e) else: self.log.error('Cannot connect to host %s: %s', host, e) sleep(1) if not self.client: raise AMQPConnectionError( 'Aborting after %d connection failures.' % retries) self.log.info('Successfully connected to host: %s', host) # Setup TCP keepalive option self.client.sd.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) # Keepalive time self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPIDLE, 20) # Keepalive interval self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPINTVL, 2) # Keepalive retry self.client.sd.setsockopt(socket.SOL_TCP, socket.TCP_KEEPCNT, 10) self.log.info('Creating channel') # Clear consume_promises each time connecting, since they are related # to the connection object self.consume_promises = [] if self.unacked: self.log.debug("Resending unacked messages from previous" " connection") self._resend_unacked_messages() if self.unsend: self.log.debug("Resending unsent messages from previous" " connection") self._resend_unsend_messages() if self.exchanges: exchanges = self.exchanges self.exchanges = [] for exchange, type in exchanges: self.exchange_declare(exchange, type) if self.consumers: for queue, callback in self.consumers.items(): self.basic_consume(queue, callback)