0
0
Fork 1
mirror of https://mau.dev/maunium/synapse.git synced 2024-12-13 09:43:33 +01:00

Removed request_key from the SyncConfig (moved outside as its own function parameter) (#17201)

Removed `request_key` from the `SyncConfig` (moved outside as its own function parameter) so it doesn't have to flow into `_generate_sync_entry_for_xxx` methods. This way we can separate the concerns of caching from generating the response and reuse the `_generate_sync_entry_for_xxx` functions as we see fit. Plus caching doesn't really have anything to do with the config of sync.

Split from https://github.com/element-hq/synapse/pull/17167

Spawning from https://github.com/element-hq/synapse/pull/17167#discussion_r1601497279
This commit is contained in:
Eric Eastwood 2024-05-16 11:54:46 -05:00 committed by GitHub
parent 7cb3f8a979
commit 28a948f04f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 59 additions and 14 deletions

1
changelog.d/17201.misc Normal file
View file

@ -0,0 +1 @@
Organize the sync cache key parameter outside of the sync config (separate concerns).

View file

@ -135,7 +135,6 @@ class SyncConfig:
user: UserID user: UserID
filter_collection: FilterCollection filter_collection: FilterCollection
is_guest: bool is_guest: bool
request_key: SyncRequestKey
device_id: Optional[str] device_id: Optional[str]
@ -328,6 +327,7 @@ class SyncHandler:
requester: Requester, requester: Requester,
sync_config: SyncConfig, sync_config: SyncConfig,
sync_version: SyncVersion, sync_version: SyncVersion,
request_key: SyncRequestKey,
since_token: Optional[StreamToken] = None, since_token: Optional[StreamToken] = None,
timeout: int = 0, timeout: int = 0,
full_state: bool = False, full_state: bool = False,
@ -340,10 +340,10 @@ class SyncHandler:
requester: The user requesting the sync response. requester: The user requesting the sync response.
sync_config: Config/info necessary to process the sync request. sync_config: Config/info necessary to process the sync request.
sync_version: Determines what kind of sync response to generate. sync_version: Determines what kind of sync response to generate.
request_key: The key to use for caching the response.
since_token: The point in the stream to sync from. since_token: The point in the stream to sync from.
timeout: How long to wait for new data to arrive before giving up. timeout: How long to wait for new data to arrive before giving up.
full_state: Whether to return the full state for each room. full_state: Whether to return the full state for each room.
Returns: Returns:
When `SyncVersion.SYNC_V2`, returns a full `SyncResult`. When `SyncVersion.SYNC_V2`, returns a full `SyncResult`.
""" """
@ -354,7 +354,7 @@ class SyncHandler:
await self.auth_blocking.check_auth_blocking(requester=requester) await self.auth_blocking.check_auth_blocking(requester=requester)
res = await self.response_cache.wrap( res = await self.response_cache.wrap(
sync_config.request_key, request_key,
self._wait_for_sync_for_user, self._wait_for_sync_for_user,
sync_config, sync_config,
sync_version, sync_version,

View file

@ -210,7 +210,6 @@ class SyncRestServlet(RestServlet):
user=user, user=user,
filter_collection=filter_collection, filter_collection=filter_collection,
is_guest=requester.is_guest, is_guest=requester.is_guest,
request_key=request_key,
device_id=device_id, device_id=device_id,
) )
@ -234,6 +233,7 @@ class SyncRestServlet(RestServlet):
requester, requester,
sync_config, sync_config,
SyncVersion.SYNC_V2, SyncVersion.SYNC_V2,
request_key,
since_token=since_token, since_token=since_token,
timeout=timeout, timeout=timeout,
full_state=full_state, full_state=full_state,

View file

@ -36,7 +36,7 @@ from synapse.server import HomeServer
from synapse.types import JsonDict, StreamToken, create_requester from synapse.types import JsonDict, StreamToken, create_requester
from synapse.util import Clock from synapse.util import Clock
from tests.handlers.test_sync import SyncVersion, generate_sync_config from tests.handlers.test_sync import SyncRequestKey, SyncVersion, generate_sync_config
from tests.unittest import ( from tests.unittest import (
FederatingHomeserverTestCase, FederatingHomeserverTestCase,
HomeserverTestCase, HomeserverTestCase,
@ -498,6 +498,15 @@ def send_presence_update(
return channel.json_body return channel.json_body
_request_key = 0
def generate_request_key() -> SyncRequestKey:
global _request_key
_request_key += 1
return ("request_key", _request_key)
def sync_presence( def sync_presence(
testcase: HomeserverTestCase, testcase: HomeserverTestCase,
user_id: str, user_id: str,
@ -521,7 +530,11 @@ def sync_presence(
sync_config = generate_sync_config(requester.user.to_string()) sync_config = generate_sync_config(requester.user.to_string())
sync_result = testcase.get_success( sync_result = testcase.get_success(
testcase.hs.get_sync_handler().wait_for_sync_for_user( testcase.hs.get_sync_handler().wait_for_sync_for_user(
requester, sync_config, SyncVersion.SYNC_V2, since_token requester,
sync_config,
SyncVersion.SYNC_V2,
generate_request_key(),
since_token,
) )
) )

View file

@ -31,7 +31,7 @@ from synapse.api.room_versions import RoomVersion, RoomVersions
from synapse.events import EventBase from synapse.events import EventBase
from synapse.events.snapshot import EventContext from synapse.events.snapshot import EventContext
from synapse.federation.federation_base import event_from_pdu_json from synapse.federation.federation_base import event_from_pdu_json
from synapse.handlers.sync import SyncConfig, SyncResult, SyncVersion from synapse.handlers.sync import SyncConfig, SyncRequestKey, SyncResult, SyncVersion
from synapse.rest import admin from synapse.rest import admin
from synapse.rest.client import knock, login, room from synapse.rest.client import knock, login, room
from synapse.server import HomeServer from synapse.server import HomeServer
@ -41,6 +41,14 @@ from synapse.util import Clock
import tests.unittest import tests.unittest
import tests.utils import tests.utils
_request_key = 0
def generate_request_key() -> SyncRequestKey:
global _request_key
_request_key += 1
return ("request_key", _request_key)
class SyncTestCase(tests.unittest.HomeserverTestCase): class SyncTestCase(tests.unittest.HomeserverTestCase):
"""Tests Sync Handler.""" """Tests Sync Handler."""
@ -77,6 +85,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config, sync_config,
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
@ -87,6 +96,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config, sync_config,
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
), ),
ResourceLimitError, ResourceLimitError,
) )
@ -102,6 +112,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config, sync_config,
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
), ),
ResourceLimitError, ResourceLimitError,
) )
@ -124,6 +135,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config=generate_sync_config(user, device_id="dev"), sync_config=generate_sync_config(user, device_id="dev"),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
@ -157,6 +169,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config=generate_sync_config(user), sync_config=generate_sync_config(user),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
self.assertIn(joined_room, [r.room_id for r in result.joined]) self.assertIn(joined_room, [r.room_id for r in result.joined])
@ -169,6 +182,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config=generate_sync_config(user, device_id="dev"), sync_config=generate_sync_config(user, device_id="dev"),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=initial_result.next_batch, since_token=initial_result.next_batch,
) )
) )
@ -200,6 +214,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config=generate_sync_config(user), sync_config=generate_sync_config(user),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
self.assertNotIn(joined_room, [r.room_id for r in result.joined]) self.assertNotIn(joined_room, [r.room_id for r in result.joined])
@ -212,6 +227,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
requester, requester,
sync_config=generate_sync_config(user, device_id="dev"), sync_config=generate_sync_config(user, device_id="dev"),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=initial_result.next_batch, since_token=initial_result.next_batch,
) )
) )
@ -254,6 +270,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
create_requester(owner), create_requester(owner),
generate_sync_config(owner), generate_sync_config(owner),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
self.assertEqual(len(alice_sync_result.joined), 1) self.assertEqual(len(alice_sync_result.joined), 1)
@ -277,6 +294,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
eve_requester, eve_requester,
eve_sync_config, eve_sync_config,
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
@ -295,6 +313,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
eve_requester, eve_requester,
eve_sync_config, eve_sync_config,
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=eve_sync_after_ban.next_batch, since_token=eve_sync_after_ban.next_batch,
) )
) )
@ -307,6 +326,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
eve_requester, eve_requester,
eve_sync_config, eve_sync_config,
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=None, since_token=None,
) )
) )
@ -341,6 +361,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
alice_requester, alice_requester,
generate_sync_config(alice), generate_sync_config(alice),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
last_room_creation_event_id = ( last_room_creation_event_id = (
@ -369,6 +390,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
), ),
), ),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=initial_sync_result.next_batch, since_token=initial_sync_result.next_batch,
) )
) )
@ -414,6 +436,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
alice_requester, alice_requester,
generate_sync_config(alice), generate_sync_config(alice),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
last_room_creation_event_id = ( last_room_creation_event_id = (
@ -452,6 +475,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
), ),
), ),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=initial_sync_result.next_batch, since_token=initial_sync_result.next_batch,
) )
) )
@ -498,6 +522,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
alice_requester, alice_requester,
generate_sync_config(alice), generate_sync_config(alice),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
last_room_creation_event_id = ( last_room_creation_event_id = (
@ -523,6 +548,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
), ),
), ),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=initial_sync_result.next_batch, since_token=initial_sync_result.next_batch,
) )
) )
@ -553,6 +579,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
), ),
), ),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=incremental_sync.next_batch, since_token=incremental_sync.next_batch,
) )
) )
@ -615,6 +642,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
alice_requester, alice_requester,
generate_sync_config(alice), generate_sync_config(alice),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
last_room_creation_event_id = ( last_room_creation_event_id = (
@ -639,6 +667,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
), ),
), ),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
room_sync = initial_sync_result.joined[0] room_sync = initial_sync_result.joined[0]
@ -660,6 +689,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
alice_requester, alice_requester,
generate_sync_config(alice), generate_sync_config(alice),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=initial_sync_result.next_batch, since_token=initial_sync_result.next_batch,
) )
) )
@ -713,6 +743,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
bob_requester, bob_requester,
generate_sync_config(bob), generate_sync_config(bob),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
@ -744,6 +775,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
bob, filter_collection=FilterCollection(self.hs, filter_dict) bob, filter_collection=FilterCollection(self.hs, filter_dict)
), ),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
since_token=None if initial_sync else initial_sync_result.next_batch, since_token=None if initial_sync else initial_sync_result.next_batch,
) )
).archived[0] ).archived[0]
@ -839,6 +871,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
create_requester(user), create_requester(user),
generate_sync_config(user), generate_sync_config(user),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
event_ids = [] event_ids = []
@ -887,6 +920,7 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
create_requester(user2), create_requester(user2),
generate_sync_config(user2), generate_sync_config(user2),
sync_version=SyncVersion.SYNC_V2, sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
priv_event_ids = [] priv_event_ids = []
@ -909,7 +943,10 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
sync_result: SyncResult = self.get_success( sync_result: SyncResult = self.get_success(
self.sync_handler.wait_for_sync_for_user( self.sync_handler.wait_for_sync_for_user(
create_requester(user), generate_sync_config(user) create_requester(user),
generate_sync_config(user),
sync_version=SyncVersion.SYNC_V2,
request_key=generate_request_key(),
) )
) )
@ -923,9 +960,6 @@ class SyncTestCase(tests.unittest.HomeserverTestCase):
self.fail("No push rules found") self.fail("No push rules found")
_request_key = 0
def generate_sync_config( def generate_sync_config(
user_id: str, user_id: str,
device_id: Optional[str] = "device_id", device_id: Optional[str] = "device_id",
@ -942,12 +976,9 @@ def generate_sync_config(
if filter_collection is None: if filter_collection is None:
filter_collection = Filtering(Mock()).DEFAULT_FILTER_COLLECTION filter_collection = Filtering(Mock()).DEFAULT_FILTER_COLLECTION
global _request_key
_request_key += 1
return SyncConfig( return SyncConfig(
user=UserID.from_string(user_id), user=UserID.from_string(user_id),
filter_collection=filter_collection, filter_collection=filter_collection,
is_guest=False, is_guest=False,
request_key=("request_key", _request_key),
device_id=device_id, device_id=device_id,
) )