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,49 +124,55 @@
(actor #:name 'hub
(on (web-request-incoming (id req) vh 'post ("hub" ()) $body)
(actor*
#:name (gensym 'hub-post)
(define params (make-immutable-hash
(form-urlencoded->alist (bytes->string/utf-8 body))))
(define callback (hash-ref params 'hub.callback))
(define mode
(match (hash-ref params 'hub.mode)
["subscribe" 'subscribe]
["unsubscribe" 'unsubscribe]))
(define topic (hash-ref params 'hub.topic))
(define lease-seconds
(match (hash-ref params 'hub.lease_seconds "unbounded")
["unbounded" #f]
[n (string->number n)]))
(define secret-string (hash-ref params 'hub.secret #f))
(define secret-bytes (and secret-string (string->bytes/utf-8 secret-string)))
(define expiry-deadline (and lease-seconds (+ (current-seconds) lease-seconds)))
(define canonical-hub (url->string (resource->url (web-request-header-resource req))))
(define ok?
(match mode
['subscribe
(if (subscription-change-validate "subscribe"
(or lease-seconds "unbounded")
topic
callback)
(begin
(retract! (subscription topic ? ? callback ?))
(assert!
(subscription topic expiry-deadline canonical-hub callback secret-bytes))
#t)
#f)]
['unsubscribe
(if (subscription-change-validate "unsubscribe"
#f
topic
callback)
(begin
(retract! (subscription topic ? ? callback ?))
#t)
#f)]))
(if ok?
(web-respond/status! id 202 #"Accepted")
(web-respond/status! id 403 #"Forbidden" #"Validation failed"))))
;; Initially, I had an (actor* ...) form here for fault
;; 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
(form-urlencoded->alist (bytes->string/utf-8 body))))
(define callback (hash-ref params 'hub.callback))
(define mode
(match (hash-ref params 'hub.mode)
["subscribe" 'subscribe]
["unsubscribe" 'unsubscribe]))
(define topic (hash-ref params 'hub.topic))
(define lease-seconds
(match (hash-ref params 'hub.lease_seconds "unbounded")
["unbounded" #f]
[n (string->number n)]))
(define secret-string (hash-ref params 'hub.secret #f))
(define secret-bytes (and secret-string (string->bytes/utf-8 secret-string)))
(define expiry-deadline (and lease-seconds (+ (current-seconds) lease-seconds)))
(define canonical-hub
(url->string (resource->url (web-request-header-resource req))))
(match mode
['subscribe
(if (subscription-change-validate "subscribe"
(or lease-seconds "unbounded")
topic
callback)
(begin
(retract! (subscription topic ? ? callback ?))
(assert!
(subscription topic expiry-deadline canonical-hub callback secret-bytes))
#t)
#f)]
['unsubscribe
(if (subscription-change-validate "unsubscribe"
#f
topic
callback)
(begin
(retract! (subscription topic ? ? callback ?))
#t)
#f)])))
(if ok?
(web-respond/status! id 202 #"Accepted")
(web-respond/status! id 403 #"Forbidden" #"Validation failed")))
(during/actor (subscription $topic _ _ $callback _)
#:name (list 'subscription topic callback)