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.
This commit is contained in:
Christopher Baines 2020-10-03 09:22:29 +01:00
parent 9723a18df4
commit 18b6dd9e6d
3 changed files with 16 additions and 20 deletions

View file

@ -391,8 +391,7 @@
(define* (controller request method-and-path-components (define* (controller request method-and-path-components
mime-types body mime-types body
secret-key-base secret-key-base)
#:key postgresql-statement-timeout)
(define (controller-thunk) (define (controller-thunk)
(match method-and-path-components (match method-and-path-components
(('GET "assets" rest ...) (('GET "assets" rest ...)
@ -430,16 +429,14 @@
"The README.html file does not exist") "The README.html file does not exist")
#:code 404)))) #:code 404))))
(_ (_
(with-postgresql-connection (with-thread-postgresql-connection
"web"
(lambda (conn) (lambda (conn)
(controller-with-database-connection request (controller-with-database-connection request
method-and-path-components method-and-path-components
mime-types mime-types
body body
conn conn
secret-key-base)) secret-key-base))))))
#:statement-timeout postgresql-statement-timeout))))
(call-with-error-handling (call-with-error-handling
controller-thunk controller-thunk
#:on-error 'backtrace #:on-error 'backtrace

View file

@ -29,8 +29,7 @@
#:use-module (guix-data-service web util) #:use-module (guix-data-service web util)
#:export (start-guix-data-service-web-server)) #:export (start-guix-data-service-web-server))
(define (handler request body controller secret-key-base (define (handler request body controller secret-key-base)
postgresql-statement-timeout)
(display (display
(format #f "~a ~a\n" (format #f "~a ~a\n"
(request-method request) (request-method request)
@ -43,18 +42,14 @@
request-components) request-components)
mime-types mime-types
body body
secret-key-base secret-key-base))))
#:postgresql-statement-timeout
postgresql-statement-timeout))))
(define* (start-guix-data-service-web-server port host secret-key-base (define* (start-guix-data-service-web-server port host secret-key-base)
#:key postgresql-statement-timeout)
(call-with-error-handling (call-with-error-handling
(lambda () (lambda ()
(run-server (lambda (request body) (run-server (lambda (request body)
(handler request body controller (handler request body controller
secret-key-base secret-key-base))
postgresql-statement-timeout))
#:host host #:host host
#:port port)) #:port port))
#:on-error 'backtrace #:on-error 'backtrace

View file

@ -31,6 +31,7 @@
(gcrypt pk-crypto) (gcrypt pk-crypto)
(guix pki) (guix pki)
(guix-data-service config) (guix-data-service config)
(guix-data-service database)
(guix-data-service web server) (guix-data-service web server)
(guix-data-service web controller) (guix-data-service web controller)
(guix-data-service web nar controller)) (guix-data-service web nar controller))
@ -194,9 +195,12 @@
(assq-ref opts 'host) (assq-ref opts 'host)
(assq-ref opts 'port)) (assq-ref opts 'port))
(start-guix-data-service-web-server (with-postgresql-connection-per-thread
(assq-ref opts 'port) "web"
(assq-ref opts 'host) (lambda ()
(assq-ref opts 'secret-key-base) (start-guix-data-service-web-server
#:postgresql-statement-timeout (assq-ref opts 'port)
(assq-ref opts 'host)
(assq-ref opts 'secret-key-base)))
#:statement-timeout
(assq-ref opts 'postgresql-statement-timeout)))) (assq-ref opts 'postgresql-statement-timeout))))