Permalink
Browse files

Merge pull request #5 from aleksandr-rakov/master

fix the empty slot
  • Loading branch information...
2 parents c99bf86 + bf14f28 commit 69fe0f798f6fd1928c86c3939ffa5b4a130b737f @tarekziade tarekziade committed Nov 17, 2012
Showing with 19 additions and 8 deletions.
  1. +1 −1 memcachepool/cache.py
  2. +18 −7 memcachepool/pool.py
@@ -35,7 +35,7 @@ def __init__(self, server, params):
self.socktimeout = int(params.get('SOCKET_TIMEOUT', 4))
self.max_item_size = long(params.get('MAX_ITEM_SIZE',
DEFAULT_ITEM_SIZE))
- self._pool = ClientPool(self._get_client, maxsize=self.maxsize)
+ self._pool = ClientPool(self._get_client, maxsize=self.maxsize,wait_for_connection=self.socktimeout)
self._blacklist = {}
# XXX using python-memcached style pickling
View
@@ -11,11 +11,12 @@
class ClientPool(object):
- def __init__(self, factory, maxsize=None, timeout=60):
+ def __init__(self, factory, maxsize=None, timeout=60,wait_for_connection=None):
self.factory = factory
self.maxsize = maxsize
self.timeout = timeout
self.clients = Queue.PriorityQueue(maxsize)
+ self.wait_for_connection=wait_for_connection
# If there is a maxsize, prime the queue with empty slots.
if maxsize is not None:
for _ in xrange(maxsize):
@@ -36,17 +37,27 @@ def _checkout_connection(self):
# Loop until we get a non-stale connection, or we create a new one.
while True:
try:
- ts, client = self.clients.get(blocking)
+ ts, client = self.clients.get(blocking,self.wait_for_connection)
except Queue.Empty:
- # No maxsize and no free connections, create a new one.
- # XXX TODO: we should be using a monotonic clock here.
- now = int(time.time())
- return now, self.factory()
+ if blocking:
+ #timeout
+ raise Exception("No connections available in the pool")
+ else:
+ # No maxsize and no free connections, create a new one.
+ # XXX TODO: we should be using a monotonic clock here.
+ now = int(time.time())
+ return now, self.factory()
else:
now = int(time.time())
# If we got an empty slot placeholder, create a new connection.
if client is None:
- return now, self.factory()
+ try:
+ return now, self.factory()
+ except Exception,e:
+ if self.maxsize is not None:
+ #return slot to queue
+ self.clients.put(EMPTY_SLOT)
+ raise e
# If the connection is not stale, go ahead and use it.
if ts + self.timeout > now:
return ts, client

0 comments on commit 69fe0f7

Please sign in to comment.