Skip to content

Commit

Permalink
Merge pull request #7585 from NBKelly/maybe-manage-users-better
Browse files Browse the repository at this point in the history
remove users from the lobby update list when they time out
  • Loading branch information
NoahTheDuke authored Jul 23, 2024
2 parents 3670a4d + d2791fc commit 0130493
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
29 changes: 18 additions & 11 deletions src/clj/web/app_state.clj
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
(ns web.app-state
(:require
[medley.core :refer [find-first]]))
[cljc.java-time.temporal.chrono-unit :as chrono]
[cljc.java-time.instant :as inst]
[medley.core :refer [dissoc-in find-first]]))

(defonce app-state
(atom {:lobbies {}
:lobby-updates {}
:users {}}))

(defonce lobby-subs-timeout-hours 6)

(defn register-user
[app-state uid user]
(-> app-state
(assoc-in [:users uid] (assoc user :uid uid))
(assoc-in [:lobby-updates uid] true)))
(assoc-in [:lobby-updates uid] (inst/now))))

(defn uid->lobby
([uid] (uid->lobby (:lobbies @app-state) uid))
Expand Down Expand Up @@ -52,21 +56,24 @@
[uid user]
(swap! app-state register-user uid user))

(defn deregister-user!
"Remove user from app-state. Mutates."
[uid]
(let [users (:users @app-state)
new-users (dissoc users uid)]
(swap! app-state #(assoc %1 :users new-users))))

(defn pause-lobby-updates
[uid]
(swap! app-state assoc-in [:lobby-updates uid] false))
(swap! app-state dissoc-in [:lobby-updates uid]))

(defn continue-lobby-updates
[uid]
(swap! app-state assoc-in [:lobby-updates uid] true))
(swap! app-state assoc-in [:lobby-updates uid] (inst/now)))

(defn receive-lobby-updates?
[uid]
(get-in @app-state [:lobby-updates uid] false))
(if-let [last-ping (get-in @app-state [:lobby-updates uid])]
(.isBefore (inst/now) (inst/plus last-ping lobby-subs-timeout-hours chrono/hours))
(do (pause-lobby-updates uid)
nil)))

(defn deregister-user!
"Remove user from app-state. Mutates."
[uid]
(pause-lobby-updates uid)
(swap! app-state dissoc-in [:users uid]))
28 changes: 22 additions & 6 deletions src/clj/web/telemetry.clj
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
(ns web.telemetry
(:require
[clojure.core.async :refer [<! go timeout]]
[web.app-state :refer [app-state]]
[web.lobby :refer [lobby-update-uids]]
[web.ws :refer [connected-sockets connections_]]
[taoensso.encore :as enc]
[taoensso.timbre :as timbre]))
[clojure.core.async :refer [<! go timeout]]
[cljc.java-time.temporal.chrono-unit :as chrono]
[cljc.java-time.duration :as duration]
[cljc.java-time.instant :as inst]
[web.app-state :refer [app-state]]
[web.lobby :refer [lobby-update-uids]]
[web.ws :refer [connected-sockets connections_]]
[taoensso.encore :as enc]
[taoensso.timbre :as timbre]))

(def log-stat-frequency (enc/ms :mins 5))

(defn subscriber-time-metrics
"average time | oldest"
[subs]
(let [now (inst/now)
age #(quot (duration/get (duration/between % now) chrono/seconds) 60)
subs-by-minute (sort (map age subs))
oldest (or (last subs-by-minute) 0)
average (quot (reduce + 0 subs-by-minute) (max 1 (count subs)))]
[average oldest]))

(defonce log-stats
(go (while true
(<! (timeout log-stat-frequency))
(let [lobbies-count (count (:lobbies @app-state))
user-cache-count (count (:users @app-state))
lobby-sub-count (count (filter identity (vals (:lobby-updates @app-state))))
lobby-update-uids (count (lobby-update-uids))
[average-sub-time oldest-sub-time] (subscriber-time-metrics (filter identity (vals (:lobby-updates @app-state))))
ajax-uid-count (count (:ajax @connected-sockets))
ajax-conn-counts (seq (map count (:ajax @connections_)))
ajax-conn-total (reduce + ajax-conn-counts)
Expand All @@ -28,6 +42,8 @@
" cached-users: " user-cache-count
" lobby-subs: " lobby-sub-count
" lobby-update-uids: " lobby-update-uids
" average-lobby-subs-lifetime: " average-sub-time "m"
" oldest-lobby-sub: " oldest-sub-time "m"
" | "
"websockets -"
" :ajax { "
Expand Down

0 comments on commit 0130493

Please sign in to comment.