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

Enforce for max_lifetime in pool.check() #482

Closed
dvarrazzo opened this issue Jan 11, 2023 · 1 comment
Closed

Enforce for max_lifetime in pool.check() #482

dvarrazzo opened this issue Jan 11, 2023 · 1 comment
Labels
good first issue Good for newcomers pool The issue is related to the psycopg_pool package

Comments

@dvarrazzo
Copy link
Member

I'm also thinking that check() might be a good moment to check if max_lifetime is expired, because it's already in the business of taking out all the connections from the pool and exercise them. This is much simpler and can be done in a bugfix for psycopg_pool 3.1. It would be up to the user to call pool.check() periodically.

Originally posted by @dvarrazzo in #247 (comment)

@dvarrazzo dvarrazzo changed the title Enforce for max_lifetime in poo.check() Enforce for max_lifetime in pool.check() Jan 11, 2023
@dvarrazzo
Copy link
Member Author

It should be as easy as (untested):

diff --git a/psycopg_pool/psycopg_pool/pool.py b/psycopg_pool/psycopg_pool/pool.py
index 609d95df..47c9561b 100644
--- a/psycopg_pool/psycopg_pool/pool.py
+++ b/psycopg_pool/psycopg_pool/pool.py
@@ -409,6 +409,13 @@ class ConnectionPool(BasePool[Connection[Any]]):
 
         while conns:
             conn = conns.pop()
+
+            if conn._expire_at <= monotonic():
+                logger.info("discarding expired connection %s", conn)
+                conn.close()
+                self.run_task(AddConnection(self))
+                continue
+
             try:
                 conn.execute("SELECT 1")
                 if conn.pgconn.transaction_status == TransactionStatus.INTRANS:

It needs the equivalent async, tests in the test suite, an entry in the changelog.

It seems simple enough if someone would like to try it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers pool The issue is related to the psycopg_pool package
Projects
None yet
Development

No branches or pull requests

1 participant