Commit fbf4c63
committed
Release v0.7.2: Replace global locks with ETS-based locking for improved concurrency
This release replaces :global.trans-based locking with a custom
ETS-based locking mechanism in ConcurrencyGate and State modules,
improving performance and reliability under high concurrency.
Key improvements:
- ETS-based locking eliminates dependency on distributed Erlang's
global name server, which can introduce latency and contention in
single-node deployments
- Dedicated lock tables (gemini_concurrency_locks and
gemini_rate_limit_locks) track lock ownership per model/key with
automatic cleanup of dead lock holders
- Lock acquisition uses insert_new for atomic compare-and-set
semantics; contending processes spin-wait with 5ms intervals
- Dead lock holder cleanup uses delete_object to prevent TOCTOU races
when reclaiming locks from terminated processes
- Lock release is explicit and guaranteed via try/after blocks around
critical sections
Implementation details:
- ConcurrencyGate.with_lock/2 acquires model-specific lock before
permit operations (acquire, release, holder tracking)
- State.with_lock/2 acquires budget-key-specific lock before
reservation operations (try_reserve_budget, reconcile_reservation)
- Both modules create secondary ETS tables with write_concurrency: true
for lock storage during lazy initialization
- Lock acquisition retries indefinitely with Process.alive? checks to
detect and clean up stale lock holders
- Test coverage expanded with high-iteration concurrency repro script
(repro_concurrency_gate.exs) demonstrating no permit overlap across
500 iterations with 8 concurrent tasks
Performance characteristics:
- Reduced latency for uncontended lock acquisition (ETS insert_new vs
global name server round-trip)
- Bounded spin-wait interval (5ms) prevents CPU saturation during lock
contention
- Automatic lock cleanup eliminates need for manual lock recovery after
process crashes
Breaking changes: None
All existing tests pass; new test validates atomic reservation with
improved non_blocking mode timing to avoid flaky race conditions.1 parent 8ae9f9d commit fbf4c63
File tree
7 files changed
+211
-18
lines changed- lib/gemini/rate_limiter
- test/gemini
7 files changed
+211
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
8 | 19 | | |
9 | 20 | | |
10 | 21 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
| 41 | + | |
41 | 42 | | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
45 | | - | |
| 46 | + | |
46 | 47 | | |
47 | 48 | | |
48 | 49 | | |
| |||
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
57 | 70 | | |
58 | 71 | | |
59 | 72 | | |
| |||
76 | 89 | | |
77 | 90 | | |
78 | 91 | | |
79 | | - | |
| 92 | + | |
80 | 93 | | |
81 | 94 | | |
82 | 95 | | |
| |||
93 | 106 | | |
94 | 107 | | |
95 | 108 | | |
96 | | - | |
| 109 | + | |
97 | 110 | | |
98 | 111 | | |
99 | 112 | | |
| |||
118 | 131 | | |
119 | 132 | | |
120 | 133 | | |
121 | | - | |
| 134 | + | |
122 | 135 | | |
123 | 136 | | |
124 | 137 | | |
| |||
149 | 162 | | |
150 | 163 | | |
151 | 164 | | |
152 | | - | |
| 165 | + | |
153 | 166 | | |
154 | 167 | | |
155 | 168 | | |
| |||
173 | 186 | | |
174 | 187 | | |
175 | 188 | | |
176 | | - | |
| 189 | + | |
177 | 190 | | |
178 | 191 | | |
179 | 192 | | |
| |||
186 | 199 | | |
187 | 200 | | |
188 | 201 | | |
189 | | - | |
| 202 | + | |
190 | 203 | | |
191 | 204 | | |
192 | 205 | | |
| |||
217 | 230 | | |
218 | 231 | | |
219 | 232 | | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
220 | 238 | | |
221 | 239 | | |
222 | 240 | | |
| |||
335 | 353 | | |
336 | 354 | | |
337 | 355 | | |
338 | | - | |
| 356 | + | |
339 | 357 | | |
340 | 358 | | |
341 | 359 | | |
| |||
371 | 389 | | |
372 | 390 | | |
373 | 391 | | |
374 | | - | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
375 | 432 | | |
376 | 433 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
55 | | - | |
| 56 | + | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
| |||
66 | 67 | | |
67 | 68 | | |
68 | 69 | | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
69 | 82 | | |
70 | 83 | | |
71 | 84 | | |
| |||
181 | 194 | | |
182 | 195 | | |
183 | 196 | | |
184 | | - | |
| 197 | + | |
185 | 198 | | |
186 | 199 | | |
187 | 200 | | |
| |||
273 | 286 | | |
274 | 287 | | |
275 | 288 | | |
276 | | - | |
| 289 | + | |
277 | 290 | | |
278 | 291 | | |
279 | 292 | | |
| |||
334 | 347 | | |
335 | 348 | | |
336 | 349 | | |
337 | | - | |
| 350 | + | |
338 | 351 | | |
339 | 352 | | |
340 | 353 | | |
| |||
369 | 382 | | |
370 | 383 | | |
371 | 384 | | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
372 | 390 | | |
373 | 391 | | |
374 | 392 | | |
| |||
458 | 476 | | |
459 | 477 | | |
460 | 478 | | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
461 | 523 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
| 4 | + | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
0 commit comments