LCOV - code coverage report
Current view: top level - matchscore/realtime/events - core.clj Coverage Total Hit
Test: lcov.info Lines: 98.4 % 63 62
Test Date: 2026-11-23 18:11:10 Functions: - 0 0

            Line data    Source code
       1            4 : (ns matchscore.realtime.events.core
       2              :   (:require
       3              :    [matchscore.realtime.core :as realtime]
       4              :    [matchscore.realtime.rooms :as rooms]
       5              :    [matchscore.realtime.subscriptions :as subscriptions]
       6              :    [matchscore.utils.audit :as audit]
       7              :    [matchscore.utils.otel :as otel]
       8              :    [taoensso.telemere :as t])
       9              :   (:import
      10              :    (io.opentelemetry.api.trace SpanKind)))
      11              : 
      12              : 
      13            8 : (defmulti -event-msg-handler :id)
      14              : 
      15              : 
      16           12 : (defmethod -event-msg-handler :default
      17              :   [{:keys [event id]}]
      18           22 :   (t/log! :debug (str "Unhandled event: " id " " event)))
      19              : 
      20              : 
      21           12 : (defmethod -event-msg-handler :chsk/uidport-open
      22              :   [{:keys [uid]}]
      23           96 :   (t/log! :info (str "User connected: " uid)))
      24              : 
      25              : 
      26           12 : (defn- disconnect-client-id [client-id ?data] (or client-id (:client-id ?data)))
      27              : 
      28              : 
      29           12 : (defmethod -event-msg-handler :chsk/uidport-close
      30              :   [{:keys [?data uid ctx]
      31              :     :as   ev-msg}]
      32           96 :   (t/log! :info (str "User disconnected: " uid))
      33            2 :   (let [{:keys [redis sente instance-id]} ctx]
      34           11 :     (if-let [client-id (disconnect-client-id (:client-id ev-msg) ?data)]
      35            6 :       (let [removed     (rooms/remove-client-from-all-rooms! redis uid client-id)
      36              :             ;; Scope the offline to this pod: a stale close arriving after the
      37              :             ;; connection reconnected on another pod must not clobber it.
      38            3 :             event-uuids (subscriptions/unsubscribe-all-for-connection!
      39            1 :                          redis
      40            1 :                          uid
      41            1 :                          client-id
      42              :                          :offline
      43            1 :                          instance-id)]
      44           26 :         (doseq [{:keys [room-id display-name]} removed]
      45            5 :           (let [member-uuids (rooms/get-room-member-uuids redis room-id)]
      46            2 :             (realtime/relay-to-members!
      47            1 :              sente
      48            1 :              redis
      49            1 :              member-uuids
      50            7 :              [:room/left {:room-id      room-id
      51            1 :                           :user-uuid    uid
      52            1 :                           :display-name display-name}])))
      53           26 :         (doseq [event-uuid event-uuids]
      54            2 :           (realtime/relay-presence-change!
      55            1 :            sente
      56            1 :            redis
      57            1 :            event-uuid
      58            1 :            uid
      59            5 :            (subscriptions/get-user-devices redis event-uuid uid))))
      60           92 :       (t/log!
      61              :        :warn
      62            5 :        {:message "Missing client-id on disconnect; skipping scoped realtime cleanup"
      63            1 :         :uid     uid
      64            3 :         :event   (:id ev-msg)}))))
      65              : 
      66              : 
      67           12 : (defmethod -event-msg-handler :chsk/ws-ping [_])
      68              : 
      69              : 
      70           12 : (defmethod -event-msg-handler :chsk/ws-pong [_])
      71              : 
      72              : 
      73           19 : (defn- span-name [id] (str "sente." (or (namespace id) "chsk") "." (name id)))
      74              : 
      75              : 
      76            4 : (defn- extract-remote-context
      77              :   "Inbound Sente payloads from instrumented clients carry a `:_otel` map with
      78              :   W3C traceparent/tracestate. Extract it so the server span continues the
      79              :   trace started in the browser."
      80              :   [{:keys [?data]}]
      81           17 :   (when (map? ?data) (when-let [carrier (:_otel ?data)] (otel/extract-context carrier))))
      82              : 
      83              : 
      84            4 : (defn event-msg-handler
      85              :  "Wraps event dispatch with a CONSUMER span and error logging.
      86              : 
      87              :   The span continues a remote trace if the client included `:_otel` in the
      88              :   payload (see frontend `matchscore.realtime.core`)."
      89              :  [ev-msg]
      90            2 :  (let [{:keys [id uid]} ev-msg
      91            3 :        remote-ctx (extract-remote-context ev-msg)
      92            7 :        attrs      {:messaging.system    "sente"
      93              :                    :messaging.operation "process"
      94            3 :                    :sente.event-id      (str id)
      95            9 :                    :sente.uid           (some->
      96            1 :                                           uid
      97            1 :                                           str)}]
      98            2 :    (otel/with-span*
      99            3 :     (span-name id)
     100            1 :     attrs
     101            1 :     remote-ctx
     102            1 :     SpanKind/CONSUMER
     103            1 :     (fn []
     104            1 :      (try
     105              :        ;; Refresh this connection's presence heartbeat on every inbound message
     106              :        ;; (incl. Sente's ws keepalive) so live connections don't age out.
     107            8 :        (when-let [redis (:redis (:ctx ev-msg))]
     108            0 :          (subscriptions/touch-connection! redis uid (:client-id ev-msg)))
     109           13 :        (audit/with-actor uid (-event-msg-handler ev-msg))
     110              :        (catch Exception e
     111           92 :          (t/log!
     112              :           :error
     113            9 :           {:message   (str "Error handling event: " (:id ev-msg))
     114            1 :            :exception e})))))))
        

Generated by: LCOV version 2.0-1