Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Convert ReadWriteLock to async/await.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Aug 28, 2020
1 parent aec7085 commit 3964069
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 32 deletions.
1 change: 1 addition & 0 deletions changelog.d/8202.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert various parts of the codebase to async/await.
49 changes: 26 additions & 23 deletions synapse/handlers/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
from typing import Any, Dict, Optional

from twisted.python.failure import Failure

from synapse.api.constants import EventTypes, Membership
from synapse.api.errors import SynapseError
from synapse.api.filtering import Filter
from synapse.logging.context import run_in_background
from synapse.metrics.background_process_metrics import run_as_background_process
from synapse.storage.state import StateFilter
from synapse.types import RoomStreamToken
from synapse.streams.config import PaginationConfig
from synapse.types import Requester, RoomStreamToken
from synapse.util.async_helpers import ReadWriteLock
from synapse.util.stringutils import random_string
from synapse.visibility import filter_events_for_client
Expand Down Expand Up @@ -247,15 +250,16 @@ def start_purge_history(self, room_id, token, delete_local_events=False):
)
return purge_id

async def _purge_history(self, purge_id, room_id, token, delete_local_events):
async def _purge_history(
self, purge_id: str, room_id: str, token: str, delete_local_events: bool
) -> None:
"""Carry out a history purge on a room.
Args:
purge_id (str): The id for this purge
room_id (str): The room to purge from
token (str): topological token to delete events before
delete_local_events (bool): True to delete local events as well as
remote ones
purge_id: The id for this purge
room_id: The room to purge from
token: topological token to delete events before
delete_local_events: True to delete local events as well as remote ones
"""
self._purges_in_progress_by_room.add(room_id)
try:
Expand Down Expand Up @@ -291,9 +295,9 @@ def get_purge_status(self, purge_id):
"""
return self._purges_by_id.get(purge_id)

async def purge_room(self, room_id):
async def purge_room(self, room_id: str) -> None:
"""Purge the given room from the database"""
with (await self.pagination_lock.write(room_id)):
with await self.pagination_lock.write(room_id):
# check we know about the room
await self.store.get_room_version_id(room_id)

Expand All @@ -307,23 +311,22 @@ async def purge_room(self, room_id):

async def get_messages(
self,
requester,
room_id=None,
pagin_config=None,
as_client_event=True,
event_filter=None,
):
requester: Requester,
room_id: Optional[str] = None,
pagin_config: Optional[PaginationConfig] = None,
as_client_event: bool = True,
event_filter: Optional[Filter] = None,
) -> Dict[str, Any]:
"""Get messages in a room.
Args:
requester (Requester): The user requesting messages.
room_id (str): The room they want messages from.
pagin_config (synapse.api.streams.PaginationConfig): The pagination
config rules to apply, if any.
as_client_event (bool): True to get events in client-server format.
event_filter (Filter): Filter to apply to results or None
requester: The user requesting messages.
room_id: The room they want messages from.
pagin_config: The pagination config rules to apply, if any.
as_client_event: True to get events in client-server format.
event_filter: Filter to apply to results or None
Returns:
dict: Pagination API results
Pagination API results
"""
user_id = requester.user.to_string()

Expand All @@ -343,7 +346,7 @@ async def get_messages(

source_config = pagin_config.get_source_config("room")

with (await self.pagination_lock.read(room_id)):
with await self.pagination_lock.read(room_id):
(
membership,
member_event_id,
Expand Down
16 changes: 7 additions & 9 deletions synapse/util/async_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import collections
import logging
from contextlib import contextmanager
from typing import Dict, Sequence, Set, Union
from typing import ContextManager, Dict, Sequence, Set, Union

import attr

Expand Down Expand Up @@ -338,11 +338,11 @@ def eb(e):


class ReadWriteLock(object):
"""A deferred style read write lock.
"""An async read write lock.
Example:
with (yield read_write_lock.read("test_key")):
with await read_write_lock.read("test_key"):
# do some work
"""

Expand All @@ -365,8 +365,7 @@ def __init__(self):
# Latest writer queued
self.key_to_current_writer = {} # type: Dict[str, defer.Deferred]

@defer.inlineCallbacks
def read(self, key):
async def read(self, key: str) -> ContextManager:
new_defer = defer.Deferred()

curr_readers = self.key_to_current_readers.setdefault(key, set())
Expand All @@ -376,7 +375,7 @@ def read(self, key):

# We wait for the latest writer to finish writing. We can safely ignore
# any existing readers... as they're readers.
yield make_deferred_yieldable(curr_writer)
await make_deferred_yieldable(curr_writer)

@contextmanager
def _ctx_manager():
Expand All @@ -388,8 +387,7 @@ def _ctx_manager():

return _ctx_manager()

@defer.inlineCallbacks
def write(self, key):
async def write(self, key: str) -> ContextManager:
new_defer = defer.Deferred()

curr_readers = self.key_to_current_readers.get(key, set())
Expand All @@ -405,7 +403,7 @@ def write(self, key):
curr_readers.clear()
self.key_to_current_writer[key] = new_defer

yield make_deferred_yieldable(defer.gatherResults(to_wait_on))
await make_deferred_yieldable(defer.gatherResults(to_wait_on))

@contextmanager
def _ctx_manager():
Expand Down

0 comments on commit 3964069

Please sign in to comment.