def on_response(self, message): self.refresh_inactivity_timer() body = json.loads(message.fcm_payload.text) try: message_id = body['message_id'] message_type = body['message_type'] except KeyError: logger.warning('Got strange response: %s', body) return if message_type not in (FCMMessageType.ACK, FCMMessageType.NACK): return request = self.requests.pop(message_id, None) if not request: logger.warning('Got response for unknown message %s', message_id) return if message_type == FCMMessageType.ACK: result = MessageResponse(message_id, STATUS_SUCCESS) request.set_result(result) elif message_type == FCMMessageType.NACK: status = body['error'] description = body['error_description'] result = MessageResponse(message_id, status, description) request.set_result(result)
async def acquire(self): 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() logger.warning('Pool is busy, wait...') while True: await asyncio.sleep(0.01) for connection in self.connections: if not connection.is_busy: return connection
def on_response(self, message): self.refresh_inactivity_timer() body = json.loads(message.fcm_payload.text) handle_upstream = False try: message_id = body['message_id'] message_type = body['message_type'] except KeyError: try: message_id = body['message_id'] category = body['category'] device_token = body['from'] data = body['data'] handle_upstream = True except KeyError: logger.warning('Got strange response: %s', body) return if handle_upstream: asyncio.ensure_future(self.send_ack(device_token, message_id)) if self.upstream_callback is not None: asyncio.ensure_future( self.upstream_callback(device_token, category, data)) return if message_type not in (FCMMessageType.ACK, FCMMessageType.NACK): return request = self.requests.pop(message_id, None) if not request: logger.warning('Got response for unknown message %s', message_id) return if message_type == FCMMessageType.ACK: result = MessageResponse(message_id, STATUS_SUCCESS) request.set_result(result) elif message_type == FCMMessageType.NACK: status = body['error'] description = body['error_description'] result = MessageResponse(message_id, status, description) request.set_result(result)
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): 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)
async def acquire(self): 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: connection = await self.connect() self.connections.append(connection) self._lock.release() return connection else: self._lock.release() logger.warning('Pool is busy, wait...') while True: await asyncio.sleep(0.01) for connection in self.connections: if not connection.is_busy: return connection