0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-22 22:49:01 -05:00

Improve logging on websocket related code (backend)

This commit is contained in:
Andrey Antukh 2023-12-14 16:24:35 +01:00 committed by Alonso Torres
parent 7648836725
commit ecee15af5b
2 changed files with 19 additions and 17 deletions

View file

@ -103,7 +103,7 @@
{:on-open {:on-open
(fn on-open [channel] (fn on-open [channel]
(l/trace :fn "on-open" :conn-id id :channel channel) (l/dbg :fn "on-open" :conn-id (str id))
(let [options (-> options (let [options (-> options
(assoc ::channel channel) (assoc ::channel channel)
(on-connect)) (on-connect))
@ -114,10 +114,10 @@
:on-close :on-close
(fn on-close [_channel code reason] (fn on-close [_channel code reason]
(l/info :fn "on-ws-terminate" (l/dbg :fn "on-close"
:conn-id id :conn-id (str id)
:code code :code code
:reason reason) :reason reason)
(sp/close! close-ch)) (sp/close! close-ch))
:on-error :on-error
@ -132,18 +132,19 @@
:on-pong :on-pong
(fn on-pong [_channel data] (fn on-pong [_channel data]
(l/trace :fn "on-pong" :data data)
(sp/put! hbeat-ch data))})) (sp/put! hbeat-ch data))}))
(defn- handle-ping! (defn- handle-ping!
[{:keys [::id ::beats ::channel] :as wsp} beat-id] [{:keys [::id ::beats ::channel] :as wsp} beat-id]
(l/trace :hint "send ping" :beat beat-id :conn-id id) (l/trc :hint "send ping" :beat beat-id :conn-id (str id))
(rws/ping channel (encode-beat beat-id)) (rws/ping channel (encode-beat beat-id))
(let [issued (swap! beats conj (long beat-id))] (let [issued (swap! beats conj (long beat-id))]
(not (>= (count issued) max-missed-heartbeats)))) (not (>= (count issued) max-missed-heartbeats))))
(defn- start-io-loop! (defn- start-io-loop!
[{:keys [::id ::close-ch ::input-ch ::output-ch ::heartbeat-ch ::channel ::handler ::beats ::on-rcv-message ::on-snd-message] :as wsp}] [{:keys [::id ::close-ch ::input-ch ::output-ch ::heartbeat-ch
::channel ::handler ::beats ::on-rcv-message ::on-snd-message]
:as wsp}]
(try (try
(handler wsp {:type :open}) (handler wsp {:type :open})
(loop [i 0] (loop [i 0]
@ -154,14 +155,16 @@
(identical? p ping-ch) (identical? p ping-ch)
(if (handle-ping! wsp i) (if (handle-ping! wsp i)
(recur (inc i)) (recur (inc i))
(rws/close channel 8802 "missing to many pings")) (do
(l/trc :hint "closing" :reason "missing to many pings")
(rws/close channel 8802 "missing to many pings")))
(or (identical? p close-ch) (nil? msg)) (or (identical? p close-ch) (nil? msg))
(do :nothing) (do :nothing)
(identical? p heartbeat-ch) (identical? p heartbeat-ch)
(let [beat (decode-beat msg)] (let [beat (decode-beat msg)]
;; (l/trace :hint "pong" :beat beat :conn-id id) (l/trc :hint "pong received" :beat beat :conn-id (str id))
(swap! beats disj beat) (swap! beats disj beat)
(recur i)) (recur i))
@ -179,7 +182,6 @@
(identical? p output-ch) (identical? p output-ch)
(let [message (on-snd-message msg) (let [message (on-snd-message msg)
message (t/encode-str message {:type :json-verbose})] message (t/encode-str message {:type :json-verbose})]
;; (l/trace :hint "writing message to output" :message msg)
(rws/send channel message) (rws/send channel message)
(recur i)))))) (recur i))))))
@ -188,12 +190,12 @@
(catch java.io.IOException _) (catch java.io.IOException _)
(catch InterruptedException _cause (catch InterruptedException _cause
(l/debug :hint "websocket thread interrumpted" :conn-id id)) (l/dbg :hint "websocket thread interrumpted" :conn-id id))
(catch Throwable cause (catch Throwable cause
(l/error :hint "unhandled exception on websocket thread" (l/err :hint "unhandled exception on websocket thread"
:conn-id id :conn-id id
:cause cause)) :cause cause))
(finally (finally
(try (try
(handler wsp {:type :close}) (handler wsp {:type :close})
@ -212,4 +214,4 @@
(catch Throwable cause (catch Throwable cause
(throw cause))) (throw cause)))
(l/trace :hint "websocket thread terminated" :conn-id id)))) (l/trc :hint "websocket thread terminated" :conn-id id))))

View file

@ -44,7 +44,7 @@
(ptk/reify ::initialize (ptk/reify ::initialize
ptk/WatchEvent ptk/WatchEvent
(watch [_ state stream] (watch [_ state stream]
(l/trace :hint "event:initialize" :fn "watch") (l/trace :hint "initialize" :fn "watch")
(let [sid (:session-id state) (let [sid (:session-id state)
uri (prepare-uri {:session-id sid}) uri (prepare-uri {:session-id sid})
ws (ws/create uri)] ws (ws/create uri)]