From 79ebe621f882aa6a793f13351d73bb13f5a312de Mon Sep 17 00:00:00 2001 From: Alexander Makarov Date: Fri, 3 Jun 2016 15:03:02 +0300 Subject: [PATCH] Handle empty memcache pool corner case Change-Id: I8053d25a08fb630f38a22292c5f7c153c7cad4b3 Closes-Bug: #1523379 --- oslo_cache/_memcache_pool.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/oslo_cache/_memcache_pool.py b/oslo_cache/_memcache_pool.py index 0226cf9b..45af9a9f 100644 --- a/oslo_cache/_memcache_pool.py +++ b/oslo_cache/_memcache_pool.py @@ -160,20 +160,28 @@ class ConnectionPool(queue.Queue): qsize = _qsize def _get(self): - if self.queue: + try: conn = self.queue.pop().connection - else: + except IndexError: conn = self._create_connection() self._acquired += 1 return conn def _drop_expired_connections(self): - """Drop all expired connections from the right end of the queue.""" + """Drop all expired connections from the left end of the queue.""" now = time.time() - while self.queue and self.queue[0].ttl < now: - conn = self.queue.popleft().connection - self._trace_logger('Reaping connection %s', id(conn)) - self._destroy_connection(conn) + try: + while self.queue[0].ttl < now: + conn = self.queue.popleft().connection + self._trace_logger('Reaping connection %s', id(conn)) + self._destroy_connection(conn) + except IndexError: + # NOTE(amakarov): This is an expected excepton. so there's no + # need to react. We have to handle exceptions instead of + # checking queue length as IndexError is a result of race + # condition too as well as of mere queue depletio of mere queue + # depletionn. + pass def _put(self, conn): self.queue.append(_PoolItem(