async def acquire(self) -> FCMXMPPConnection: for connection in self.connections: if not connection.is_busy: return connection else: await self._lock.acquire() for connection in self.connections: if not connection.is_busy: self._lock.release() return connection if len(self.connections) < self.max_connections: try: connection = await self.connect() except Exception as e: logger.error('Could not connect to server: %s', e) self._lock.release() raise ConnectionError self.connections.append(connection) self._lock.release() return connection else: self._lock.release() while True: await asyncio.sleep(0.01) for connection in self.connections: if not connection.is_busy: return connection
async def send_message(self, message: Message) -> MessageResponse: attempt = 0 while True: attempt += 1 if attempt > self.MAX_ATTEMPTS: logger.warning('Trying to send message %s: attempt #%s', message.message_id, attempt) logger.debug('Message %s: waiting for connection', message.message_id) try: connection = await self.acquire() except ConnectionError: logger.warning( 'Could not send notification %s due to ' 'connection problem', message.message_id) await asyncio.sleep(1) continue logger.debug('Message %s: connection %s acquired', message.message_id, connection) try: response = await connection.send_message(message) return response except ConnectionClosed: logger.warning( 'Could not send message %s: ' 'ConnectionClosed', message.message_id) except Exception as e: logger.error('Could not send message %s: %s', message.message_id, e)
async def send_message(self, message: Message) -> MessageResponse: response = await self.pool.send_message(message) if not response.is_successful: msg = 'Status of message %s is %s' %\ (message.message_id, response.status) if response.description: msg += ' (%s)' % response.description logger.error(msg) return response
async def send_message(self, message): attempt = 0 while True: attempt += 1 if attempt > self.MAX_ATTEMPTS: logger.warning('Trying to send message %s: attempt #%s', message.message_id, attempt) logger.debug('Message %s: waiting for connection', message.message_id) connection = await self.acquire() logger.debug('Message %s: connection %s acquired', message.message_id, connection) try: response = await connection.send_message(message) return response except ConnectionClosed: logger.warning( 'Could not send message %s: ' 'ConnectionClosed', message.message_id) except Exception as e: logger.error('Could not send message %s: %s', message.message_id, e)