Close postgresql connections when the thread pool thread is idle

I think the idle connections associated with idle threads are still taking up
memory, so especially now that you can configure an arbitrary number of
threads (and thus connections), I think it's good to close them regularly.
This commit is contained in:
Christopher Baines 2022-10-23 11:28:37 +01:00
parent aaec813cba
commit d06230fcf4
3 changed files with 52 additions and 2 deletions

View file

@ -27,6 +27,7 @@
with-postgresql-connection-per-thread
with-thread-postgresql-connection
close-thread-postgresql-connection
with-postgresql-transaction
@ -146,6 +147,15 @@
(f conn)))))
(define (close-thread-postgresql-connection)
(let ((conn (fluid-ref %thread-postgresql-connection)))
(when conn
(pg-conn-finish conn)
(hash-remove! (%postgresql-connections-hash-table)
(current-thread))
(fluid-set! %thread-postgresql-connection
conn))))
(define* (with-postgresql-transaction conn f
#:key always-rollback?)
(exec-query conn "BEGIN;")