Move the exception handling to the knots web server
Rather than adding another layer. This commit also removes the extra fibers layer when processing requests. I forget exactly why this was added, but if the reason resurfaces then it should be addressed in knots.
This commit is contained in:
parent
7dd6be1c3b
commit
98c4fae76f
2 changed files with 49 additions and 96 deletions
|
|
@ -720,59 +720,9 @@
|
||||||
#:sxml (server-starting-up-page)
|
#:sxml (server-starting-up-page)
|
||||||
#:code 503)))
|
#:code 503)))
|
||||||
|
|
||||||
(with-exception-handler
|
(if startup-completed?
|
||||||
(lambda (exn)
|
(running-controller-thunk)
|
||||||
(when (resource-pool-timeout-error? exn)
|
(startup-controller-thunk)))
|
||||||
(spawn-fiber
|
|
||||||
(lambda ()
|
|
||||||
(let* ((pool (resource-pool-timeout-error-pool exn))
|
|
||||||
(stats (resource-pool-stats pool)))
|
|
||||||
(simple-format (current-error-port)
|
|
||||||
"resource pool timeout error: ~A, ~A\n"
|
|
||||||
pool
|
|
||||||
stats)))))
|
|
||||||
|
|
||||||
(case (most-appropriate-mime-type
|
|
||||||
mime-types
|
|
||||||
'(text/html application/json))
|
|
||||||
((application/json)
|
|
||||||
(render-json `((error . ,(if (%show-error-details)
|
|
||||||
(simple-format #f "~A" exn)
|
|
||||||
#f)))
|
|
||||||
#:code 500))
|
|
||||||
(else
|
|
||||||
(render-html #:sxml (error-page
|
|
||||||
(if (%show-error-details)
|
|
||||||
exn
|
|
||||||
#f))
|
|
||||||
#:code 500))))
|
|
||||||
(lambda ()
|
|
||||||
(with-throw-handler #t
|
|
||||||
(if startup-completed?
|
|
||||||
running-controller-thunk
|
|
||||||
startup-controller-thunk)
|
|
||||||
(lambda (key . args)
|
|
||||||
(match method-and-path-components
|
|
||||||
((method path-components ...)
|
|
||||||
(simple-format
|
|
||||||
(current-error-port)
|
|
||||||
"error: when processing: ~A /~A\n ~A ~A\n"
|
|
||||||
method (string-join path-components "/")
|
|
||||||
key args)))
|
|
||||||
|
|
||||||
(let* ((stack (make-stack #t 4))
|
|
||||||
(backtrace
|
|
||||||
(call-with-output-string
|
|
||||||
(lambda (port)
|
|
||||||
(display "\nBacktrace:\n" port)
|
|
||||||
(display-backtrace stack port)
|
|
||||||
(newline port)
|
|
||||||
(newline port)))))
|
|
||||||
(display
|
|
||||||
backtrace
|
|
||||||
(current-error-port))))))
|
|
||||||
#:unwind? #t))
|
|
||||||
|
|
||||||
|
|
||||||
(define* (base-controller request method-and-path-components
|
(define* (base-controller request method-and-path-components
|
||||||
startup-completed?)
|
startup-completed?)
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
(define-module (guix-data-service web server)
|
(define-module (guix-data-service web server)
|
||||||
#:use-module (srfi srfi-1)
|
#:use-module (srfi srfi-1)
|
||||||
#:use-module (srfi srfi-11)
|
#:use-module (srfi srfi-11)
|
||||||
|
#:use-module (srfi srfi-71)
|
||||||
#:use-module (ice-9 match)
|
#:use-module (ice-9 match)
|
||||||
#:use-module (ice-9 threads)
|
#:use-module (ice-9 threads)
|
||||||
#:use-module (web http)
|
#:use-module (web http)
|
||||||
|
|
@ -39,6 +40,8 @@
|
||||||
#:use-module (guix-data-service database)
|
#:use-module (guix-data-service database)
|
||||||
#:use-module (guix-data-service web controller)
|
#:use-module (guix-data-service web controller)
|
||||||
#:use-module (guix-data-service web util)
|
#:use-module (guix-data-service web util)
|
||||||
|
#:use-module (guix-data-service web render)
|
||||||
|
#:use-module (guix-data-service web view html)
|
||||||
#:use-module (guix-data-service model guix-revision-package-derivation)
|
#:use-module (guix-data-service model guix-revision-package-derivation)
|
||||||
#:export (%guix-data-service-metrics-registry
|
#:export (%guix-data-service-metrics-registry
|
||||||
|
|
||||||
|
|
@ -134,33 +137,6 @@
|
||||||
#:hz 0
|
#:hz 0
|
||||||
#:parallelism 1)))
|
#:parallelism 1)))
|
||||||
|
|
||||||
(call-with-new-thread
|
|
||||||
(lambda ()
|
|
||||||
(run-fibers
|
|
||||||
(lambda ()
|
|
||||||
(let* ((current (current-scheduler))
|
|
||||||
(schedulers
|
|
||||||
(cons current (scheduler-remote-peers current))))
|
|
||||||
|
|
||||||
(set! request-scheduler current)
|
|
||||||
|
|
||||||
(for-each
|
|
||||||
(lambda (i sched)
|
|
||||||
(spawn-fiber
|
|
||||||
(lambda ()
|
|
||||||
(catch 'system-error
|
|
||||||
(lambda ()
|
|
||||||
(set-thread-name
|
|
||||||
(string-append "rp " (number->string i))))
|
|
||||||
(const #t)))
|
|
||||||
sched))
|
|
||||||
(iota (length schedulers))
|
|
||||||
schedulers))
|
|
||||||
|
|
||||||
(wait finished?))
|
|
||||||
#:hz 0
|
|
||||||
#:parallelism 4)))
|
|
||||||
|
|
||||||
(run-fibers
|
(run-fibers
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(let* ((current (current-scheduler))
|
(let* ((current (current-scheduler))
|
||||||
|
|
@ -179,8 +155,6 @@
|
||||||
(iota (length schedulers))
|
(iota (length schedulers))
|
||||||
schedulers))
|
schedulers))
|
||||||
|
|
||||||
(while (not request-scheduler)
|
|
||||||
(sleep 0.1))
|
|
||||||
(while (not priority-scheduler)
|
(while (not priority-scheduler)
|
||||||
(sleep 0.1))
|
(sleep 0.1))
|
||||||
|
|
||||||
|
|
@ -268,29 +242,58 @@ port. Also, the port used can be changed by passing the --port option.\n"
|
||||||
(with-resource-from-pool (background-connection-pool) conn
|
(with-resource-from-pool (background-connection-pool) conn
|
||||||
(backfill-guix-revision-package-derivation-distribution-counts
|
(backfill-guix-revision-package-derivation-distribution-counts
|
||||||
conn)))
|
conn)))
|
||||||
request-scheduler)
|
#:parallel? #t)
|
||||||
|
|
||||||
(let ((render-metrics (make-render-metrics registry)))
|
(let ((render-metrics (make-render-metrics registry)))
|
||||||
(run-knots-web-server
|
(run-knots-web-server
|
||||||
(lambda (request)
|
(lambda (request)
|
||||||
(metric-increment requests-metric)
|
(metric-increment requests-metric)
|
||||||
|
|
||||||
(let ((body (read-request-body request))
|
(let ((body (read-request-body request)))
|
||||||
(reply (make-channel)))
|
(handler request finished? body controller
|
||||||
|
secret-key-base
|
||||||
|
startup-completed
|
||||||
|
render-metrics)))
|
||||||
|
#:exception-handler
|
||||||
|
(lambda (exn request)
|
||||||
|
(when (resource-pool-timeout-error? exn)
|
||||||
(spawn-fiber
|
(spawn-fiber
|
||||||
(lambda ()
|
(lambda ()
|
||||||
(call-with-values
|
(let* ((pool (resource-pool-timeout-error-pool exn))
|
||||||
(lambda ()
|
(stats (resource-pool-stats pool)))
|
||||||
(handler request finished? body controller
|
(simple-format (current-error-port)
|
||||||
secret-key-base
|
"resource pool timeout error: ~A, ~A\n"
|
||||||
startup-completed
|
pool
|
||||||
render-metrics))
|
stats)))))
|
||||||
(lambda vals
|
|
||||||
(put-message reply vals))))
|
|
||||||
request-scheduler
|
|
||||||
#:parallel? #t)
|
|
||||||
|
|
||||||
(apply values (get-message reply))))
|
(let ((path-components
|
||||||
|
mime-types
|
||||||
|
(request->path-components-and-mime-type request)))
|
||||||
|
(simple-format
|
||||||
|
(current-error-port)
|
||||||
|
"error: when processing: ~A /~A\n ~A\n"
|
||||||
|
(request-method request)
|
||||||
|
(string-join path-components "/")
|
||||||
|
exn)
|
||||||
|
|
||||||
|
(case (most-appropriate-mime-type
|
||||||
|
mime-types
|
||||||
|
'(text/html application/json))
|
||||||
|
((application/json)
|
||||||
|
(apply
|
||||||
|
values
|
||||||
|
(render-json `((error . ,(if (%show-error-details)
|
||||||
|
(simple-format #f "~A" exn)
|
||||||
|
#f)))
|
||||||
|
#:code 500)))
|
||||||
|
(else
|
||||||
|
(apply
|
||||||
|
values
|
||||||
|
(render-html #:sxml (error-page
|
||||||
|
(if (%show-error-details)
|
||||||
|
exn
|
||||||
|
#f))
|
||||||
|
#:code 500))))))
|
||||||
#:host host
|
#:host host
|
||||||
#:port port)))
|
#:port port)))
|
||||||
#:unwind? #t)))
|
#:unwind? #t)))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue