Skip to content

Commit

Permalink
refactor(django-channels@storage): move throttling methods to redis s…
Browse files Browse the repository at this point in the history
…torage and close method to base

style: fix linter wrong return type of save_snapshot
  • Loading branch information
cacosandon committed Apr 6, 2024
1 parent 6c03d23 commit f7c6f26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
22 changes: 7 additions & 15 deletions pycrdt_websocket/django_channels/storage/base_yroom_storage.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import time
from abc import ABC, abstractmethod
from typing import Optional

Expand Down Expand Up @@ -55,10 +54,8 @@ async def save_snapshot(self):
```
"""

def __init__(self, room_name: str, save_throttle_interval: int | None) -> None:
def __init__(self, room_name: str) -> None:
self.room_name = room_name
self.save_throttle_interval = save_throttle_interval
self.last_saved_at = time.time()

@abstractmethod
async def get_document(self) -> Doc:
Expand Down Expand Up @@ -95,15 +92,10 @@ async def save_snapshot(self) -> None:
"""Saves the document encoded as update to the database."""
...

async def throttled_save_snapshot(self) -> None:
"""Saves the document encoded as update to the database, throttled."""
async def close(self) -> None:
"""Closes the storage connection.
if (
not self.save_throttle_interval
or time.time() - self.last_saved_at <= self.save_throttle_interval
):
return

await self.save_snapshot()

self.last_saved_at = time.time()
Useful for cleaning up resources like closing a database
connection or saving the document before exiting.
"""
pass
21 changes: 19 additions & 2 deletions pycrdt_websocket/django_channels/storage/redis_yroom_storage.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import time
from typing import Optional

import redis.asyncio as redis
Expand All @@ -14,7 +15,10 @@ class RedisYRoomStorage(BaseYRoomStorage):
"""

def __init__(self, room_name: str, save_throttle_interval: int | None = None) -> None:
super().__init__(room_name, save_throttle_interval)
super().__init__(room_name)

self.save_throttle_interval = save_throttle_interval
self.last_saved_at = time.time()

self.redis_key = f"document:{self.room_name}"
self.redis = self._make_redis()
Expand Down Expand Up @@ -64,9 +68,22 @@ async def update_document(self, update: bytes):
async def load_snapshot(self) -> Optional[bytes]:
return None

async def save_snapshot(self) -> Optional[bytes]:
async def save_snapshot(self) -> None:
return None

async def throttled_save_snapshot(self) -> None:
"""Saves the document encoded as update to the database, throttled."""

if (
not self.save_throttle_interval
or time.time() - self.last_saved_at <= self.save_throttle_interval
):
return

await self.save_snapshot()

self.last_saved_at = time.time()

async def close(self):
await self.save_snapshot()
await self.redis.close()
Expand Down

0 comments on commit f7c6f26

Please sign in to comment.