aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/server.scm
diff options
context:
space:
mode:
authorChristopher Baines <mail@cbaines.net>2023-07-09 16:52:35 +0100
committerChristopher Baines <mail@cbaines.net>2023-07-10 18:56:31 +0100
commit7251c7d653de29f36d50b33badf05a5db983b8e7 (patch)
tree3f74252cf1f0d13d35dc1253406d9a3b92b67f7e /guix-data-service/web/server.scm
parent672ee6216e1d15f7f550f53017323b59f05303cb (diff)
downloaddata-service-7251c7d653de29f36d50b33badf05a5db983b8e7.tar
data-service-7251c7d653de29f36d50b33badf05a5db983b8e7.tar.gz
Stop using a pool of threads for database operations
Now that squee cooperates with suspendable ports, this is unnecessary. Use a connection pool to still support running queries in parallel using multiple connections.
Diffstat (limited to 'guix-data-service/web/server.scm')
-rw-r--r--guix-data-service/web/server.scm65
1 files changed, 47 insertions, 18 deletions
diff --git a/guix-data-service/web/server.scm b/guix-data-service/web/server.scm
index 6570c1a..84a0e6b 100644
--- a/guix-data-service/web/server.scm
+++ b/guix-data-service/web/server.scm
@@ -25,8 +25,10 @@
#:use-module (web uri)
#:use-module (system repl error-handling)
#:use-module (ice-9 atomic)
- #:use-module (fibers web server)
+ #:use-module (fibers)
+ #:use-module (fibers conditions)
#:use-module (prometheus)
+ #:use-module (guix-data-service utils)
#:use-module (guix-data-service database)
#:use-module (guix-data-service web controller)
#:use-module (guix-data-service web util)
@@ -60,7 +62,9 @@
render-metrics))))
(define* (start-guix-data-service-web-server port host secret-key-base
- startup-completed)
+ startup-completed
+ #:key postgresql-statement-timeout
+ postgresql-connections)
(define registry
(make-metrics-registry #:namespace "guixdataservice"))
@@ -69,25 +73,50 @@
(%database-metrics-registry registry)
- (call-with-error-handling
- (lambda ()
- (run-server (lambda (request body)
+ (let ((finished? (make-condition)))
+ (call-with-sigint
+ (lambda ()
+ (run-fibers
+ (lambda ()
+ (parameterize
+ ((connection-pool
+ (make-resource-pool
+ (lambda ()
+ (open-postgresql-connection
+ "web"
+ postgresql-statement-timeout))
+ (floor (/ postgresql-connections 2))))
+
+ (reserved-connection-pool
+ (make-resource-pool
+ (lambda ()
+ (open-postgresql-connection
+ "web-reserved"
+ postgresql-statement-timeout))
+ (floor (/ postgresql-connections 2))))
+
+ (resource-pool-default-timeout 10))
+
+ (with-exception-handler
+ (lambda (exn)
+ (simple-format
+ (current-error-port)
+ "\n
+error: guix-data-service could not start: ~A
+
+Check if it's already running, or whether another process is using that
+port. Also, the port used can be changed by passing the --port option.\n"
+ exn)
+ (primitive-exit 1))
+ (lambda ()
+ (run-server/patched
+ (lambda (request body)
(handler request body controller
secret-key-base
startup-completed
render-metrics))
#:host host
#:port port))
- #:on-error 'backtrace
- #:post-error (lambda (key . args)
- (when (eq? key 'system-error)
- (match args
- (("bind" "~A" ("Address already in use") _)
- (simple-format
- (current-error-port)
- "\n
-error: guix-data-service could not start, as it could not bind to port ~A
-
-Check if it's already running, or whether another process is using that
-port. Also, the port used can be changed by passing the --port option.\n"
- port)))))))
+ #:unwind? #t))
+ (wait finished?))))
+ finished?)))