diff options
| author | Kostya <mail@sartin.in> | 2026-10-11 14:48:12 +0300 |
|---|---|---|
| committer | Kostya <mail@sartin.in> | 2026-10-11 14:48:39 +0300 |
| commit | 241fd5f0a46aff89a3302d2753a01e217a562e79 (patch) | |
| tree | df599ecef792055f4ad1b560f1a79082bb68e879 /server/src/main | |
| parent | cf13d4608acfb2d81ec0056717e561bb40f991a2 (diff) | |
| download | guess-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 'server/src/main')
| -rw-r--r-- | server/src/main/java/market/guess/server/EngineLockFilter.java | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/server/src/main/java/market/guess/server/EngineLockFilter.java b/server/src/main/java/market/guess/server/EngineLockFilter.java new file mode 100644 index 0000000..181d96b --- /dev/null +++ b/server/src/main/java/market/guess/server/EngineLockFilter.java | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | package market.guess.server; | ||
| 2 | |||
| 3 | import jakarta.servlet.FilterChain; | ||
| 4 | import jakarta.servlet.ServletException; | ||
| 5 | import jakarta.servlet.annotation.WebFilter; | ||
| 6 | import jakarta.servlet.http.HttpFilter; | ||
| 7 | import jakarta.servlet.http.HttpServletRequest; | ||
| 8 | import jakarta.servlet.http.HttpServletResponse; | ||
| 9 | import java.io.IOException; | ||
| 10 | import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
| 11 | |||
| 12 | @WebFilter("/api/*") | ||
| 13 | public final class EngineLockFilter extends HttpFilter { | ||
| 14 | private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
| 15 | |||
| 16 | @Override | ||
| 17 | protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) | ||
| 18 | throws IOException, ServletException { | ||
| 19 | var held = "GET".equals(req.getMethod()) ? lock.readLock() : lock.writeLock(); | ||
| 20 | held.lock(); | ||
| 21 | try { | ||
| 22 | chain.doFilter(req, res); | ||
| 23 | } finally { | ||
| 24 | held.unlock(); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||