Spaces:
Running
Running
Update npc_core.py
Browse files- npc_core.py +21 -7
npc_core.py
CHANGED
|
@@ -18,14 +18,19 @@ KST = timezone(timedelta(hours=9))
|
|
| 18 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
# Global DB Connection Pool β database locked λ°©μ§
|
| 20 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
-
_db_semaphore = asyncio.Semaphore(
|
| 22 |
-
_db_write_lock = asyncio.Semaphore(2)
|
|
|
|
| 23 |
|
| 24 |
@asynccontextmanager
|
| 25 |
async def get_db(db_path, write=False, timeout=30.0):
|
| 26 |
"""Semaphore-controlled DB connection β prevents 'database is locked'"""
|
| 27 |
sem = _db_write_lock if write else _db_semaphore
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
async with aiosqlite.connect(db_path, timeout=timeout) as db:
|
| 30 |
await db.execute("PRAGMA busy_timeout=30000")
|
| 31 |
await db.execute("PRAGMA journal_mode=WAL")
|
|
@@ -35,15 +40,24 @@ async def get_db(db_path, write=False, timeout=30.0):
|
|
| 35 |
await db.commit()
|
| 36 |
except Exception:
|
| 37 |
pass
|
|
|
|
|
|
|
| 38 |
|
| 39 |
@asynccontextmanager
|
| 40 |
-
async def get_db_read(db_path, timeout=
|
| 41 |
-
"""Read-only
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
async with aiosqlite.connect(db_path, timeout=timeout) as db:
|
| 44 |
-
await db.execute("PRAGMA busy_timeout=
|
| 45 |
await db.execute("PRAGMA journal_mode=WAL")
|
|
|
|
| 46 |
yield db
|
|
|
|
|
|
|
| 47 |
def get_current_time_kst():
|
| 48 |
return datetime.now(KST)
|
| 49 |
def get_current_date_kst():
|
|
|
|
| 18 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 19 |
# Global DB Connection Pool β database locked λ°©μ§
|
| 20 |
# ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 21 |
+
_db_semaphore = asyncio.Semaphore(20) # μΌλ° DB μ°κ²° (μ€μΌμ€λ¬+importedν¨μ)
|
| 22 |
+
_db_write_lock = asyncio.Semaphore(2) # μ΅λ 2κ° λμ μ°κΈ°
|
| 23 |
+
_db_read_sem = asyncio.Semaphore(30) # API μ½κΈ° μ μ© β WALμ 무μ ν λμ μ½κΈ° μ§μ
|
| 24 |
|
| 25 |
@asynccontextmanager
|
| 26 |
async def get_db(db_path, write=False, timeout=30.0):
|
| 27 |
"""Semaphore-controlled DB connection β prevents 'database is locked'"""
|
| 28 |
sem = _db_write_lock if write else _db_semaphore
|
| 29 |
+
try:
|
| 30 |
+
await asyncio.wait_for(sem.acquire(), timeout=15.0)
|
| 31 |
+
except asyncio.TimeoutError:
|
| 32 |
+
raise Exception(f"DB {'write' if write else 'read'} semaphore timeout β server busy")
|
| 33 |
+
try:
|
| 34 |
async with aiosqlite.connect(db_path, timeout=timeout) as db:
|
| 35 |
await db.execute("PRAGMA busy_timeout=30000")
|
| 36 |
await db.execute("PRAGMA journal_mode=WAL")
|
|
|
|
| 40 |
await db.commit()
|
| 41 |
except Exception:
|
| 42 |
pass
|
| 43 |
+
finally:
|
| 44 |
+
sem.release()
|
| 45 |
|
| 46 |
@asynccontextmanager
|
| 47 |
+
async def get_db_read(db_path, timeout=10.0):
|
| 48 |
+
"""Read-only β separate high-concurrency semaphore (WAL supports unlimited reads)"""
|
| 49 |
+
try:
|
| 50 |
+
await asyncio.wait_for(_db_read_sem.acquire(), timeout=5.0)
|
| 51 |
+
except asyncio.TimeoutError:
|
| 52 |
+
raise Exception("DB read semaphore timeout β server busy")
|
| 53 |
+
try:
|
| 54 |
async with aiosqlite.connect(db_path, timeout=timeout) as db:
|
| 55 |
+
await db.execute("PRAGMA busy_timeout=10000")
|
| 56 |
await db.execute("PRAGMA journal_mode=WAL")
|
| 57 |
+
await db.execute("PRAGMA query_only=ON")
|
| 58 |
yield db
|
| 59 |
+
finally:
|
| 60 |
+
_db_read_sem.release()
|
| 61 |
def get_current_time_kst():
|
| 62 |
return datetime.now(KST)
|
| 63 |
def get_current_date_kst():
|