Skip to content

pool: Decrement ._opened_conns when connect() raises exception #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions tornado_mysql/pools.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def stat(self):
"""Returns (opened connections, free connections, waiters)"""
return (self._opened_conns, len(self._free_conn), len(self._waitings))

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

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

# Wait to other connection is released.
fut = Future()
self._waitings.append(fut)
return fut

def _on_connect(self, fut):
if fut.exception():
self._opened_conns -= 1

def _put_conn(self, conn):
if (len(self._free_conn) < self.max_idle and
self.io_loop.time() - conn.connected_time < self.max_recycle_sec):
Expand Down