Skip to content

Commit

Permalink
[#259] TODO
Browse files Browse the repository at this point in the history
Quick sketch of a possible implementation.
Didn't test, or think about this much.
  • Loading branch information
ptaoussanis committed Aug 19, 2016
1 parent e729077 commit 4da5ece
Showing 1 changed file with 49 additions and 25 deletions.
74 changes: 49 additions & 25 deletions src/taoensso/sente.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@
send-buf-ms-ajax send-buf-ms-ws
user-id-fn csrf-token-fn handshake-data-fn packer]
:or {recv-buf-or-n (async/sliding-buffer 1000)
ws-kalive-ms (enc/ms :secs 25) ; < Heroku 55s timeout
ws-kalive-ms (enc/ms :secs 24) ; < Heroku 55s timeout
lp-timeout-ms (enc/ms :secs 20) ; < Heroku 30s timeout
send-buf-ms-ajax 100
send-buf-ms-ws 30
Expand Down Expand Up @@ -593,10 +593,11 @@
(when (interfaces/sch-open? server-ch)
;; (assert (= _sch server-ch))
(when (= udt-t1 udt-t0)
;; Ref. issue #230:
;; We've seen no send/recv activity on this
;; conn w/in our kalive window so send a ping
;; ->client (should auto-close conn if it's
;; gone dead)
;; gone dead).
(interfaces/sch-send! server-ch websocket?
(pack packer nil :chsk/ws-ping)))
(recur udt-t1))))))
Expand Down Expand Up @@ -929,14 +930,15 @@

[client-id chs params packer url
state_ ; {:type _ :open? _ :uid _ :csrf-token _ ...}
retry-handle_ retry-count_ ever-opened?_
instance-handle_ retry-count_ ever-opened?_
backoff-ms-fn ; (fn [nattempt]) -> msecs
cbs-waiting_ ; {<cb-uuid> <fn> ...}
socket_]
socket_
udt-last-comms_]

IChSocket
(-chsk-disconnect! [chsk reason]
(reset! retry-handle_ "_disable-auto-retry")
(reset! instance-handle_ nil) ; Disable auto retry
(swap-chsk-state! chsk #(chsk-state->closed % reason))
(when-let [s @socket_] (.close s 1000 "CLOSE_NORMAL")))

Expand Down Expand Up @@ -964,6 +966,7 @@

(try
(.send @socket_ ppstr)
(reset! udt-last-comms_ (now-udt))
:apparent-success
(catch :default e
(errorf e "Chsk send error")
Expand All @@ -980,8 +983,8 @@
(enc/oget goog/global "MozWebSocket")
(enc/oget @?node-npm-websocket_ "w3cwebsocket"))]

(let [retry-handle (enc/uuid-str)
have-handle? (fn [] (= @retry-handle_ retry-handle))
(let [instance-handle (reset! instance-handle_ (enc/uuid-str))
have-handle? (fn [] (= @instance-handle_ instance-handle))
connect-fn
(fn connect-fn []
(when (have-handle?)
Expand Down Expand Up @@ -1032,14 +1035,15 @@
;; whether they're wrapped or not
[clj ?cb-uuid] (unpack packer ppstr)]

(reset! udt-last-comms_ (now-udt))

(or
(when (handshake? clj)
(receive-handshake! :ws chsk clj)
(reset! retry-count_ 0))

(when (= clj :chsk/ws-ping)
(when @debug-mode?_
(receive-buffered-evs! chs [[:debug/ws-ping]]))
(receive-buffered-evs! chs [[:chsk/ws-ping]])
:noop)

(if-let [cb-uuid ?cb-uuid]
Expand Down Expand Up @@ -1077,7 +1081,27 @@
:last-ws-close last-ws-close))
(retry-fn))))))))))))]

(reset! retry-handle_ retry-handle)
;; TODO ws-kalive-ms, document that it should be > server val
(when-let [ms (ms :secs 27)]
(go-loop []
(let [udt-t0 @udt-last-comms_]
(<! (async/timeout ms))
(when (have-handle?)
(let [udt-t1 @udt-last-comms_]
(when (= udt-t0 udt-t1)
;; Ref. issue #259:
;; We've seen no send/recv activity on this
;; conn w/in our kalive window so send a ping
;; ->server (should auto-close conn if it's
;; gone dead). The server generally sends pings so
;; this should be rare. Mostly here to help clients
;; identify conns that were suddenly dropped.

;; TODO Confirm that a failed send will actually
;; trigger on-error or on-close
(-chsk-send! chsk [:chsk/ws-ping] {})))
(recur)))))

(reset! retry-count_ 0)
(connect-fn)
chsk)))))
Expand All @@ -1086,12 +1110,13 @@
(defn- new-ChWebSocket [opts]
(map->ChWebSocket
(merge
{:state_ (atom {:type :ws :open? false :ever-opened? false})
:retry-handle_ (atom "_pending")
:retry-count_ (atom 0)
:ever-opened?_ (atom false)
:cbs-waiting_ (atom {})
:socket_ (atom nil)}
{:state_ (atom {:type :ws :open? false :ever-opened? false})
:instance-handle_ (atom nil)
:retry-count_ (atom 0)
:ever-opened?_ (atom false)
:cbs-waiting_ (atom {})
:socket_ (atom nil)
:udt-last-comms_ (atom nil)}
opts))))

(def ^:private default-client-side-ajax-timeout-ms
Expand All @@ -1106,13 +1131,13 @@
;; Handles (re)polling, etc.

[client-id chs params packer url state_
retry-handle_ ever-opened?_
instance-handle_ ever-opened?_
backoff-ms-fn
ajax-opts curr-xhr_]

IChSocket
(-chsk-disconnect! [chsk reason]
(reset! retry-handle_ "_disable-auto-retry")
(reset! instance-handle_ nil) ; Disable auto retry
(swap-chsk-state! chsk #(chsk-state->closed % reason))
(when-let [x @curr-xhr_] (.abort x)))

Expand Down Expand Up @@ -1176,8 +1201,8 @@
:apparent-success))))

(-chsk-connect! [chsk]
(let [retry-handle (enc/uuid-str)
have-handle? (fn [] (= @retry-handle_ retry-handle))
(let [instance-handle (reset! instance-handle_ (enc/uuid-str))
have-handle? (fn [] (= @instance-handle_ instance-handle))
poll-fn ; async-poll-for-update-fn
(fn poll-fn [retry-count]
(tracef "async-poll-for-update!")
Expand Down Expand Up @@ -1249,18 +1274,17 @@
(let [buffered-evs clj] ; An application reply
(receive-buffered-evs! chs buffered-evs))))))))))))]

(reset! retry-handle_ retry-handle)
(poll-fn 0)
chsk))))

#?(:cljs
(defn- new-ChAjaxSocket [opts]
(map->ChAjaxSocket
(merge
{:state_ (atom {:type :ajax :open? false :ever-opened? false})
:retry-handle_ (atom "_pending")
:ever-opened?_ (atom false)
:curr-xhr_ (atom nil)}
{:state_ (atom {:type :ajax :open? false :ever-opened? false})
:instance-handle_ (atom nil)
:ever-opened?_ (atom false)
:curr-xhr_ (atom nil)}
opts))))

#?(:cljs
Expand Down

0 comments on commit 4da5ece

Please sign in to comment.