fix: threading lock in Database for concurrent writes
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
|
||||
import json
|
||||
import sqlite3
|
||||
import threading
|
||||
from datetime import datetime, timezone
|
||||
from pathlib import Path
|
||||
|
||||
@@ -21,6 +22,7 @@ class Database:
|
||||
self.conn.row_factory = sqlite3.Row
|
||||
self.conn.execute("PRAGMA journal_mode=WAL")
|
||||
self.conn.execute("PRAGMA busy_timeout=30000")
|
||||
self._lock = threading.Lock()
|
||||
self._init_schema()
|
||||
|
||||
def __enter__(self):
|
||||
@@ -175,6 +177,7 @@ class Database:
|
||||
|
||||
Если request_id передан и уже существует — silently return (идемпотентность).
|
||||
"""
|
||||
with self._lock:
|
||||
ci = client_info
|
||||
|
||||
# Подсчёт DTC/PID из ответов
|
||||
@@ -213,7 +216,6 @@ class Database:
|
||||
llm_duration_ms, llm_success,
|
||||
raw_responses, request_id, response_json
|
||||
) VALUES (?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?, ?,?,?, ?,?,?, ?,?,?, ?,?, ?,?,?,?, ?,?,?,?,?)
|
||||
|
||||
""", (
|
||||
ci.get("client_ip"), ci.get("real_ip"), ci.get("user_agent"),
|
||||
ci.get("content_length"),
|
||||
@@ -244,6 +246,7 @@ class Database:
|
||||
|
||||
def save_dtc_scan(self, client_info: dict, dtc_codes: list[str]):
|
||||
"""Сохраняет быстрый скан кодов ошибок."""
|
||||
with self._lock:
|
||||
self.conn.execute("""
|
||||
INSERT INTO sessions (
|
||||
client_ip, real_ip, user_agent,
|
||||
@@ -285,6 +288,7 @@ class Database:
|
||||
|
||||
profile — результат obd.probe.probe().
|
||||
"""
|
||||
with self._lock:
|
||||
now = datetime.now(timezone.utc).isoformat()
|
||||
self.conn.execute("""
|
||||
INSERT INTO device_profiles
|
||||
|
||||
Reference in New Issue
Block a user