Skip to content

Commit

Permalink
[new] [Undertow adapter] [#409] Allow Ajax read timeout (@kajism)
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Mar 7, 2023
1 parent 318e90a commit 8806e72
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/taoensso/sente/server_adapters/undertow.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"Sente server adapter for ring-undertow-adapter."
{:author "Nik Peric"}
(:require
[clojure.core.async :as async]
[ring.adapter.undertow.websocket :as websocket]
[ring.adapter.undertow.response :as response]
[taoensso.sente.interfaces :as i])
Expand Down Expand Up @@ -40,24 +39,30 @@
(defprotocol ISenteUndertowAjaxChannel
(ajax-read! [sch]))

(deftype SenteUndertowAjaxChannel [resp-ch open?_ on-close _adapter-opts]
(deftype SenteUndertowAjaxChannel [resp-promise_ open?_ on-close adapter-opts]
i/IServerChan
(sch-send! [sch websocket? msg] (async/put! resp-ch msg (fn [_] (i/sch-close! sch))))
(sch-send! [sch websocket? msg] (deliver resp-promise_ msg) (i/sch-close! sch))
(sch-open? [sch] @open?_)
(sch-close! [sch]
(when (compare-and-set! open?_ true false)
(deliver resp-promise_ nil)
(when on-close (on-close sch false nil))
(async/close! resp-ch)
true))

ISenteUndertowAjaxChannel
(ajax-read! [sch] (async/<!! resp-ch)))
(ajax-read! [sch]
(let [{:keys [ajax-resp-timeout-ms ajax-resp-timeout-val]}
adapter-opts]

(if ajax-resp-timeout-ms
(deref resp-promise_ ajax-resp-timeout-ms ajax-resp-timeout-val)
(deref resp-promise_)))))

(defn- ajax-ch [{:keys [on-open on-close]} adapter-opts]
(let [resp-ch (async/chan 1)
open?_ (atom true)
sch (SenteUndertowAjaxChannel. resp-ch open?_ on-close
adapter-opts)]
(let [open?_ (atom true)
sch
(SenteUndertowAjaxChannel. (promise) open?_ on-close
adapter-opts)]

(when on-open (on-open sch false))
sch))
Expand All @@ -78,5 +83,11 @@
(ajax-ch callbacks-map adapter-opts))}))

(defn get-sch-adapter
([ ] (get-sch-adapter nil))
([opts] (UndertowServerChanAdapter. opts)))
"Returns an Undertow ServerChanAdapter. Options:
:ajax-resp-timeout-ms ; Max msecs to wait for Ajax responses
:ajax-resp-timeout-val ; Value returned in case of above timeout"
([] (get-sch-adapter nil))
([{:as opts
:keys [ajax-resp-timeout-ms
ajax-resp-timeout-val]}]
(UndertowServerChanAdapter. opts)))

0 comments on commit 8806e72

Please sign in to comment.