Use with-handlers in hub actor instead of actor* for fault isolation

This commit is contained in:
Tony Garnock-Jones 2016-10-25 17:06:45 -04:00
parent f2f8fda62e
commit f16b0884f0
1 changed files with 49 additions and 43 deletions

View File

@ -124,8 +124,14 @@
(actor #:name 'hub (actor #:name 'hub
(on (web-request-incoming (id req) vh 'post ("hub" ()) $body) (on (web-request-incoming (id req) vh 'post ("hub" ()) $body)
(actor* ;; Initially, I had an (actor* ...) form here for fault
#:name (gensym 'hub-post) ;; isolation. However, this led to problems since I wanted
;; to use `assert!` and `retract!` to signal to the
;; `during/actor`, and the assertions were being lost as
;; the `actor*` terminated. So instead, I'm using Rackety
;; `with-handlers`.
(define ok?
(with-handlers [(values (lambda (e) #f))]
(define params (make-immutable-hash (define params (make-immutable-hash
(form-urlencoded->alist (bytes->string/utf-8 body)))) (form-urlencoded->alist (bytes->string/utf-8 body))))
(define callback (hash-ref params 'hub.callback)) (define callback (hash-ref params 'hub.callback))
@ -141,8 +147,8 @@
(define secret-string (hash-ref params 'hub.secret #f)) (define secret-string (hash-ref params 'hub.secret #f))
(define secret-bytes (and secret-string (string->bytes/utf-8 secret-string))) (define secret-bytes (and secret-string (string->bytes/utf-8 secret-string)))
(define expiry-deadline (and lease-seconds (+ (current-seconds) lease-seconds))) (define expiry-deadline (and lease-seconds (+ (current-seconds) lease-seconds)))
(define canonical-hub (url->string (resource->url (web-request-header-resource req)))) (define canonical-hub
(define ok? (url->string (resource->url (web-request-header-resource req))))
(match mode (match mode
['subscribe ['subscribe
(if (subscription-change-validate "subscribe" (if (subscription-change-validate "subscribe"
@ -163,10 +169,10 @@
(begin (begin
(retract! (subscription topic ? ? callback ?)) (retract! (subscription topic ? ? callback ?))
#t) #t)
#f)])) #f)])))
(if ok? (if ok?
(web-respond/status! id 202 #"Accepted") (web-respond/status! id 202 #"Accepted")
(web-respond/status! id 403 #"Forbidden" #"Validation failed")))) (web-respond/status! id 403 #"Forbidden" #"Validation failed")))
(during/actor (subscription $topic _ _ $callback _) (during/actor (subscription $topic _ _ $callback _)
#:name (list 'subscription topic callback) #:name (list 'subscription topic callback)