Make geo lookups non-blocking with bulk DB writes and background tasks
This commit is contained in:
@@ -50,10 +50,17 @@ This document breaks the entire BanGUI project into development stages, ordered
|
||||
|
||||
---
|
||||
|
||||
## Task 3 — Non-Blocking Web Requests & Bulk DB Operations
|
||||
## Task 3 — Non-Blocking Web Requests & Bulk DB Operations ✅ DONE
|
||||
|
||||
**Goal:** Ensure the web UI remains responsive while geo-IP lookups and database writes are in progress.
|
||||
|
||||
**Resolution:**
|
||||
- **Bulk DB writes:** `geo_service.lookup_batch` now collects resolved IPs into `pos_rows` / `neg_ips` lists across the chunk loop and flushes them with two `executemany` calls per chunk instead of one `execute` per IP.
|
||||
- **`lookup_cached_only`:** New function that returns `(geo_map, uncached)` immediately from the in-memory + SQLite cache with no API calls. Used by `bans_by_country` for its hot path.
|
||||
- **Background geo resolution:** `bans_by_country` calls `lookup_cached_only` for an instant response, then fires `asyncio.create_task(geo_service.lookup_batch(uncached, …))` to populate the cache in the background for subsequent requests.
|
||||
- **Batch enrichment for `get_active_bans`:** `jail_service.get_active_bans` now accepts `http_session` / `app_db` and resolves all banned IPs in a single `lookup_batch` call (chunked 100-IP batches) instead of firing one coroutine per IP through `asyncio.gather`.
|
||||
- 12 new tests across `test_geo_service.py`, `test_jail_service.py`, and `test_ban_service.py`; `ruff` and `mypy --strict` clean; 145 tests pass.
|
||||
|
||||
**Details:**
|
||||
|
||||
- After the geo-IP service was integrated, web UI requests became slow or appeared to hang because geo lookups and individual DB writes block the async event loop.
|
||||
|
||||
Reference in New Issue
Block a user