Skip to content

Commit 75d3466

Browse files
authored
pool: Decrement ._opened_conns when connect() raises exception (#9)
2 parents 293950f + 8828460 commit 75d3466

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
@@ -54,7 +54,7 @@ def stat(self):
5454
"""Returns (opened connections, free connections, waiters)"""
5555
return (self._opened_conns, len(self._free_conn), len(self._waitings))
5656

57-
def _get_conn(self):
57+
def _get_conn(self): # -> Future[connection]
5858
now = self.io_loop.time()
5959

6060
# Try to reuse in free pool
@@ -72,13 +72,19 @@ def _get_conn(self):
7272
if self.max_open == 0 or self._opened_conns < self.max_open:
7373
self._opened_conns += 1
7474
log.debug("Creating new connection: %s", self.stat())
75-
return connect(**self.connect_kwargs)
75+
fut = connect(**self.connect_kwargs)
76+
fut.add_done_callback(self._on_connect) # self._opened_conns -=1 on exception
77+
return fut
7678

7779
# Wait to other connection is released.
7880
fut = Future()
7981
self._waitings.append(fut)
8082
return fut
8183

84+
def _on_connect(self, fut):
85+
if fut.exception():
86+
self._opened_conns -= 1
87+
8288
def _put_conn(self, conn):
8389
if (len(self._free_conn) < self.max_idle and
8490
self.io_loop.time() - conn.connected_time < self.max_recycle_sec):

0 commit comments

Comments
 (0)