Skip to content

Commit c711ba5

Browse files
committed
[mod] [#462] Don't throw by default on Ajax read timeouts
It's generally not actionable when this happens, so the objective here is just to provide info. The logging can be disabled either through normal log filtering (e.g. by ns for Timbre/Telemere, or id for Telemere), or by adjusting the on-timeout handler.
1 parent bc29ac3 commit c711ba5

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

src/taoensso/sente/server_adapters/community/jetty.clj

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@
7070

7171
ISenteJettyAjaxChannel
7272
(ajax-read! [_sch]
73-
(let [{:keys [ajax-resp-timeout-ms]} adapter-opts
73+
(let [{:keys [ajax-resp-timeout-ms on-ajax-resp-timeout]} adapter-opts
7474
resp
7575
(if ajax-resp-timeout-ms
7676
(deref resp-promise_ ajax-resp-timeout-ms ::timeout)
7777
(deref resp-promise_))]
7878

7979
(if (= resp ::timeout)
80-
(throw (ex-info "Ajax read timeout" {:timeout-msecs ajax-resp-timeout-ms}))
80+
(when-let [f on-ajax-resp-timeout] (f sch ring-req))
8181
resp))))
8282

8383
(defn- ajax-ch
@@ -115,15 +115,22 @@
115115
Supports Jetty 11, 12.
116116
117117
Options:
118-
`:ajax-resp-timeout-ms` - Max msecs to wait for Ajax responses (default 60 secs),
119-
exception thrown on timeout.
118+
`:ajax-resp-timeout-ms` - Max msecs to wait for Ajax responses (default 60 secs)
119+
`:on-ajax-resp-timeout` - (fn [ServerChan ring-req]) to trigger after above timeout (logs by default)
120120
121121
[1] Ref. <https://github.com/ring-clojure/ring/tree/master/ring-jetty-adapter>."
122122
([] (get-sch-adapter nil))
123123
([{:as opts
124-
:keys [ajax-resp-timeout-ms]
125-
:or {ajax-resp-timeout-ms (* 60 1000)}}]
124+
:keys [ajax-resp-timeout-ms on-ajax-resp-timeout]
125+
:or
126+
{ ajax-resp-timeout-ms (* 60 1000)
127+
on-ajax-resp-timeout
128+
(fn [sch ring-req]
129+
(trove/log!
130+
{:level :warn, :id :sente.server.jetty/ajax-read-timeout,
131+
:data ring-req}))}}]
126132

127133
(JettyServerChanAdapter.
128134
(assoc opts
129-
:ajax-resp-timeout-ms ajax-resp-timeout-ms))))
135+
:ajax-resp-timeout-ms ajax-resp-timeout-ms
136+
:on-ajax-resp-timeout on-ajax-resp-timeout))))

src/taoensso/sente/server_adapters/community/undertow.clj

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
(:require
44
[ring.adapter.undertow.websocket :as websocket]
55
[ring.adapter.undertow.response :as response]
6-
[taoensso.sente.interfaces :as i])
6+
[taoensso.sente.interfaces :as i]
7+
[taoensso.trove :as trove])
78
(:import
89
[io.undertow.websockets.core WebSocketChannel]
910
[io.undertow.server HttpServerExchange]
@@ -52,14 +53,14 @@
5253

5354
ISenteUndertowAjaxChannel
5455
(ajax-read! [sch]
55-
(let [{:keys [ajax-resp-timeout-ms]} adapter-opts
56+
(let [{:keys [ajax-resp-timeout-ms on-ajax-resp-timeout]} adapter-opts
5657
resp
5758
(if ajax-resp-timeout-ms
5859
(deref resp-promise_ ajax-resp-timeout-ms ::timeout)
5960
(deref resp-promise_))]
6061

6162
(if (= resp ::timeout)
62-
(throw (ex-info "Ajax read timeout" {:timeout-msecs ajax-resp-timeout-ms}))
63+
(when-let [f on-ajax-resp-timeout] (f sch ring-req))
6364
resp))))
6465

6566
(defn- ajax-ch
@@ -91,15 +92,22 @@
9192
"Returns a Sente `ServerChan` adapter for `ring-undertow-adapter` [1].
9293
9394
Options:
94-
`:ajax-resp-timeout-ms` - Max msecs to wait for Ajax responses (default 60 secs),
95-
exception thrown on timeout.
95+
`:ajax-resp-timeout-ms` - Max msecs to wait for Ajax responses (default 60 secs)
96+
`:on-ajax-resp-timeout` - (fn [ServerChan ring-req]) to trigger after above timeout (logs by default)
9697
9798
[1] Ref. <https://github.com/luminus-framework/ring-undertow-adapter>."
9899
([] (get-sch-adapter nil))
99100
([{:as opts
100-
:keys [ajax-resp-timeout-ms]
101-
:or {ajax-resp-timeout-ms (* 60 1000)}}]
101+
:keys [ajax-resp-timeout-ms on-ajax-resp-timeout]
102+
:or
103+
{ ajax-resp-timeout-ms (* 60 1000)
104+
on-ajax-resp-timeout
105+
(fn [sch ring-req]
106+
(trove/log!
107+
{:level :warn, :id :sente.server.undertow/ajax-read-timeout,
108+
:data ring-req}))}}]
102109

103110
(UndertowServerChanAdapter.
104111
(assoc opts
105-
:ajax-resp-timeout-ms ajax-resp-timeout-ms))))
112+
:ajax-resp-timeout-ms ajax-resp-timeout-ms
113+
:on-ajax-resp-timeout on-ajax-resp-timeout))))

0 commit comments

Comments
 (0)