cbaines pushed a commit to branch master
in repository data-service.

commit 9fcd615c1f62de72474a348a0bc935e87adfaa67
Author: Christopher Baines <m...@cbaines.net>
AuthorDate: Mon Apr 1 22:20:00 2024 +0100

    Remove the per-thread PostgreSQL connection code
    
    As this has been replaced by a fibers resource pool.
---
 guix-data-service/database.scm | 72 +++---------------------------------------
 1 file changed, 5 insertions(+), 67 deletions(-)

diff --git a/guix-data-service/database.scm b/guix-data-service/database.scm
index 7270e90..0b5175a 100644
--- a/guix-data-service/database.scm
+++ b/guix-data-service/database.scm
@@ -29,10 +29,7 @@
 
             with-postgresql-connection
             open-postgresql-connection
-
-            with-postgresql-connection-per-thread
-            with-thread-postgresql-connection
-            close-thread-postgresql-connection
+            close-postgresql-connection
 
             with-postgresql-transaction
 
@@ -116,6 +113,10 @@
 
     conn))
 
+(define (close-postgresql-connection conn name)
+  (pg-conn-finish conn)
+  (decrement-connection-gauge name))
+
 (define (run-sqitch)
   (with-postgresql-connection
    "sqitch"
@@ -201,69 +202,6 @@
 (define %postgresql-connections-name
   (make-parameter #f))
 
-(define* (with-postgresql-connection-per-thread name thunk
-                                                #:key (statement-timeout #f))
-  (parameterize ((%postgresql-connection-parameters
-                  (list name statement-timeout))
-                 (%postgresql-connections-hash-table
-                  (make-hash-table))
-                 (%postgresql-connections-name
-                  name))
-    (call-with-values
-        thunk
-      (lambda vals
-        (hash-for-each
-         (lambda (thread conn)
-           (pg-conn-finish conn)
-           (decrement-connection-gauge name))
-         (%postgresql-connections-hash-table))
-
-        (apply values vals)))))
-
-(define %thread-postgresql-connection
-  (make-thread-local-fluid))
-
-(define (with-thread-postgresql-connection f)
-  (define (set-current-thread-connection conn)
-    (if conn
-        (hash-set! (%postgresql-connections-hash-table)
-                   (current-thread)
-                   conn)
-        (hash-remove! (%postgresql-connections-hash-table)
-                      (current-thread)))
-    (fluid-set! %thread-postgresql-connection
-                conn))
-
-  (let ((conn (fluid-ref %thread-postgresql-connection)))
-    (if conn
-        ;; Assume an exception here could mean the connection has failed, so
-        ;; close it
-        (with-exception-handler
-            (lambda (exn)
-              (pg-conn-finish conn)
-              (decrement-connection-gauge
-               (%postgresql-connections-name))
-              (set-current-thread-connection #f)
-              (raise-exception exn))
-          (lambda ()
-            (f conn)))
-
-        (let ((conn (apply open-postgresql-connection
-                           (%postgresql-connection-parameters))))
-          (set-current-thread-connection conn)
-
-          (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 #f)
-      (decrement-connection-gauge
-       (%postgresql-connections-name)))))
-
 (define* (with-postgresql-transaction conn f
                                       #:key always-rollback?)
   (exec-query conn "BEGIN;")

Reply via email to