2016-11-20 21:13:40 +00:00
|
|
|
#lang syndicate/actor
|
|
|
|
|
|
|
|
(provide remote-topic-main)
|
|
|
|
|
|
|
|
(require racket/dict)
|
|
|
|
(require racket/set)
|
|
|
|
(require file/sha1)
|
|
|
|
|
|
|
|
(require/activate syndicate/drivers/timestate)
|
|
|
|
(require/activate syndicate/drivers/web)
|
|
|
|
(require/activate "../config.rkt")
|
|
|
|
|
|
|
|
(require "../private/util.rkt")
|
|
|
|
(require "../protocol.rkt")
|
|
|
|
|
2016-11-21 21:04:07 +00:00
|
|
|
(module+ test (require rackunit))
|
|
|
|
|
2016-11-21 04:05:01 +00:00
|
|
|
(define (set-minimum xs)
|
|
|
|
(for/fold [(i #f)] [(candidate (in-set xs))]
|
|
|
|
(cond [(not candidate) i]
|
|
|
|
[(not i) candidate]
|
|
|
|
[(< candidate i) candidate]
|
|
|
|
[else i])))
|
|
|
|
|
2016-11-21 21:04:07 +00:00
|
|
|
(define (shrink-lease seconds)
|
|
|
|
(if (< seconds 100)
|
|
|
|
(* seconds 9/10)
|
|
|
|
(- seconds 10)))
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
(check-equal? (shrink-lease 1000) 990)
|
|
|
|
(check-equal? (shrink-lease 101) 91)
|
|
|
|
(check-equal? (shrink-lease 100) 90)
|
|
|
|
(check-equal? (shrink-lease 90) 81)
|
|
|
|
(check-equal? (shrink-lease 50) 45))
|
|
|
|
|
2016-11-20 21:13:40 +00:00
|
|
|
(define (remote-topic-main full-topic)
|
|
|
|
(define sub-id (random-hex-string 16))
|
2016-11-21 21:09:07 +00:00
|
|
|
(log-info "Remote sub endpoint ~a for topic ~s" sub-id full-topic)
|
2016-11-20 21:13:40 +00:00
|
|
|
|
|
|
|
(field [current-content-hash #f]
|
|
|
|
[current-content-type #f]
|
|
|
|
[current-upstream-hub #f]
|
|
|
|
[established-upstream-hub #f])
|
|
|
|
|
|
|
|
(field [last-upstream-check 0]
|
2016-11-21 21:04:07 +00:00
|
|
|
[poll-interval-seconds #f]
|
|
|
|
[next-subscription-refresh #f])
|
2016-11-20 21:13:40 +00:00
|
|
|
|
|
|
|
(define/query-config min-poll-interval 60)
|
2016-11-21 21:04:07 +00:00
|
|
|
(define/query-config max-upstream-redirects 5)
|
|
|
|
(define/query-config subscription-retry-delay 600)
|
|
|
|
|
|
|
|
(define/query-set poll-intervals (topic-demand full-topic $i) i)
|
2016-11-20 21:13:40 +00:00
|
|
|
(begin/dataflow
|
2016-11-21 04:05:01 +00:00
|
|
|
(define candidate (set-minimum (poll-intervals)))
|
2016-11-20 21:13:40 +00:00
|
|
|
(poll-interval-seconds (and candidate (max candidate (min-poll-interval)))))
|
|
|
|
(begin/dataflow
|
|
|
|
(log-info "Poll interval for ~a is now ~a"
|
|
|
|
full-topic
|
|
|
|
(match (poll-interval-seconds)
|
|
|
|
[#f "disabled"]
|
|
|
|
[n (format "~a seconds" n)])))
|
|
|
|
|
|
|
|
(during (canonical-local-host $canonical-host-name $cport)
|
|
|
|
(define callback (canonical-url canonical-host-name cport `("sub" (,sub-id ()))))
|
|
|
|
|
|
|
|
(define (refresh-subscription!)
|
|
|
|
;; TODO: shared secret
|
2016-11-21 21:04:07 +00:00
|
|
|
(define hub (current-upstream-hub)) ;; keep a stable value, lest it change underneath us
|
|
|
|
(when hub
|
|
|
|
(log-info "Subscribing to hub ~s for topic ~s" hub full-topic)
|
|
|
|
(analyze-response
|
|
|
|
(lambda () (web-post/form-parameters! hub `((hub.callback . ,callback)
|
|
|
|
(hub.mode . "subscribe")
|
|
|
|
(hub.topic . ,full-topic))))
|
|
|
|
(lambda ()
|
|
|
|
(when (equal? (current-upstream-hub) hub) ;; it may have changed asynchronously
|
|
|
|
(established-upstream-hub hub)))
|
|
|
|
(lambda (resp response-body)
|
|
|
|
(log-warning
|
|
|
|
"Upstream subscription to hub ~s for topic ~s failed:\n - headers: ~v\n - body: ~s"
|
|
|
|
hub
|
|
|
|
full-topic
|
|
|
|
resp
|
|
|
|
response-body)
|
|
|
|
(next-subscription-refresh (+ (current-inexact-milliseconds)
|
|
|
|
(* 1000.0 (subscription-retry-delay))))))))
|
2016-11-20 21:13:40 +00:00
|
|
|
|
|
|
|
(define (unsubscribe!)
|
2016-11-21 21:04:07 +00:00
|
|
|
(define hub (established-upstream-hub))
|
|
|
|
(when hub
|
|
|
|
(log-info "Unsubscribing from hub ~s for topic ~s" hub full-topic)
|
|
|
|
(analyze-response
|
|
|
|
(lambda () (web-post/form-parameters! hub `((hub.callback . ,callback)
|
|
|
|
(hub.mode . "unsubscribe")
|
|
|
|
(hub.topic . ,full-topic))))
|
|
|
|
void
|
|
|
|
(lambda (resp response-body)
|
|
|
|
(log-warning
|
|
|
|
"Upstream unsubscription from hub ~s for topic ~s failed:\n - headers: ~v\n - body: ~s"
|
|
|
|
hub
|
|
|
|
full-topic
|
|
|
|
resp
|
|
|
|
response-body)))
|
|
|
|
(when (equal? (established-upstream-hub) hub) ;; it may have changed asynchronously
|
|
|
|
(established-upstream-hub #f))))
|
|
|
|
|
|
|
|
(field [poll-busy? #f]) ;; Ensure we only run one poll at once
|
|
|
|
(define (poll-upstream!)
|
|
|
|
(when (not (poll-busy?))
|
|
|
|
(poll-busy? #t)
|
2016-11-20 21:13:40 +00:00
|
|
|
(log-info "Checking upstream ~a" full-topic)
|
|
|
|
(define-values (resp response-body)
|
|
|
|
(web-request! 'get full-topic #:redirect-budget (max-upstream-redirects)))
|
|
|
|
(last-upstream-check (current-inexact-milliseconds))
|
|
|
|
(match (web-response-header-code-type resp)
|
|
|
|
['successful
|
|
|
|
(define new-content-hash (sha1 (open-input-bytes response-body)))
|
|
|
|
(define new-content-type (dict-ref (web-response-header-headers resp) 'content-type #f))
|
|
|
|
(define parsed-link-headers (parse-link-headers (web-response-header-headers resp)))
|
|
|
|
(define upstream-hub (link-header-ref parsed-link-headers 'hub #f))
|
|
|
|
(define upstream-topic (link-header-ref parsed-link-headers 'self #f))
|
|
|
|
(when (not (and (equal? (current-content-hash) new-content-hash)
|
|
|
|
(equal? (current-content-type) new-content-type)))
|
|
|
|
(current-content-hash new-content-hash)
|
|
|
|
(current-content-type new-content-type)
|
|
|
|
(send! (notification full-topic
|
|
|
|
upstream-hub
|
|
|
|
upstream-topic
|
|
|
|
response-body
|
|
|
|
new-content-type)))
|
2016-11-21 21:04:07 +00:00
|
|
|
(current-upstream-hub upstream-hub)]
|
2016-11-20 21:13:40 +00:00
|
|
|
[other
|
2016-11-21 21:04:07 +00:00
|
|
|
(log-warning "Upstream ~a yielded ~a code ~a during poll"
|
2016-11-20 21:13:40 +00:00
|
|
|
full-topic
|
|
|
|
other
|
2016-11-21 21:04:07 +00:00
|
|
|
(web-response-header-code resp))])
|
|
|
|
(poll-busy? #f)))
|
|
|
|
|
|
|
|
(on-start (poll-upstream!)) ;; always check at least once, for discovery
|
|
|
|
(on-stop (unsubscribe!))
|
2016-11-21 21:09:07 +00:00
|
|
|
(on-stop (log-info "Remote topic ~s terminating" full-topic))
|
2016-11-21 21:04:07 +00:00
|
|
|
|
|
|
|
(begin/dataflow
|
|
|
|
(when (not (equal? (current-upstream-hub) (established-upstream-hub)))
|
|
|
|
(unsubscribe!)
|
|
|
|
(refresh-subscription!)))
|
|
|
|
|
|
|
|
(begin/dataflow
|
|
|
|
(when (next-subscription-refresh)
|
|
|
|
(log-info "Next subscription refresh for topic ~s will occur in ~a seconds."
|
|
|
|
full-topic
|
|
|
|
(/ (- (next-subscription-refresh) (current-inexact-milliseconds)) 1000.0))))
|
|
|
|
|
|
|
|
(on #:when (next-subscription-refresh) (asserted (later-than (next-subscription-refresh)))
|
|
|
|
(next-subscription-refresh #f)
|
|
|
|
(refresh-subscription!))
|
|
|
|
|
|
|
|
(on #:when (poll-interval-seconds) (asserted (later-than (+ (last-upstream-check)
|
|
|
|
(* 1000 (or (poll-interval-seconds) 0)))))
|
|
|
|
(poll-upstream!))
|
2016-11-20 21:13:40 +00:00
|
|
|
|
|
|
|
(on (web-request-get (id req) (vh canonical-host-name cport) ("sub" (,sub-id ())))
|
|
|
|
(log-info "Received verification-of-intent: ~v" (web-request-header-query req))
|
2016-11-21 21:04:07 +00:00
|
|
|
(define challenge (dict-ref (web-request-header-query req) 'hub.challenge ""))
|
|
|
|
(define lease-seconds (dict-ref (web-request-header-query req) 'hub.lease_seconds #f))
|
|
|
|
(if lease-seconds
|
|
|
|
(next-subscription-refresh (+ (current-inexact-milliseconds)
|
|
|
|
(* 1000.0 (shrink-lease lease-seconds))))
|
|
|
|
(log-warning "Upstream hub for topic ~s did not supply hub.lease_seconds" full-topic))
|
2016-11-20 21:13:40 +00:00
|
|
|
(web-respond/bytes! id (string->bytes/utf-8 challenge)))
|
|
|
|
|
|
|
|
(on (web-request-incoming (id req) (vh canonical-host-name cport) 'post ("sub" (,sub-id ())) $body)
|
|
|
|
;; TODO: verify the use of the shared secret
|
|
|
|
(actor*
|
|
|
|
(define parsed-link-headers (parse-link-headers (web-request-header-headers req)))
|
|
|
|
(define upstream-hub (link-header-ref parsed-link-headers 'hub #f))
|
|
|
|
(define upstream-topic (link-header-ref parsed-link-headers 'self #f))
|
|
|
|
(define content-type (web-request-header-content-type req))
|
|
|
|
(log-info "Remote topic ~a got ~v message ~v; upstream hub ~v, topic ~v"
|
|
|
|
full-topic
|
|
|
|
content-type
|
|
|
|
body
|
|
|
|
upstream-hub
|
|
|
|
upstream-topic)
|
|
|
|
(current-content-hash (sha1 (open-input-bytes body)))
|
|
|
|
(current-content-type content-type)
|
|
|
|
(current-upstream-hub upstream-hub)
|
|
|
|
(send! (notification full-topic
|
|
|
|
upstream-hub
|
|
|
|
upstream-topic
|
|
|
|
body
|
|
|
|
content-type))
|
|
|
|
(web-respond/status! id 201 #"Created")))))
|