seawolf2357 commited on
Commit
6d05b3b
Β·
verified Β·
1 Parent(s): a13a457

Update npc_core.py

Browse files
Files changed (1) hide show
  1. 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(5) # μ΅œλŒ€ 5개 λ™μ‹œ DB μ—°κ²°
22
- _db_write_lock = asyncio.Semaphore(2) # μ΅œλŒ€ 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
- async with sem:
 
 
 
 
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=15.0):
41
- """Read-only shortcut β€” higher concurrency"""
42
- async with _db_semaphore:
 
 
 
 
43
  async with aiosqlite.connect(db_path, timeout=timeout) as db:
44
- await db.execute("PRAGMA busy_timeout=15000")
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():