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
async def connect(self): _, protocol = await self.loop.create_connection( protocol_factory=partial(self.protocol_class, self.apns_topic, None, self.loop, self.discard_connection), host=self.protocol_class.APNS_SERVER, port=self.protocol_class.APNS_PORT, ssl=self.ssl_context) logger.info('Connection established (total: %d)', len(self.connections) + 1) return protocol
def discard_connection(self, connection): logger.debug('Connection %s discarded', connection) self.connections.remove(connection) logger.info('Connection released (total: %d)', len(self.connections))