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
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

View File

@ -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

View File

@ -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))))