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

Pool on_connect callback called on every acquire #551

Closed
aaliddell opened this issue Apr 1, 2019 · 1 comment · Fixed by #552
Closed

Pool on_connect callback called on every acquire #551

aaliddell opened this issue Apr 1, 2019 · 1 comment · Fixed by #552

Comments

@aaliddell
Copy link
Contributor

When using an on_connect callback, it appears to get called every time a connection is aquired, rather than every time a new connection is made, which appears to differ from the stated documentation:

on_connect – a callback coroutine executed at once for every created connection. May be used for setting up connection level state like client encoding etc.

Although it looks like it was intended to put one of 'least' or 'most' between the 'at' and 'once'?

This is due to the callback check being made in the acquire code:

aiopg/aiopg/pool.py

Lines 165 to 180 in 9fdf7b9

async def _acquire(self):
if self._closing:
raise RuntimeError("Cannot acquire connection after closing pool")
async with self._cond:
while True:
await 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:
await self._on_connect(conn)
return conn
else:
await self._cond.wait()

Should this instead be located after the new connection is made in _fill_free_pool: https://github.com/aio-libs/aiopg/blob/master/aiopg/pool.py#L199

The test case here currently doesn't catch this, as it only checks the call is made once. Perhaps it should instead check that when reusing a connection, the on_connect callback isn't called multiple times, maybe by using a pool with minsize = maxsize = 1 and acquiring the connection twice?

@aio-libs-bot
Copy link

GitMate.io thinks possibly related issues are #141 (Add an on_connect callback parameter to pool), #16 (Add timeout for acquiring from pool), #130 (Implement timeout on acquiring connection from pool), #113 (Can't acquire connection from pool), and #335 (pool.acquire() function is not thread safe).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants