performance: db/aiohttp connection pooling
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import os
|
||||
import random
|
||||
from typing import LiteralString, Optional, Union
|
||||
import aiosqlite as sqlite3
|
||||
from fastapi import FastAPI, Depends
|
||||
from fastapi_throttle import RateLimiter
|
||||
from fastapi.responses import JSONResponse
|
||||
from .constructors import RandMsgRequest
|
||||
import shared # Use shared SQLite pool
|
||||
|
||||
|
||||
class RandMsg(FastAPI):
|
||||
@@ -103,11 +103,11 @@ class RandMsg(FastAPI):
|
||||
}
|
||||
)
|
||||
|
||||
async with sqlite3.connect(database=randmsg_db_path, timeout=1) as _db:
|
||||
async with await _db.execute(db_query) as _cursor:
|
||||
if not isinstance(_cursor, sqlite3.Cursor):
|
||||
return JSONResponse(content={"err": True})
|
||||
result: Optional[sqlite3.Row] = await _cursor.fetchone()
|
||||
# Use shared SQLite pool for connection reuse
|
||||
sqlite_pool = shared.get_sqlite_pool()
|
||||
async with sqlite_pool.connection(randmsg_db_path, timeout=1) as _db:
|
||||
async with _db.execute(db_query) as _cursor:
|
||||
result = await _cursor.fetchone()
|
||||
if not result:
|
||||
return JSONResponse(content={"err": True})
|
||||
(result_id, result_msg) = result
|
||||
|
||||
Reference in New Issue
Block a user