aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/example
diff options
context:
space:
mode:
authorKostya <mail@sartin.in>2026-10-11 14:48:12 +0300
committerKostya <mail@sartin.in>2026-10-11 14:48:39 +0300
commit241fd5f0a46aff89a3302d2753a01e217a562e79 (patch)
treedf599ecef792055f4ad1b560f1a79082bb68e879 /src/test/java/com/example
parentcf13d4608acfb2d81ec0056717e561bb40f991a2 (diff)
downloadguess-market-241fd5f0a46aff89a3302d2753a01e217a562e79.tar.gz
guess-market-241fd5f0a46aff89a3302d2753a01e217a562e79.tar.xz
guess-market-241fd5f0a46aff89a3302d2753a01e217a562e79.zip
server: serialize engine access with one read/write lock
The engine was written for a single-threaded desktop app: users, events and ledgers live in plain lists, and a balance update is a read followed by a write. Tomcat runs each request on its own thread, so once clients poll while others trade, a read can iterate a list mid-insert and fail with a 500, and two deposits can both read the same old balance and lose one update. Every server-side call into the engine arrives through /api/*, so put one ReentrantReadWriteLock in a filter there. GETs, which is all the polling, share the read lock and still run in parallel. Everything else takes the write lock and changes the market alone, which also makes check-then-act steps like opening an event or claiming a username atomic. Writes are short in-memory operations, so one lock is enough. Per-event locks are the next step if trading volume ever needs them. The test runs writers depositing into one account while others poll three endpoints and log new users in. Without the filter it fails with 500s; with it, every request succeeds and the balance and ledger count match the number of deposits.
Diffstat (limited to 'src/test/java/com/example')
0 files changed, 0 insertions, 0 deletions