diff --git a/tornado_mysql/pools.py b/tornado_mysql/pools.py index bb3e721..0a42004 100644 --- a/tornado_mysql/pools.py +++ b/tornado_mysql/pools.py @@ -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 @@ -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):