This commit is contained in:
retoor 2025-06-14 09:15:08 +02:00
parent 52538d0181
commit a4d29b9d6f
5 changed files with 66 additions and 37 deletions

View File

@ -5,6 +5,7 @@ import ssl
import uuid
import signal
from datetime import datetime
from contextlib import asynccontextmanager
from snek import snode
from snek.view.threads import ThreadsView
@ -231,6 +232,7 @@ class Application(BaseApplication):
print(ex)
self.db.commit()
async def prepare_database(self, app):
self.db.query("PRAGMA journal_mode=WAL")
self.db.query("PRAGMA syncnorm=off")
@ -453,6 +455,27 @@ class Application(BaseApplication):
template_paths.append(self.template_path)
return FileSystemLoader(template_paths)
@asynccontextmanager
async def no_save(self):
stats = {
'count': 0
}
async def patched_save(*args, **kwargs):
await self.cache.set(args[0]["uid"], args[0])
stats['count'] = stats['count'] + 1
print(f"save is ignored {stats['count']} times")
return args[0]
save_original = app.services.channel_message.mapper.save
self.services.channel_message.mapper.save = patched_save
raised_exception = None
try:
yield
except Exception as ex:
raised_exception = ex
finally:
self.services.channel_message.mapper.save = save_original
if raised_exception:
raise raised_exception
app = Application(db_path="sqlite:///snek.db")

View File

@ -14,7 +14,7 @@ class Cache:
self.cache = {}
self.max_items = max_items
self.stats = {}
self.enabled = False
self.enabled = True
self.lru = []
self.version = ((42 + 420 + 1984 + 1990 + 10 + 6 + 71 + 3004 + 7245) ^ 1337) + 4

View File

@ -28,7 +28,13 @@ class BaseMapper:
use_semaphore = kwargs.pop("use_semaphore", False)
if use_semaphore:
async with self.semaphore:
database_exception = None
for x in range(20):
try:
return func(*args, **kwargs)
except Exception as ex:
database_exception = ex
raise database_exception
return await self.loop.run_in_executor(None, lambda: func(*args, **kwargs))
async def new(self):

View File

@ -40,10 +40,10 @@ class BaseService:
yield record
async def get(self, uid=None, **kwargs):
kwargs["deleted_at"] = None
if uid:
if not kwargs:
result = await self.cache.get(uid)
if False and result and result.__class__ == self.mapper.model_class:
if result and result.__class__ == self.mapper.model_class:
return result
kwargs["uid"] = uid
@ -52,7 +52,7 @@ class BaseService:
await self.cache.set(result["uid"], result)
return result
async def save(self, model: UserModel):
async def save(self, model):
# if model.is_valid: You Know why not
if await self.mapper.save(model):
await self.cache.set(model["uid"], model)

View File

@ -308,13 +308,13 @@ class RPCView(BaseView):
return True
async def update_message_text(self, message_uid, text):
async with self.app.no_save():
self._require_login()
message = await self.services.channel_message.get(message_uid)
if message["user_uid"] != self.user_uid:
raise Exception("Not allowed")
if message.get_seconds_since_last_update() > 3:
await self.finalize_message(message["uid"])
if message.get_seconds_since_last_update() > 5:
return {
"error": "Message too old",
"seconds_since_last_update": message.get_seconds_since_last_update(),