Skip to content

Commit ef46e26

Browse files
authored
Merge pull request #427 from wwwjfy/init-args
fix #407, extensions accept kwargs for db init
2 parents dc1dfdd + 31ef463 commit ef46e26

File tree

11 files changed

+55
-2
lines changed

11 files changed

+55
-2
lines changed

docs/aiohttp.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
===============
2+
Aiohttp Support
3+
===============
4+
5+
**THIS IS A WIP**

docs/quart.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
===============
2+
Quart Support
3+
===============
4+
5+
**THIS IS A WIP**

docs/sanic.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Supported configurations:
2626
- DB_POOL_MIN_SIZE
2727
- DB_POOL_MAX_SIZE
2828
- DB_USE_CONNECTION_FOR_REQUEST
29+
- DB_KWARGS
2930

3031
An example server:
3132

@@ -79,6 +80,7 @@ To integrate with Sanic, a few configurations needs to be set in
7980
- DB_ECHO: if not set, ``False``
8081
- DB_POOL_MIN_SIZE: if not set, 5
8182
- DB_POOL_MAX_SIZE: if not set, 10
83+
- DB_KWARGS; if not set, empty dictionary
8284

8385
An example:
8486

gino/ext/aiohttp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ class Gino(_Gino):
8383
* ``pool_max_size`` - the maximum number of connections in the db pool.
8484
* ``echo`` - enable SQLAlchemy echo mode.
8585
* ``ssl`` - SSL context passed to ``asyncpg.connect``, default is ``None``.
86+
* ``kwargs`` - other parameters passed to the specified dialects,
87+
like ``asyncpg``. Unrecognized parameters will cause exceptions.
8688
8789
If the ``db`` is set as an aiohttp middleware, then a lazy connection is
8890
available at ``request['connection']``. By default, a database connection
@@ -132,6 +134,7 @@ async def before_server_start(app_):
132134
max_size=config.setdefault('pool_max_size', 10),
133135
ssl=config.setdefault('ssl'),
134136
loop=app_.loop,
137+
**config.setdefault('kwargs', dict()),
135138
)
136139

137140
async def after_server_stop(app_):

gino/ext/quart.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ async def before_first_request():
116116
max_size=app.config.setdefault('DB_POOL_MAX_SIZE', 10),
117117
ssl=app.config.setdefault('DB_SSL'),
118118
loop=asyncio.get_event_loop(),
119+
**app.config.setdefault('DB_KWARGS', dict()),
119120
)
120121

121122
async def first_or_404(self, *args, **kwargs):

gino/ext/sanic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ async def before_server_start(_, loop):
113113
max_size=app.config.setdefault('DB_POOL_MAX_SIZE', 10),
114114
ssl=app.config.setdefault('DB_SSL'),
115115
loop=loop,
116+
**app.config.setdefault('DB_KWARGS', dict()),
116117
)
117118

118119
@app.listener('after_server_stop')

gino/ext/tornado.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ async def init_app(self, app, *, loop=None, dsn='', driver='asyncpg',
203203
host='localhost', port=5432,
204204
user='postgres', password='', database='postgres',
205205
echo=False, pool_min_size=5, pool_max_size=10,
206-
ssl=None):
206+
ssl=None, **kwargs):
207207
"""
208208
Initialize database
209209
@@ -225,6 +225,8 @@ async def init_app(self, app, *, loop=None, dsn='', driver='asyncpg',
225225
pool, default is ``10``.
226226
:param ssl: SSL context passed to ``asyncpg.connect``, default is
227227
``None``. This can be ``True`` or an instance of ``ssl.SSLContext``.
228+
:param kwargs: other parameters passed to the specified dialects,
229+
like ``asyncpg``. Unrecognized parameters will cause exceptions.
228230
"""
229231
if loop is None:
230232
loop = tornado.ioloop.IOLoop.current()
@@ -243,7 +245,7 @@ async def init_app(self, app, *, loop=None, dsn='', driver='asyncpg',
243245

244246
await self.set_bind(
245247
dsn, echo=echo, min_size=pool_min_size, max_size=pool_max_size,
246-
ssl=ssl, loop=asyncio_loop,
248+
ssl=ssl, loop=asyncio_loop, **kwargs,
247249
)
248250

249251
app.db = self

tests/test_aiohttp.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .models import DB_ARGS, PG_URL
88

99
pytestmark = pytest.mark.asyncio
10+
_MAX_INACTIVE_CONNECTION_LIFETIME = 59.0
1011

1112

1213
# noinspection PyShadowingNames
@@ -15,6 +16,11 @@ async def _test_client(config):
1516

1617
db = Gino()
1718
app = web.Application(middlewares=[db])
19+
config.update({
20+
'kwargs': dict(
21+
max_inactive_connection_lifetime=_MAX_INACTIVE_CONNECTION_LIFETIME,
22+
),
23+
})
1824
app['config'] = dict(gino=config)
1925
db.init_app(app)
2026

@@ -28,6 +34,10 @@ class User(db.Model):
2834

2935
@routes.get('/')
3036
async def root(request):
37+
conn = await request['connection'].get_raw_connection()
38+
# noinspection PyProtectedMember
39+
assert conn._holder._max_inactive_time == \
40+
_MAX_INACTIVE_CONNECTION_LIFETIME
3141
return web.Response(text='Hello, world!')
3242

3343
@routes.get('/users/{uid}')

tests/test_quart.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
from .models import DB_ARGS, PG_URL
1919

2020
pytestmark = pytest.mark.asyncio
21+
_MAX_INACTIVE_CONNECTION_LIFETIME = 59.0
2122

2223

2324
# noinspection PyShadowingNames
2425
async def _app(config):
2526
app = Quart(__name__)
2627
app.config.update(config)
28+
app.config.update({
29+
'DB_KWARGS': dict(
30+
max_inactive_connection_lifetime=_MAX_INACTIVE_CONNECTION_LIFETIME,
31+
),
32+
})
2733

2834
db = Gino(app)
2935

@@ -35,6 +41,10 @@ class User(db.Model):
3541

3642
@app.route('/')
3743
async def root():
44+
conn = await request.connection.get_raw_connection()
45+
# noinspection PyProtectedMember
46+
assert conn._holder._max_inactive_time == \
47+
_MAX_INACTIVE_CONNECTION_LIFETIME
3848
return 'Hello, world!'
3949

4050
async def _get_user(ctx, uid: int, method: str) -> dict:

tests/test_sanic.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,18 @@
88

99
from .models import DB_ARGS, PG_URL
1010

11+
_MAX_INACTIVE_CONNECTION_LIFETIME = 59.0
12+
1113

1214
# noinspection PyShadowingNames
1315
async def _app(config):
1416
app = sanic.Sanic()
1517
app.config.update(config)
18+
app.config.update({
19+
'DB_KWARGS': dict(
20+
max_inactive_connection_lifetime=_MAX_INACTIVE_CONNECTION_LIFETIME,
21+
),
22+
})
1623

1724
db = Gino(app)
1825

@@ -24,6 +31,10 @@ class User(db.Model):
2431

2532
@app.route('/')
2633
async def root(request):
34+
conn = await request['connection'].get_raw_connection()
35+
# noinspection PyProtectedMember
36+
assert conn._holder._max_inactive_time == \
37+
_MAX_INACTIVE_CONNECTION_LIFETIME
2738
return text('Hello, world!')
2839

2940
@app.route('/users/<uid:int>')

0 commit comments

Comments
 (0)