class ConnectionCache(object):
    def __init__(self, max_per_cache=None):
        if None == max_per_cache:
            max_per_cache = MAX_PER_CACHE_DEFAULT
        self.size = 0
        self.max_per_cache = max_per_cache
        self.cache = LIFOQueue(maxsize=self.max_per_cache)

    def get_connection(self):
        try:
            return self.cache.get_nowait()
        except Queue.Empty:
            logger.warn("ConnectionCache queue empty, size=%d, qsize=%d" %
                        (self.size, self.cache.qsize()))
            pass
        # I chose not to lock here. Max is advisory, if two threads
        # eagerly await a connection near max, I say allow them both
        # to make one
        if self.size < self.max_per_cache:
            self.size += 1
            return self._make_connection()
        else:
            logger.warn("ConnectionCache queue over, size=%d, qsize=%d" %
                        (self.size, self.cache.qsize()))
        try:
            return self.cache.get(True, MAX_WAIT)
        except Queue.Empty:
            # ERROR: Should log this!
            logger.error(
                "ConnectionCache waited more than %d seconds, size=%d, qsize=%d"
                % (MAX_WAIT, self.size, self.cache.qsize()))
            pass
        if self.size > INF_WAIT_MAX_CONNECTIONS:
            logger.warn(
                "ConnectionCache wait forever, size=%d, max_connections=%d, qsize=%d"
                % (self.size, INF_WAIT_MAX_CONNECTIONS, self.cache.qsize()))
            return self.cache.get()
        self.size += 1
        logger.warn(
            "ConnectionCache wait inf, size=%d, max_connections=%d, qsize=%d" %
            (self.size, INF_WAIT_MAX_CONNECTIONS, self.cache.qsize()))
        return self._make_connection()

    def destroy_connection(self, c):
        c.c.close()
        self.size -= 1

    def put_connection(self, c):
        self.cache.put(c)
Example #2
0
class ConnectionCache(object):
    def __init__(self, max=15):
        self.size = 0
        self.max = max
        self.cache = LIFOQueue(maxsize=self.max)

    def get_connection(self):

        if self.size > max_connections:
            # ERROR: Should log this!
            #sys.stderr.write("ConnectionCache queue exceeds %d (%d)\n" %
            #                 (max_connections, self.cache.qsize()))
            pass

        try:
            return self.cache.get_nowait()
        except Queue.Empty:
            pass

        # I chose not to lock here. Max is advisory, if two threads
        # eagerly await a connection near max, I say allow them both
        # to make one
        if self.size < self.max:
            self.size += 1
            return self._make_connection()

        try:
            return self.cache.get(True, max_wait)
        except Queue.Empty:
            # ERROR: Should log this!
            #sys.stderr.write("ConnectionCache waited more than "
            #                 "%d seconds for one of %d connections!\n" %
            #                 (max_wait, self.size))
            pass

        if self.size > inf_wait_max_connections:
            return self.cache.get()

        self.size += 1
        return self._make_connection()

    def put_connection(self, c):
        self.cache.put(c)
class ConnectionCache(object):
    def __init__(self, max=15):
        self.size = 0
        self.max = max
        self.cache = LIFOQueue(maxsize = self.max)

    def get_connection(self):

        if self.size > max_connections:
            # ERROR: Should log this!
            #sys.stderr.write("ConnectionCache queue exceeds %d (%d)\n" %
            #                 (max_connections, self.cache.qsize()))
            pass

        try:
            return self.cache.get_nowait()
        except Queue.Empty:
            pass

        # I chose not to lock here. Max is advisory, if two threads
        # eagerly await a connection near max, I say allow them both
        # to make one
        if self.size < self.max:
            self.size += 1
            return self._make_connection()

        try:
            return self.cache.get(True, max_wait)
        except Queue.Empty:
            # ERROR: Should log this!
            #sys.stderr.write("ConnectionCache waited more than "
            #                 "%d seconds for one of %d connections!\n" %
            #                 (max_wait, self.size))
            pass

        if self.size > inf_wait_max_connections:
            return self.cache.get()

        self.size += 1
        return self._make_connection()

    def put_connection(self, c):
        self.cache.put(c)
Example #4
0
class ConnectionCache(object):
    def __init__(self, max_per_cache=None):
        if None == max_per_cache:
            max_per_cache = MAX_PER_CACHE_DEFAULT
        self.size = 0
        self.max_per_cache = max_per_cache
        self.cache = LIFOQueue(maxsize = self.max_per_cache)

    def get_connection(self):
        try:
            return self.cache.get_nowait()
        except Queue.Empty:
            logger.warn("ConnectionCache queue empty, size=%d, qsize=%d" % (self.size, self.cache.qsize()))
            pass
        # I chose not to lock here. Max is advisory, if two threads
        # eagerly await a connection near max, I say allow them both
        # to make one
        if self.size < self.max_per_cache:
            self.size += 1
            return self._make_connection()
        else:
            logger.warn("ConnectionCache queue over, size=%d, qsize=%d" % (self.size, self.cache.qsize()))
        try:
            return self.cache.get(True, MAX_WAIT)
        except Queue.Empty:
            # ERROR: Should log this!
            logger.error("ConnectionCache waited more than %d seconds, size=%d, qsize=%d" % (MAX_WAIT, self.size, self.cache.qsize()))
            pass
        if self.size > INF_WAIT_MAX_CONNECTIONS:
            logger.warn("ConnectionCache wait forever, size=%d, max_connections=%d, qsize=%d" % (self.size, INF_WAIT_MAX_CONNECTIONS, self.cache.qsize()))
            return self.cache.get()
        self.size += 1
        logger.warn("ConnectionCache wait inf, size=%d, max_connections=%d, qsize=%d" % (self.size, INF_WAIT_MAX_CONNECTIONS, self.cache.qsize()))
        return self._make_connection()

    def destroy_connection(self, c):
        c.c.close()
        self.size -= 1

    def put_connection(self, c):
        self.cache.put(c)
 def __init__(self, max_per_cache=None):
     if None == max_per_cache:
         max_per_cache = MAX_PER_CACHE_DEFAULT
     self.size = 0
     self.max_per_cache = max_per_cache
     self.cache = LIFOQueue(maxsize=self.max_per_cache)
Example #6
0
 def __init__(self, max=15):
     self.size = 0
     self.max = max
     self.cache = LIFOQueue(maxsize=self.max)
 def __init__(self, max=15):
     self.size = 0
     self.max = max
     self.cache = LIFOQueue(maxsize = self.max)
Example #8
0
 def __init__(self, max_per_cache=None):
     if None == max_per_cache:
         max_per_cache = MAX_PER_CACHE_DEFAULT
     self.size = 0
     self.max_per_cache = max_per_cache
     self.cache = LIFOQueue(maxsize = self.max_per_cache)