URL: http://github.com/MagicStack/asyncpg/pull/482.patch
handler=handler) + return middleware_factory + + def create_pool(dsn=None, *, min_size=10, max_size=10, @@ -791,6 +818,7 @@ def create_pool(dsn=None, *, max_inactive_connection_lifetime=300.0, setup=None, init=None, + middlewares=None, loop=None, connection_class=connection.Connection, **connect_kwargs): @@ -866,6 +894,23 @@ def create_pool(dsn=None, *, or :meth:`Connection.set_type_codec() <\ asyncpg.connection.Connection.set_type_codec>`. + :param middlewares: + A list of middleware functions to be middleware just + before a connection excecutes a statement. + Syntax of a middleware is as follows: + + .. code-block:: python + + async def middleware_factory(connection, handler): + async def middleware(query, args, limit, + timeout, return_status): + print('do something before') + result, stmt = await handler(query, args, limit, + timeout, return_status) + print('do something after') + return result, stmt + return middleware + :param loop: An asyncio event loop instance. If ``None``, the default event loop will be used. @@ -893,6 +938,7 @@ def create_pool(dsn=None, *, dsn, connection_class=connection_class, min_size=min_size, max_size=max_size, - max_queries=max_queries, loop=loop, setup=setup, init=init, + max_queries=max_queries, loop=loop, setup=setup, + middlewares=middlewares, init=init, max_inactive_connection_lifetime=max_inactive_connection_lifetime, **connect_kwargs) diff --git a/docs/installation.rst b/docs/installation.rst index 6d9ec2ef..e9b9c344 100644 --- a/docs/installation.rst +++ b/docs/installation.rst @@ -30,6 +30,7 @@ If you want to build **asyncpg** from a Git checkout you will need: * CPython header files. These can usually be obtained by installing the relevant Python development package: **python3-dev** on Debian/Ubuntu, **python3-devel** on RHEL/Fedora. + * Clone the repo with submodules (`git clone --recursive`, or `git submodules init; git submodules update`) Once the above requirements are satisfied, run the following command in the root of the source checkout: diff --git a/tests/test_pool.py b/tests/test_pool.py index e51923e4..36bf312b 100644 --- a/tests/test_pool.py +++ b/tests/test_pool.py @@ -76,6 +76,48 @@ async def worker(): tasks = [worker() for _ in range(n)] await asyncio.gather(*tasks) + async def test_pool_with_middleware(self): + called = False + + async def my_middleware_factory(connection, handler): + async def middleware(query, args, limit, timeout, return_status): + nonlocal called + called = True + return await handler(query, args, limit, + timeout, return_status) + return middleware + + pool = await self.create_pool(database='postgres', + min_size=1, max_size=1, + middlewares=[my_middleware_factory]) + + con = await pool.acquire(timeout=5) + await con.fetchval('SELECT 1') + assert called + + pool.terminate() + del con + + async def test_pool_with_middleware_decorator(self): + called = False + + @pg_pool.middleware + async def my_middleware(query, args, limit, timeout, return_status, + *, connection, handler): + nonlocal called + called = True + return await handler(query, args, limit, + timeout, return_status) + + pool = await self.create_pool(database='postgres', min_size=1, + max_size=1, middlewares=[my_middleware]) + con = await pool.acquire(timeout=5) + await con.fetchval('SELECT 1') + assert called + + pool.terminate() + del con + async def test_pool_03(self): pool = await self.create_pool(database='postgres', min_size=1, max_size=1)Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: