Skip to content

Commit

Permalink
add cas control with participate handler
Browse files Browse the repository at this point in the history
  • Loading branch information
humorless committed Jul 23, 2024
1 parent 611608e commit 2b5a7fa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
22 changes: 16 additions & 6 deletions repl-sessions/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,30 @@

(def req {:identity "ccc"
:path-params {:id "17592186045455"}})
(def session (merge {:session/capacity 14}
(db/entity (parse-long (get-in req [:path-params :id])))))

(def session-eid 17592186045455)

@(db/transact [[:db/add session-eid :session/capacity 14]
[:db/add session-eid :session/signup 0]])

(def session (db/entity (parse-long (get-in req [:path-params :id]))))

(let [user-id-str (:identity req)
session-eid (parse-long (get-in req [:path-params :id]))
;; session (db/entity session-eid)
capacity (:session/capacity session)
curr-participants (:session/participants session)]
curr-participants (:session/participants session)
signup (:session/signup session)
new-signup (inc signup)]
;;TODO
;; Write some code to handle the case that :db/cas throws exception at race condition
(prn :session-eid session-eid)
(prn :capacity capacity)
(prn :curr-ps curr-participants)
(prn :check (< (count curr-participants) capacity))
(if (< (count curr-participants) capacity)
@(db/transact [[:db/add session-eid :session/participants user-id-str]])
(prn :check (< signup capacity))
(prn :debug-tx [[:db/cas session-eid :session/signup signup new-signup]
[:db/add session-eid :session/participants user-id-str]])
(if (< signup capacity)
@(db/transact [[:db/cas session-eid :session/signup signup new-signup]
[:db/add session-eid :session/participants user-id-str]])
{:html/body "No enough capacity for this session"}))
9 changes: 6 additions & 3 deletions src/co/gaiwan/compass/routes/sessions.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,13 @@
session-eid (parse-long (get-in req [:path-params :id]))
session (db/entity session-eid)
capacity (:session/capacity session)
current-participants (:session/participants session)]
(if (< (count current-participants) capacity)
signup (:session/signup session)
new-signup (inc signup)]
(if (< signup capacity)
(do
@(db/transact [[:db/add session-eid :session/participants user-id-str]])
;;TODO: add try/catch to handle :db/cas
@(db/transact [[:db/cas session-eid :session/signup signup new-signup]
[:db/add session-eid :session/participants user-id-str]])
{:html/body "success"})
{:html/body "No enough capacity for this session"}))
{:html/body (pr-str (db/entity (parse-long (get-in req [:path-params :id]))))})))
Expand Down

0 comments on commit 2b5a7fa

Please sign in to comment.