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

release connections of multiple dbs #16

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,17 @@ The config includes:
## Lazy Connection

If `use_connection_for_request` is set to be True, then a lazy connection is available
at `request['connection']`. By default, a database connection is borrowed on the first
at `request['connections']`. By default, a database connection is borrowed on the first
query, shared in the same execution context, and returned to the pool on response.
If you need to release the connection early in the middle to do some long-running tasks,
you can simply do this:

```python
await request['connection'].release(permanent=False)
await request['connections'][0].release(permanent=False)
```

Or iterate it if you have multiple databases like:

```python
[await conn.release(permanent=False) for conn in request['connections']]
```
18 changes: 13 additions & 5 deletions src/gino_starlette.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,17 @@ async def __call__(
self, scope: Scope, receive: Receive, send: Send
) -> None:
if scope["type"] == "http" and self._conn_for_req:
scope["connection"] = await self.db.acquire(lazy=True)
conn = await self.db.acquire(lazy=True)

if "connections" not in scope:
scope["connections"] = []
scope["connections"].append(conn)

try:
await self.app(scope, receive, send)
finally:
conn = scope.pop("connection", None)
if conn is not None:
conns = scope.pop("connections", [])
for conn in conns:
await conn.release()
return

Expand Down Expand Up @@ -123,14 +128,17 @@ class Gino(_Gino):
like ``asyncpg``. Unrecognized parameters will cause exceptions.

If ``use_connection_for_request`` is set to be True, then a lazy connection
is available at ``request['connection']``. By default, a database
is available in ``request['connections']``. By default, a database
connection is borrowed on the first query, shared in the same execution
context, and returned to the pool on response. If you need to release the
connection early in the middle to do some long-running tasks, you can
simply do this::

await request['connection'].release(permanent=False)
await request['connections'][0].release(permanent=False)

Or iterate it if you have multiple databases like:

[await conn.release(permanent=False) for conn in request['connections']]
"""

model_base_classes = _Gino.model_base_classes + (StarletteModelMixin,)
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class User(db.Model):

@app.route("/")
async def root(request):
conn = await request["connection"].get_raw_connection()
conn = await request["connections"][0].get_raw_connection()
# noinspection PyProtectedMember
assert (
conn._holder._max_inactive_time == _MAX_INACTIVE_CONNECTION_LIFETIME
Expand All @@ -192,7 +192,7 @@ async def get_user(request):
return JSONResponse((await q.gino.first_or_404()).to_dict())
elif method == "2":
return JSONResponse(
(await request["connection"].first_or_404(q)).to_dict()
(await request["connections"][0].first_or_404(q)).to_dict()
)
elif method == "3":
return JSONResponse((await db.bind.first_or_404(q)).to_dict())
Expand All @@ -207,7 +207,7 @@ async def add_user(request):
await u.query.gino.first_or_404()
await db.first_or_404(u.query)
await db.bind.first_or_404(u.query)
await request["connection"].first_or_404(u.query)
await request["connections"][0].first_or_404(u.query)
return JSONResponse(u.to_dict())

e = await gino.create_engine(PG_URL)
Expand Down