Commit b34a1a5
committed
fix: replace SimpleConnectionPool with ThreadedConnectionPool (#60)
SimpleConnectionPool is NOT thread-safe. FastAPI serves concurrent requests
across multiple threads, so two requests can receive the same connection
simultaneously — causing 'connection already in use' errors, data corruption,
and intermittent failures under load.
Changes:
- Replace SimpleConnectionPool with ThreadedConnectionPool (psycopg2's
built-in thread-safe pool that uses a threading.Lock internally)
- Add DB_POOL_MIN / DB_POOL_MAX env vars for pool size tuning (defaults: 2/10)
- Add close_pool() method and call it during app shutdown (lifespan)
- All existing code paths already use try/finally for connection return
Closes #601 parent 4541dc4 commit b34a1a5
1 file changed
Lines changed: 32 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
235 | 235 | | |
236 | 236 | | |
237 | 237 | | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
238 | 251 | | |
239 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
240 | 263 | | |
241 | 264 | | |
242 | 265 | | |
243 | 266 | | |
244 | 267 | | |
245 | 268 | | |
246 | | - | |
247 | | - | |
248 | | - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
249 | 274 | | |
250 | 275 | | |
251 | 276 | | |
| |||
259 | 284 | | |
260 | 285 | | |
261 | 286 | | |
262 | | - | |
| 287 | + | |
263 | 288 | | |
264 | 289 | | |
265 | 290 | | |
| |||
1646 | 1671 | | |
1647 | 1672 | | |
1648 | 1673 | | |
1649 | | - | |
| 1674 | + | |
| 1675 | + | |
1650 | 1676 | | |
1651 | 1677 | | |
1652 | 1678 | | |
| |||
0 commit comments