Skip to content
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

Timeout on aquiring connection from pool #218

Closed
Show file tree
Hide file tree
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
33 changes: 18 additions & 15 deletions aiopg/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings


import async_timeout
from psycopg2.extensions import TRANSACTION_STATUS_IDLE

from .connection import connect, TIMEOUT
Expand Down Expand Up @@ -163,21 +164,23 @@ def acquire(self):

@asyncio.coroutine
def _acquire(self):
if self._closing:
raise RuntimeError("Cannot acquire connection after closing pool")
with (yield from self._cond):
while True:
yield from self._fill_free_pool(True)
if self._free:
conn = self._free.popleft()
assert not conn.closed, conn
assert conn not in self._used, (conn, self._used)
self._used.add(conn)
if self._on_connect is not None:
yield from self._on_connect(conn)
return conn
else:
yield from self._cond.wait()
with async_timeout.timeout(self._timeout, loop=self._loop):
if self._closing:
raise RuntimeError(
"Cannot acquire connection after closing pool")
with (yield from self._cond):
while True:
yield from self._fill_free_pool(True)
if self._free:
conn = self._free.popleft()
assert not conn.closed, conn
assert conn not in self._used, (conn, self._used)
self._used.add(conn)
if self._on_connect is not None:
yield from self._on_connect(conn)
return conn
else:
yield from self._cond.wait()

@asyncio.coroutine
def _fill_free_pool(self, override_min):
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
from setuptools import setup


install_requires = ['psycopg2>=2.5.2']
install_requires = [
'psycopg2>=2.5.2',
'async-timeout==1.1.0'
]

PY_VER = sys.version_info

Expand Down