[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
03/06: Stop opening a PostgreSQL connection per request
From: |
Christopher Baines |
Subject: |
03/06: Stop opening a PostgreSQL connection per request |
Date: |
Sat, 3 Oct 2020 16:43:18 -0400 (EDT) |
cbaines pushed a commit to branch master
in repository data-service.
commit 18b6dd9e6d4463e47ce457187d956c1c3dd8dd08
Author: Christopher Baines <mail@cbaines.net>
AuthorDate: Sat Oct 3 09:22:29 2020 +0100
Stop opening a PostgreSQL connection per request
This was good in that it avoided having to deal with long running
connections,
but it probably takes some time to open the connection, and these changes
are
a step towards offloading the PostgreSQL queries to other threads, so they
don't block the threads for fibers.
---
guix-data-service/web/controller.scm | 9 +++------
guix-data-service/web/server.scm | 13 ++++---------
scripts/guix-data-service.in | 14 +++++++++-----
3 files changed, 16 insertions(+), 20 deletions(-)
diff --git a/guix-data-service/web/controller.scm
b/guix-data-service/web/controller.scm
index 4cf1f82..a8a8696 100644
--- a/guix-data-service/web/controller.scm
+++ b/guix-data-service/web/controller.scm
@@ -391,8 +391,7 @@
(define* (controller request method-and-path-components
mime-types body
- secret-key-base
- #:key postgresql-statement-timeout)
+ secret-key-base)
(define (controller-thunk)
(match method-and-path-components
(('GET "assets" rest ...)
@@ -430,16 +429,14 @@
"The README.html file does not exist")
#:code 404))))
(_
- (with-postgresql-connection
- "web"
+ (with-thread-postgresql-connection
(lambda (conn)
(controller-with-database-connection request
method-and-path-components
mime-types
body
conn
- secret-key-base))
- #:statement-timeout postgresql-statement-timeout))))
+ secret-key-base))))))
(call-with-error-handling
controller-thunk
#:on-error 'backtrace
diff --git a/guix-data-service/web/server.scm b/guix-data-service/web/server.scm
index 8f0ce56..05c0a58 100644
--- a/guix-data-service/web/server.scm
+++ b/guix-data-service/web/server.scm
@@ -29,8 +29,7 @@
#:use-module (guix-data-service web util)
#:export (start-guix-data-service-web-server))
-(define (handler request body controller secret-key-base
- postgresql-statement-timeout)
+(define (handler request body controller secret-key-base)
(display
(format #f "~a ~a\n"
(request-method request)
@@ -43,18 +42,14 @@
request-components)
mime-types
body
- secret-key-base
- #:postgresql-statement-timeout
- postgresql-statement-timeout))))
+ secret-key-base))))
-(define* (start-guix-data-service-web-server port host secret-key-base
- #:key
postgresql-statement-timeout)
+(define* (start-guix-data-service-web-server port host secret-key-base)
(call-with-error-handling
(lambda ()
(run-server (lambda (request body)
(handler request body controller
- secret-key-base
- postgresql-statement-timeout))
+ secret-key-base))
#:host host
#:port port))
#:on-error 'backtrace
diff --git a/scripts/guix-data-service.in b/scripts/guix-data-service.in
index 70274d0..d3ed0d8 100644
--- a/scripts/guix-data-service.in
+++ b/scripts/guix-data-service.in
@@ -31,6 +31,7 @@
(gcrypt pk-crypto)
(guix pki)
(guix-data-service config)
+ (guix-data-service database)
(guix-data-service web server)
(guix-data-service web controller)
(guix-data-service web nar controller))
@@ -194,9 +195,12 @@
(assq-ref opts 'host)
(assq-ref opts 'port))
- (start-guix-data-service-web-server
- (assq-ref opts 'port)
- (assq-ref opts 'host)
- (assq-ref opts 'secret-key-base)
- #:postgresql-statement-timeout
+ (with-postgresql-connection-per-thread
+ "web"
+ (lambda ()
+ (start-guix-data-service-web-server
+ (assq-ref opts 'port)
+ (assq-ref opts 'host)
+ (assq-ref opts 'secret-key-base)))
+ #:statement-timeout
(assq-ref opts 'postgresql-statement-timeout))))
- branch master updated (470573b -> 55eaaae), Christopher Baines, 2020/10/03
- 01/06: Extract out opening PostgreSQL connections, Christopher Baines, 2020/10/03
- 02/06: Add some utilities to work with PostgreSQL connections in threads, Christopher Baines, 2020/10/03
- 03/06: Stop opening a PostgreSQL connection per request,
Christopher Baines <=
- 05/06: Completely rework the way db connections are handled during requests, Christopher Baines, 2020/10/03
- 04/06: Rework the shortlived PostgreSQL specific connection channel, Christopher Baines, 2020/10/03
- 06/06: Bump the copyright date in the footer, Christopher Baines, 2020/10/03