File tree 2 files changed +25
-16
lines changed
pycrdt_websocket/django_channels/storage
2 files changed +25
-16
lines changed Original file line number Diff line number Diff line change 1
- import time
2
1
from abc import ABC , abstractmethod
3
2
from typing import Optional
4
3
@@ -55,10 +54,8 @@ async def save_snapshot(self):
55
54
```
56
55
"""
57
56
58
- def __init__ (self , room_name : str , save_throttle_interval : int | None ) -> None :
57
+ def __init__ (self , room_name : str ) -> None :
59
58
self .room_name = room_name
60
- self .save_throttle_interval = save_throttle_interval
61
- self .last_saved_at = time .time ()
62
59
63
60
@abstractmethod
64
61
async def get_document (self ) -> Doc :
@@ -95,15 +92,10 @@ async def save_snapshot(self) -> None:
95
92
"""Saves the document encoded as update to the database."""
96
93
...
97
94
98
- async def throttled_save_snapshot (self ) -> None :
99
- """Saves the document encoded as update to the database, throttled."""
95
+ async def close (self ) -> None :
96
+ """Closes the storage connection.
100
97
101
- if (
102
- not self .save_throttle_interval
103
- or time .time () - self .last_saved_at <= self .save_throttle_interval
104
- ):
105
- return
106
-
107
- await self .save_snapshot ()
108
-
109
- self .last_saved_at = time .time ()
98
+ Useful for cleaning up resources like closing a database
99
+ connection or saving the document before exiting.
100
+ """
101
+ pass
Original file line number Diff line number Diff line change
1
+ import time
1
2
from typing import Optional
2
3
3
4
import redis .asyncio as redis
@@ -14,7 +15,10 @@ class RedisYRoomStorage(BaseYRoomStorage):
14
15
"""
15
16
16
17
def __init__ (self , room_name : str , save_throttle_interval : int | None = None ) -> None :
17
- super ().__init__ (room_name , save_throttle_interval )
18
+ super ().__init__ (room_name )
19
+
20
+ self .save_throttle_interval = save_throttle_interval
21
+ self .last_saved_at = time .time ()
18
22
19
23
self .redis_key = f"document:{ self .room_name } "
20
24
self .redis = self ._make_redis ()
@@ -67,6 +71,19 @@ async def load_snapshot(self) -> Optional[bytes]:
67
71
async def save_snapshot (self ) -> Optional [bytes ]:
68
72
return None
69
73
74
+ async def throttled_save_snapshot (self ) -> None :
75
+ """Saves the document encoded as update to the database, throttled."""
76
+
77
+ if (
78
+ not self .save_throttle_interval
79
+ or time .time () - self .last_saved_at <= self .save_throttle_interval
80
+ ):
81
+ return
82
+
83
+ await self .save_snapshot ()
84
+
85
+ self .last_saved_at = time .time ()
86
+
70
87
async def close (self ):
71
88
await self .save_snapshot ()
72
89
await self .redis .close ()
You can’t perform that action at this time.
0 commit comments