Ejemplo n.º 1
0
    def get_connection(self):
        with self._lock:
            try:
                conn = self.idle.pop()

                curr_time = datetime.datetime.now()
                # If 'refresh_connection' flag is True and the connection was
                # created more than 'connection_refresh_time' seconds ago,
                # release the connection (as its stale) and create a new one
                if self.refresh_connection and (
                        curr_time - conn.create_time
                ).total_seconds() > self.connection_refresh_time:
                    logger.debug(
                        'Connection with id {} was created more than {} seconds ago. Releasing the connection and creating a new one.'
                        .format(id(conn), self.connection_refresh_time))
                    self.release_connection(conn, True)
                    conn = Connection(self, self.account)
                    logger.debug("Created new connection with id: {}".format(
                        id(conn)))
            except KeyError:
                conn = Connection(self, self.account)
                logger.debug(
                    "No connection found in idle set. Created a new connection with id: {}"
                    .format(id(conn)))

            self.active.add(conn)
            logger.debug("Adding connection with id {} to active set".format(
                id(conn)))

        logger.debug('num active: {}'.format(len(self.active)))
        logger.debug('num idle: {}'.format(len(self.idle)))
        return conn
Ejemplo n.º 2
0
 def get_connection(self):
     with self._lock:
         try:
             conn = self.idle.pop()
         except KeyError:
             conn = Connection(self, self.account)
         self.active.add(conn)
     logger.debug('num active: %d' % len(self.active))
     return conn
Ejemplo n.º 3
0
    def get_connection(self):
        with self._lock:
            try:
                conn = self.idle.pop()

                curr_time = datetime.datetime.now()
                # If 'refresh_connection' flag is True and the connection was
                # created more than 'connection_refresh_time' seconds ago,
                # release the connection (as its stale) and create a new one
                if self.refresh_connection and (
                        curr_time - conn.create_time
                ).total_seconds() > self.connection_refresh_time:
                    logger.debug(
                        'Connection with id {} was created more than {} seconds ago. Releasing the connection and creating a new one.'
                        .format(id(conn), self.connection_refresh_time))
                    # Since calling disconnect() repeatedly is safe, we call disconnect()
                    # here explicitly, instead of relying on the garbage collector to clean
                    # up the object and call disconnect(). This makes the behavior of the
                    # code more predictable as we are not relying on when garbage collector is called
                    conn.disconnect()
                    conn = Connection(self, self.account)
                    logger.debug("Created new connection with id: {}".format(
                        id(conn)))
            except KeyError:
                conn = Connection(self, self.account)
                logger.debug(
                    "No connection found in idle set. Created a new connection with id: {}"
                    .format(id(conn)))

            self.active.add(conn)
            logger.debug("Adding connection with id {} to active set".format(
                id(conn)))

        logger.debug('num active: {}'.format(len(self.active)))
        logger.debug('num idle: {}'.format(len(self.idle)))
        return conn