mirror of
https://github.com/penpot/penpot.git
synced 2025-06-02 06:51:41 +02:00
✨ Improve websocket notifications metrics.
This commit is contained in:
parent
f8b349814c
commit
8236d84dfa
1 changed files with 65 additions and 56 deletions
|
@ -41,19 +41,32 @@
|
||||||
(defmethod ig/init-key ::handler
|
(defmethod ig/init-key ::handler
|
||||||
[_ {:keys [session metrics] :as cfg}]
|
[_ {:keys [session metrics] :as cfg}]
|
||||||
(let [wrap-session (:middleware session)
|
(let [wrap-session (:middleware session)
|
||||||
mtx-active-conn (mtx/create
|
|
||||||
{:name "http_ws_notifications_active_connections"
|
mtx-active-connections
|
||||||
:registry (:registry metrics)
|
(mtx/create
|
||||||
:type :gauge
|
{:name "websocket_notifications_active_connections"
|
||||||
:help "Active websocket connections on notifications service."})
|
:registry (:registry metrics)
|
||||||
mtx-msg-counter (mtx/create
|
:type :gauge
|
||||||
{:name "http_ws_notifications_message_counter"
|
:help "Active websocket connections on notifications service."})
|
||||||
:registry (:registry metrics)
|
|
||||||
:type :counter
|
mtx-message-recv
|
||||||
:help "Counter of total messages processed on websocket conenction on notifications service."})
|
(mtx/create
|
||||||
cfg (assoc cfg
|
{:name "websocket_notifications_message_recv_timing"
|
||||||
:mtx-active-conn mtx-active-conn
|
:registry (:registry metrics)
|
||||||
:mtx-msg-counter mtx-msg-counter)]
|
:type :summary
|
||||||
|
:help "Message receive summary timing (ms)."})
|
||||||
|
|
||||||
|
mtx-message-send
|
||||||
|
(mtx/create
|
||||||
|
{:name "websocket_notifications_message_send_timing"
|
||||||
|
:registry (:registry metrics)
|
||||||
|
:type :summary
|
||||||
|
:help "Message receive summary timing (ms)."})
|
||||||
|
|
||||||
|
cfg (assoc cfg
|
||||||
|
:mtx-active-connections mtx-active-connections
|
||||||
|
:mtx-message-recv mtx-message-recv
|
||||||
|
:mtx-message-send mtx-message-send)]
|
||||||
(-> #(handler cfg %)
|
(-> #(handler cfg %)
|
||||||
(wrap-session)
|
(wrap-session)
|
||||||
(wrap-keyword-params)
|
(wrap-keyword-params)
|
||||||
|
@ -100,18 +113,10 @@
|
||||||
;; WebSocket Http Handler
|
;; WebSocket Http Handler
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
|
||||||
(declare on-connect)
|
(declare handle-connect)
|
||||||
|
|
||||||
(defrecord WebSocket [conn in out sub])
|
(defrecord WebSocket [conn in out sub])
|
||||||
|
|
||||||
;; (defonce metrics-active-connections
|
|
||||||
;; (mtx/gauge {:id "notificatons__active_connections"
|
|
||||||
;; :help "Active connections to the notifications service."}))
|
|
||||||
|
|
||||||
;; (defonce metrics-message-counter
|
|
||||||
;; (mtx/counter {:id "notificatons__messages_counter"
|
|
||||||
;; :help "A total number of messages handled by the notifications service."}))
|
|
||||||
|
|
||||||
(defn- ws-send
|
(defn- ws-send
|
||||||
[conn data]
|
[conn data]
|
||||||
(try
|
(try
|
||||||
|
@ -122,51 +127,55 @@
|
||||||
false)))
|
false)))
|
||||||
|
|
||||||
(defn websocket
|
(defn websocket
|
||||||
[{:keys [file-id team-id redis mtx-active-conn mtx-msg-counter] :as cfg}]
|
[{:keys [file-id team-id redis] :as cfg}]
|
||||||
(let [in (a/chan 32)
|
(let [in (a/chan 32)
|
||||||
out (a/chan 32)]
|
out (a/chan 32)
|
||||||
{:on-connect
|
mtx-active-connections (:mtx-active-connections cfg)
|
||||||
(fn [conn]
|
mtx-message-send (:mtx-message-send cfg)
|
||||||
(mtx-active-conn :inc)
|
mtx-message-recv (:mtx-message-recv cfg)
|
||||||
(let [sub (rd/subscribe redis {:xform (map t/decode-str)
|
|
||||||
:topics [file-id team-id]})
|
|
||||||
ws (WebSocket. conn in out sub nil cfg)]
|
|
||||||
|
|
||||||
;; message forwarding loop
|
ws-send (mtx/wrap-summary ws-send mtx-message-send)]
|
||||||
(a/go-loop []
|
|
||||||
(let [val (a/<! out)]
|
|
||||||
(when-not (nil? val)
|
|
||||||
(when (ws-send conn (t/encode-str val))
|
|
||||||
(recur)))))
|
|
||||||
|
|
||||||
(a/go
|
(letfn [(on-connect [conn]
|
||||||
(a/<! (on-connect ws))
|
(mtx-active-connections :inc)
|
||||||
(a/close! sub))))
|
(let [sub (rd/subscribe redis {:xform (map t/decode-str)
|
||||||
|
:topics [file-id team-id]})
|
||||||
|
ws (WebSocket. conn in out sub nil cfg)]
|
||||||
|
|
||||||
:on-error
|
;; message forwarding loop
|
||||||
(fn [_conn _e]
|
(a/go-loop []
|
||||||
(a/close! out)
|
(let [val (a/<! out)]
|
||||||
(a/close! in))
|
(when-not (nil? val)
|
||||||
|
(when (ws-send conn (t/encode-str val))
|
||||||
|
(recur)))))
|
||||||
|
|
||||||
:on-close
|
(a/go
|
||||||
(fn [_conn _status _reason]
|
(a/<! (handle-connect ws))
|
||||||
(mtx-active-conn :dec)
|
(a/close! sub))))
|
||||||
(a/close! out)
|
|
||||||
(a/close! in))
|
|
||||||
|
|
||||||
:on-text
|
(on-error [_conn _e]
|
||||||
(fn [_ws message]
|
(a/close! out)
|
||||||
(mtx-msg-counter :inc)
|
(a/close! in))
|
||||||
(let [message (t/decode-str message)]
|
|
||||||
(a/>!! in message)))
|
|
||||||
|
|
||||||
:on-bytes
|
(on-close [_conn _status _reason]
|
||||||
(constantly nil)}))
|
(mtx-active-connections :dec)
|
||||||
|
(a/close! out)
|
||||||
|
(a/close! in))
|
||||||
|
|
||||||
|
(on-message [_ws message]
|
||||||
|
(let [message (t/decode-str message)]
|
||||||
|
(a/>!! in message)))]
|
||||||
|
|
||||||
|
{:on-connect on-connect
|
||||||
|
:on-error on-error
|
||||||
|
:on-close on-close
|
||||||
|
:on-text (mtx/wrap-summary on-message mtx-message-recv)
|
||||||
|
:on-bytes (constantly nil)})))
|
||||||
|
|
||||||
(declare handle-message)
|
(declare handle-message)
|
||||||
(declare start-loop!)
|
(declare start-loop!)
|
||||||
|
|
||||||
(defn- on-connect
|
(defn- handle-connect
|
||||||
[{:keys [conn] :as ws}]
|
[{:keys [conn] :as ws}]
|
||||||
(a/go
|
(a/go
|
||||||
(try
|
(try
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue