forum : verrou limité aux appels SQLite (connexion verrouillée), plus de verrou pendant la lecture réseau ; délai de 30 s sur les sockets
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
6462b41b30
révision
f673b752b3
3 fichiers modifiés
+56
-22
Aucun fichier ne correspond à votre recherche
+54
-1
@@ -1,5 +1,6 @@
|
|||||||
"""Schéma SQLite et accès aux données (version 4 : + sessions Gastinel, demandes d'adhésion, photos de la fiche station)."""
|
"""Schéma SQLite et accès aux données (version 4 : + sessions Gastinel, demandes d'adhésion, photos de la fiche station)."""
|
||||||
import sqlite3
|
import sqlite3
|
||||||
|
import threading
|
||||||
|
|
||||||
SCHEMA = """
|
SCHEMA = """
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
@@ -129,6 +130,58 @@ SYSTEME = "systeme" # compte technique, auteur du sujet fixe
|
|||||||
BULLETIN = "Bulletin de la semaine du Vercors"
|
BULLETIN = "Bulletin de la semaine du Vercors"
|
||||||
|
|
||||||
|
|
||||||
|
class _Resultat:
|
||||||
|
"""Résultat déjà lu sous le verrou (les lignes ne dépendent plus de la connexion partagée)."""
|
||||||
|
def __init__(self, lignes, rowcount, lastrowid):
|
||||||
|
self._l, self.rowcount, self.lastrowid = lignes, rowcount, lastrowid
|
||||||
|
def fetchone(self):
|
||||||
|
return self._l.pop(0) if self._l else None
|
||||||
|
def fetchall(self):
|
||||||
|
l, self._l = self._l, []
|
||||||
|
return l
|
||||||
|
def __iter__(self):
|
||||||
|
return iter(self.fetchall())
|
||||||
|
|
||||||
|
|
||||||
|
class ConnexionVerrouillee:
|
||||||
|
"""Connexion SQLite partagée entre les fils du serveur : chaque appel passe sous un verrou et lit
|
||||||
|
ses lignes tout de suite. Le verrou ne couvre que la base, jamais la lecture du réseau."""
|
||||||
|
def __init__(self, con):
|
||||||
|
self._con, self.verrou = con, threading.RLock()
|
||||||
|
def execute(self, sql, params=()):
|
||||||
|
with self.verrou:
|
||||||
|
c = self._con.execute(sql, params)
|
||||||
|
return _Resultat(c.fetchall(), c.rowcount, c.lastrowid)
|
||||||
|
def executemany(self, sql, seq):
|
||||||
|
with self.verrou:
|
||||||
|
c = self._con.executemany(sql, seq)
|
||||||
|
return _Resultat([], c.rowcount, c.lastrowid)
|
||||||
|
def executescript(self, sql):
|
||||||
|
with self.verrou:
|
||||||
|
return self._con.executescript(sql)
|
||||||
|
def commit(self):
|
||||||
|
with self.verrou:
|
||||||
|
self._con.commit()
|
||||||
|
def rollback(self):
|
||||||
|
with self.verrou:
|
||||||
|
self._con.rollback()
|
||||||
|
def __enter__(self): # « with con: » = une transaction entière sous le verrou
|
||||||
|
self.verrou.acquire()
|
||||||
|
try:
|
||||||
|
self._con.__enter__()
|
||||||
|
except BaseException:
|
||||||
|
self.verrou.release()
|
||||||
|
raise
|
||||||
|
return self
|
||||||
|
def __exit__(self, *exc):
|
||||||
|
try:
|
||||||
|
return self._con.__exit__(*exc)
|
||||||
|
finally:
|
||||||
|
self.verrou.release()
|
||||||
|
def __getattr__(self, nom):
|
||||||
|
return getattr(self._con, nom)
|
||||||
|
|
||||||
|
|
||||||
def connecter(chemin=":memory:"):
|
def connecter(chemin=":memory:"):
|
||||||
con = sqlite3.connect(chemin, check_same_thread=False)
|
con = sqlite3.connect(chemin, check_same_thread=False)
|
||||||
con.row_factory = sqlite3.Row
|
con.row_factory = sqlite3.Row
|
||||||
@@ -192,4 +245,4 @@ def connecter(chemin=":memory:"):
|
|||||||
con.execute("INSERT INTO topics(title, author_id, fixed, pinned, pin_pos, replies_open)"
|
con.execute("INSERT INTO topics(title, author_id, fixed, pinned, pin_pos, replies_open)"
|
||||||
" VALUES (?, ?, 1, 1, 0, 0)", (BULLETIN, sid))
|
" VALUES (?, ?, 1, 1, 0, 0)", (BULLETIN, sid))
|
||||||
con.commit()
|
con.commit()
|
||||||
return con
|
return ConnexionVerrouillee(con)
|
||||||
+1
-11
@@ -11,7 +11,6 @@ Second serveur, sur la même base que le forum (python forum/panneau.py --port 8
|
|||||||
Variables d'environnement : celles du forum (FORUM_HUB_URL, FORUM_CLIENT_ID, FORUM_OAUTH_SECRET[_FILE],
|
Variables d'environnement : celles du forum (FORUM_HUB_URL, FORUM_CLIENT_ID, FORUM_OAUTH_SECRET[_FILE],
|
||||||
FORUM_PROPRIETAIRE_ID) + FORUM_PANNEAU_REDIRECT_URI (défaut : http://localhost:<port>/connexion/retour).
|
FORUM_PROPRIETAIRE_ID) + FORUM_PANNEAU_REDIRECT_URI (défaut : http://localhost:<port>/connexion/retour).
|
||||||
"""
|
"""
|
||||||
import threading
|
|
||||||
import argparse
|
import argparse
|
||||||
import hashlib
|
import hashlib
|
||||||
import hmac
|
import hmac
|
||||||
@@ -317,16 +316,7 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
def creer_serveur(forum, port=0, hote="127.0.0.1", reglages=None):
|
def creer_serveur(forum, port=0, hote="127.0.0.1", reglages=None):
|
||||||
h = type("H", (Handler,), {"app": App(forum, reglages or connexion.Reglages())})
|
h = type("H", (Handler,), {"app": App(forum, reglages or connexion.Reglages())})
|
||||||
# Une seule connexion SQLite partagée entre les fils : les requêtes passent une par une
|
h.timeout = 30 # socket muette plus de 30 s : connexion fermée
|
||||||
# (sinon « bad parameter or other API misuse » et connexion coupée quand plusieurs arrivent ensemble).
|
|
||||||
VERROU_BASE = threading.RLock()
|
|
||||||
for nom in ("do_GET", "do_POST", "do_HEAD"):
|
|
||||||
orig = getattr(h, nom, None)
|
|
||||||
if orig is not None:
|
|
||||||
def enveloppe(self, _orig=orig):
|
|
||||||
with VERROU_BASE:
|
|
||||||
return _orig(self)
|
|
||||||
setattr(h, nom, enveloppe)
|
|
||||||
return ThreadingHTTPServer((hote, port), h)
|
return ThreadingHTTPServer((hote, port), h)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-10
@@ -2049,16 +2049,7 @@ def form_supprimer(action, champ, ident, classe="suppr", resume="Supprimer", aid
|
|||||||
|
|
||||||
def creer_serveur(forum, port=0, hote="127.0.0.1", reglages=None):
|
def creer_serveur(forum, port=0, hote="127.0.0.1", reglages=None):
|
||||||
h = type("H", (Handler,), {"app": App(forum, reglages or connexion.Reglages())})
|
h = type("H", (Handler,), {"app": App(forum, reglages or connexion.Reglages())})
|
||||||
# Une seule connexion SQLite partagée entre les fils : les requêtes passent une par une
|
h.timeout = 30 # socket muette plus de 30 s : connexion fermée
|
||||||
# (sinon « bad parameter or other API misuse » et connexion coupée quand plusieurs arrivent ensemble).
|
|
||||||
VERROU_BASE = threading.RLock()
|
|
||||||
for nom in ("do_GET", "do_POST", "do_HEAD"):
|
|
||||||
orig = getattr(h, nom, None)
|
|
||||||
if orig is not None:
|
|
||||||
def enveloppe(self, _orig=orig):
|
|
||||||
with VERROU_BASE:
|
|
||||||
return _orig(self)
|
|
||||||
setattr(h, nom, enveloppe)
|
|
||||||
return ThreadingHTTPServer((hote, port), h)
|
return ThreadingHTTPServer((hote, port), h)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Référencer dans un nouveau ticket
Bloquer un utilisateur