Line data Source code
1 4 : (ns matchscore.realtime.core
2 : (:require
3 : [matchscore.realtime.subscriptions :as subscriptions]
4 : [taoensso.carmine :as car]
5 : [taoensso.telemere :as t]))
6 :
7 :
8 4 : (defn send!
9 : "Send an event to a specific user by UUID."
10 : [sente user-uuid event]
11 0 : ((:send-fn sente) user-uuid event))
12 :
13 :
14 4 : (defn broadcast!
15 : "Send an event to all connected users."
16 : [sente event]
17 8 : (let [uids (:any @(:connected-uids sente))]
18 94 : (t/log!
19 : :info
20 0 : ["Broadcasting to UIDs"
21 0 : {:uids uids
22 0 : :event-id (first event)}])
23 26 : (doseq [uid uids]
24 22 : (t/log! :debug ["Sending to UID" uid])
25 6 : ((:send-fn sente) uid event))))
26 :
27 :
28 4 : (defn connected-uids
29 : "Return the set of connected user UUIDs."
30 : [sente]
31 7 : (:any @(:connected-uids sente)))
32 :
33 :
34 4 : (defn send-to-room!
35 : "Send an event to room members that are currently connected."
36 : [sente member-uuids event]
37 4 : (let [online (connected-uids sente)
38 4 : targets (filter online member-uuids)]
39 32 : (doseq [uid targets] ((:send-fn sente) uid event))))
40 :
41 :
42 4 : (defn cross-replica-connected-uids
43 : "Return the union of connected user UUIDs across all instances by
44 : scanning sente:connected:* keys in Redis."
45 : [redis]
46 5 : (let [keys (car/scan-keys redis "sente:connected:*")]
47 47 : (if (empty? keys) #{} (set (car/wcar redis (apply car/sunion keys))))))
48 :
49 :
50 4 : (defn relay!
51 : "Publish a push event to the sente:fanout Redis pub/sub channel so all
52 : instances can deliver it to their locally-connected targets."
53 : [redis targets event]
54 10 : (when (seq targets)
55 70 : (car/wcar
56 2 : redis
57 6 : (car/publish
58 : "sente:fanout"
59 4 : (pr-str
60 8 : {:targets targets
61 2 : :event event})))))
62 :
63 :
64 4 : (defn relay-to-members!
65 : "Deliver `event` to `member-uuids` across the whole cluster via the Redis
66 : `sente:fanout` channel: every instance's relay listener (incl. this one)
67 : delivers it to its own locally-connected members. Routing through Redis even
68 : for members connected to THIS instance is what lets a single user's
69 : connections spread across pods all receive the event — a per-uid local/remote
70 : split would deliver only to the originating pod's clients of that uid and
71 : silently drop the user's other devices. No-op when the member list is empty."
72 : [_sente redis member-uuids event]
73 0 : (relay! redis (vec member-uuids) event))
74 :
75 :
76 4 : (defn relay-broadcast!
77 : "Broadcast `event` to every user connected to ANY instance (the cross-replica
78 : union), delivering locally where possible and fanning the rest out via Redis."
79 : [sente redis event]
80 0 : (relay-to-members! sente redis (cross-replica-connected-uids redis) event))
81 :
82 :
83 4 : (defn relay-to-event!
84 : "Deliver a push `event` to all online subscribers of `event-uuid` across the
85 : whole cluster via the Redis `sente:fanout` channel. Every instance's relay
86 : listener — including this one — delivers the event to its own
87 : locally-connected subscribers. Delivery goes through Redis even for
88 : subscribers connected to THIS instance: a single user can hold connections on
89 : several pods at once, and a per-uid local/remote split would deliver only to
90 : the originating pod's clients of that uid, silently dropping the user's other
91 : devices (e.g. an `:away` presence flip never reaching the user's other tabs).
92 : No-op when nobody is subscribed."
93 : [_sente redis event-uuid event]
94 10 : (let [subscribers (subscriptions/get-subscribers redis event-uuid)]
95 184 : (t/log!
96 : :info
97 12 : (str
98 : "Delivering "
99 6 : (first event)
100 : " for event "
101 2 : event-uuid
102 : " to "
103 4 : (count subscribers)
104 : " subscriber(s) via fanout"))
105 14 : (relay! redis (vec subscribers) event)))
106 :
107 :
108 4 : (defn relay-presence-change!
109 : "Broadcast that `user-uuid`'s connection set for `event-uuid` changed to all
110 : online subscribers of the event. `devices` is the user's current device list
111 : (empty = the user is now offline for this event)."
112 : [sente redis event-uuid user-uuid devices]
113 2 : (relay-to-event!
114 1 : sente
115 1 : redis
116 1 : event-uuid
117 7 : [:event/presence-changed {:event-uuid event-uuid
118 1 : :user-uuid user-uuid
119 1 : :devices devices}]))
|