Skip to content

Commit e19a81e

Browse files
committedMay 26, 2015
Decrement Pool._opened_conns when connect() raises exception
1 parent 58bf9f3 commit e19a81e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed
 

‎tornado_mysql/pools.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def stat(self):
5858
"""Returns (opened connections, free connections, waiters)"""
5959
return (self._opened_conns, len(self._free_conn), len(self._waitings))
6060

61-
def _get_conn(self):
61+
def _get_conn(self): # -> Future[connection]
6262
now = self.io_loop.time()
6363

6464
# Try to reuse in free pool
@@ -76,13 +76,19 @@ def _get_conn(self):
7676
if self.max_open == 0 or self._opened_conns < self.max_open:
7777
self._opened_conns += 1
7878
_debug("Creating new connection:", self.stat())
79-
return connect(**self.connect_kwargs)
79+
fut = connect(**self.connect_kwargs)
80+
fut.add_done_callback(self._on_connect) # self._opened_conns -=1 on exception
81+
return fut
8082

8183
# Wait to other connection is released.
8284
fut = Future()
8385
self._waitings.append(fut)
8486
return fut
8587

88+
def _on_connect(self, fut):
89+
if fut.exception():
90+
self._opened_conns -= 1
91+
8692
def _put_conn(self, conn):
8793
if (len(self._free_conn) < self.max_idle and
8894
self.io_loop.time() - conn.connected_time < self.max_recycle_sec):

0 commit comments

Comments
 (0)