Skip to content

Commit

Permalink
Reduce critical section even more. Use lock only to update counters.
Browse files Browse the repository at this point in the history
  • Loading branch information
edevil committed Feb 13, 2013
1 parent 003ddb6 commit 37f2c7a
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions pycassa/pool.py
Expand Up @@ -512,6 +512,30 @@ def _put_conn(self, conn):
self._q.put_nowait(conn)
return conn

def _new_if_required(self, max_conns):
""" Creates new connection if there is room """
try:
self._pool_lock.acquire()
if self._current_conns < max_conns:
new_conn = True
self._current_conns += 1
else:
new_conn = False
finally:
self._pool_lock.release()

if new_conn:
try:
return self._create_connection()
except:
try:
self._pool_lock.acquire()
self._current_conns -= 1
raise
finally:
self._pool_lock.release()
return None

def get(self):
""" Gets a connection from the pool. """
conn = None
Expand All @@ -523,15 +547,8 @@ def get(self):
return conn
except AttributeError:
pass
try:
self._pool_lock.acquire()
if self._current_conns < self._pool_size:
# The pool was not prefilled, and we need to add connections to reach pool_size
conn = self._create_connection()
self._current_conns += 1
finally:
self._pool_lock.release()

conn = self._new_if_required(self._pool_size)
if not conn:
try:
# We don't want to waste time blocking if overflow is not enabled; similarly,
Expand All @@ -549,14 +566,7 @@ def get(self):
conn = self._q.get(block, timeout)
conn._checkout()
except Queue.Empty:
try:
self._pool_lock.acquire()
if self._current_conns < self._max_conns:
conn = self._create_connection()
self._current_conns += 1
finally:
self._pool_lock.release()

conn = self._new_if_required(self._max_conns)
if not conn:
self._notify_on_pool_max(pool_max=self._max_conns)
size_msg = "size %d" % (self._pool_size, )
Expand Down

0 comments on commit 37f2c7a

Please sign in to comment.