Esempio n. 1
0
 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', str(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
Esempio n. 2
0
    async def send_notification(self, request):
        failed_attempts = 0
        while True:
            logger.debug('Notification %s: waiting for connection',
                         request.notification_id)
            try:
                connection = await self.acquire()
            except ConnectionError:
                failed_attempts += 1
                logger.warning('Could not send notification %s: '
                               'ConnectionError', request.notification_id)

                if self.max_connection_attempts \
                        and failed_attempts > self.max_connection_attempts:
                    logger.error('Failed to connect after %d attempts.',
                                 failed_attempts)
                    raise

                await asyncio.sleep(1)
                continue
            logger.debug('Notification %s: connection %s acquired',
                         request.notification_id, connection)
            try:
                response = await connection.send_notification(request)
                return response
            except NoAvailableStreamIDError:
                connection.close()
            except ConnectionClosed:
                logger.warning('Could not send notification %s: '
                               'ConnectionClosed', request.notification_id)
            except FlowControlError:
                logger.debug('Got FlowControlError for notification %s',
                             request.notification_id)
                await asyncio.sleep(1)
Esempio n. 3
0
 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.create_connection()
             except Exception as e:
                 logger.error("Could not connect to server: %s", str(e))
                 self._lock.release()
                 raise ConnectionError()
             self.connections.append(connection)
             logger.info("Connection established (total: %d)",
                         len(self.connections))
             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
Esempio n. 4
0
 async def send_notification(self, request):
     response = await self.pool.send_notification(request)
     if not response.is_successful:
         logger.error('Status of notification %s is %s (%s)',
                      request.notification_id, response.status,
                      response.description)
     return response
Esempio n. 5
0
 async def send_notification(self, request):
     attempts = 0
     while attempts < self.max_connection_attempts:
         attempts += 1
         logger.debug(
             "Notification %s: waiting for connection",
             request.notification_id,
         )
         try:
             connection = await self.acquire()
         except ConnectionError:
             logger.warning(
                 "Could not send notification %s: "
                 "ConnectionError",
                 request.notification_id,
             )
             await asyncio.sleep(1)
             continue
         logger.debug(
             "Notification %s: connection %s acquired",
             request.notification_id,
             connection,
         )
         try:
             response = await connection.send_notification(request)
             return response
         except NoAvailableStreamIDError:
             connection.close()
         except ConnectionClosed:
             logger.warning(
                 "Could not send notification %s: "
                 "ConnectionClosed",
                 request.notification_id,
             )
         except FlowControlError:
             logger.debug(
                 "Got FlowControlError for notification %s",
                 request.notification_id,
             )
             await asyncio.sleep(1)
     logger.error("Failed to send after %d attempts.", attempts)
     raise MaxAttemptsExceeded