-
Notifications
You must be signed in to change notification settings - Fork 158
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 connections recycling #373
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7aae914
Added optional parameter `recycle` for `create_engine` in aiopg.sa
soar c622443
Fix code style
soar 54eea1f
Added test for connection recycling
soar 0d006db
Use `loop.time()` instead of `time.time()`
soar cfc3b7e
One more `time.time()` to `loop.time()` change
soar ca36327
Remove unused import
soar 8f5deab
Rename parameter `recycle` to `pool_recycle` according to SQLAlchemy …
soar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import asyncio | ||
import collections | ||
import sys | ||
import time | ||
import warnings | ||
|
||
|
||
|
@@ -17,20 +18,21 @@ | |
|
||
|
||
def create_pool(dsn=None, *, minsize=1, maxsize=10, | ||
loop=None, timeout=TIMEOUT, | ||
loop=None, timeout=TIMEOUT, recycle=-1, | ||
enable_json=True, enable_hstore=True, enable_uuid=True, | ||
echo=False, on_connect=None, | ||
**kwargs): | ||
coro = _create_pool(dsn=dsn, minsize=minsize, maxsize=maxsize, loop=loop, | ||
timeout=timeout, enable_json=enable_json, | ||
enable_hstore=enable_hstore, enable_uuid=enable_uuid, | ||
echo=echo, on_connect=on_connect, **kwargs) | ||
timeout=timeout, recycle=recycle, | ||
enable_json=enable_json, enable_hstore=enable_hstore, | ||
enable_uuid=enable_uuid, echo=echo, | ||
on_connect=on_connect, **kwargs) | ||
return _PoolContextManager(coro) | ||
|
||
|
||
@asyncio.coroutine | ||
def _create_pool(dsn=None, *, minsize=1, maxsize=10, | ||
loop=None, timeout=TIMEOUT, | ||
loop=None, timeout=TIMEOUT, recycle=-1, | ||
enable_json=True, enable_hstore=True, enable_uuid=True, | ||
echo=False, on_connect=None, | ||
**kwargs): | ||
|
@@ -40,7 +42,7 @@ def _create_pool(dsn=None, *, minsize=1, maxsize=10, | |
pool = Pool(dsn, minsize, maxsize, loop, timeout, | ||
enable_json=enable_json, enable_hstore=enable_hstore, | ||
enable_uuid=enable_uuid, echo=echo, on_connect=on_connect, | ||
**kwargs) | ||
recycle=recycle, **kwargs) | ||
if minsize > 0: | ||
with (yield from pool._cond): | ||
yield from pool._fill_free_pool(False) | ||
|
@@ -52,7 +54,7 @@ class Pool(asyncio.AbstractServer): | |
|
||
def __init__(self, dsn, minsize, maxsize, loop, timeout, *, | ||
enable_json, enable_hstore, enable_uuid, echo, | ||
on_connect, **kwargs): | ||
on_connect, recycle, **kwargs): | ||
if minsize < 0: | ||
raise ValueError("minsize should be zero or greater") | ||
if maxsize < minsize and maxsize != 0: | ||
|
@@ -61,6 +63,7 @@ def __init__(self, dsn, minsize, maxsize, loop, timeout, *, | |
self._minsize = minsize | ||
self._loop = loop | ||
self._timeout = timeout | ||
self._recycle = recycle | ||
self._enable_json = enable_json | ||
self._enable_hstore = enable_hstore | ||
self._enable_uuid = enable_uuid | ||
|
@@ -187,6 +190,10 @@ def _fill_free_pool(self, override_min): | |
conn = self._free[-1] | ||
if conn.closed: | ||
self._free.pop() | ||
elif self._recycle > -1 \ | ||
and time.time() - conn.last_usage > self._recycle: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asvetlov Done |
||
conn.close() | ||
self._free.pop() | ||
else: | ||
self._free.rotate() | ||
n += 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this check a bit fragile, what will happened if someone pass
pool_recycle=-0.5
, I suggest change default and add parameter validation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've used code logic from SQLAlchemy for compatibility - see: https://github.com/zzzeek/sqlalchemy/blob/master/lib/sqlalchemy/pool.py#L623
But I also think, that
None
by default and comparisionself._recycle is not None and self._recycle > 0
will be better.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sense, lets leave
-1