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 uuid
import signal import signal
from datetime import datetime from datetime import datetime
from contextlib import asynccontextmanager
from snek import snode from snek import snode
from snek.view.threads import ThreadsView from snek.view.threads import ThreadsView
@ -230,6 +231,7 @@ class Application(BaseApplication):
except Exception as ex: except Exception as ex:
print(ex) print(ex)
self.db.commit() self.db.commit()
async def prepare_database(self, app): async def prepare_database(self, app):
self.db.query("PRAGMA journal_mode=WAL") self.db.query("PRAGMA journal_mode=WAL")
@ -452,7 +454,28 @@ class Application(BaseApplication):
template_paths.append(self.template_path) template_paths.append(self.template_path)
return FileSystemLoader(template_paths) 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") app = Application(db_path="sqlite:///snek.db")

View File

@ -14,7 +14,7 @@ class Cache:
self.cache = {} self.cache = {}
self.max_items = max_items self.max_items = max_items
self.stats = {} self.stats = {}
self.enabled = False self.enabled = True
self.lru = [] self.lru = []
self.version = ((42 + 420 + 1984 + 1990 + 10 + 6 + 71 + 3004 + 7245) ^ 1337) + 4 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) use_semaphore = kwargs.pop("use_semaphore", False)
if use_semaphore: if use_semaphore:
async with self.semaphore: async with self.semaphore:
return func(*args, **kwargs) 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)) return await self.loop.run_in_executor(None, lambda: func(*args, **kwargs))
async def new(self): async def new(self):

View File

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

View File

@ -308,40 +308,40 @@ class RPCView(BaseView):
return True return True
async def update_message_text(self, message_uid, text): async def update_message_text(self, message_uid, text):
self._require_login() async with self.app.no_save():
message = await self.services.channel_message.get(message_uid) self._require_login()
if message["user_uid"] != self.user_uid: message = await self.services.channel_message.get(message_uid)
raise Exception("Not allowed") if message["user_uid"] != self.user_uid:
raise Exception("Not allowed")
if message.get_seconds_since_last_update() > 3: if message.get_seconds_since_last_update() > 5:
await self.finalize_message(message["uid"]) return {
return { "error": "Message too old",
"error": "Message too old", "seconds_since_last_update": message.get_seconds_since_last_update(),
"seconds_since_last_update": message.get_seconds_since_last_update(), "success": False,
"success": False, }
}
message["message"] = text message["message"] = text
if not text: if not text:
message["deleted_at"] = now() message["deleted_at"] = now()
else: else:
message["deleted_at"] = None message["deleted_at"] = None
await self.services.channel_message.save(message) await self.services.channel_message.save(message)
data = message.record data = message.record
data["text"] = message["message"] data["text"] = message["message"]
data["message_uid"] = message_uid data["message_uid"] = message_uid
await self.services.socket.broadcast( await self.services.socket.broadcast(
message["channel_uid"], message["channel_uid"],
{ {
"channel_uid": message["channel_uid"], "channel_uid": message["channel_uid"],
"event": "update_message_text", "event": "update_message_text",
"data": message.record, "data": message.record,
}, },
) )
return {"success": True} return {"success": True}
async def send_message(self, channel_uid, message, is_final=True): async def send_message(self, channel_uid, message, is_final=True):
self._require_login() self._require_login()