@@ -1390,7 +1390,7 @@ async def handle_new_client_event(
13901390 extra_users = extra_users ,
13911391 ),
13921392 run_in_background (
1393- self .cache_joined_hosts_for_event , event , context
1393+ self .cache_joined_hosts_for_events , events_and_context
13941394 ).addErrback (
13951395 log_failure , "cache_joined_hosts_for_event failed"
13961396 ),
@@ -1491,62 +1491,65 @@ async def _persist_events(
14911491 await self .store .remove_push_actions_from_staging (event .event_id )
14921492 raise
14931493
1494- async def cache_joined_hosts_for_event (
1495- self , event : EventBase , context : EventContext
1494+ async def cache_joined_hosts_for_events (
1495+ self , events_and_context : List [ Tuple [ EventBase , EventContext ]]
14961496 ) -> None :
1497- """Precalculate the joined hosts at the event , when using Redis, so that
1497+ """Precalculate the joined hosts at each of the given events , when using Redis, so that
14981498 external federation senders don't have to recalculate it themselves.
14991499 """
15001500
1501- if not self ._external_cache .is_enabled ():
1502- return
1503-
1504- # If external cache is enabled we should always have this.
1505- assert self ._external_cache_joined_hosts_updates is not None
1501+ for event , _ in events_and_context :
1502+ if not self ._external_cache .is_enabled ():
1503+ return
15061504
1507- # We actually store two mappings, event ID -> prev state group,
1508- # state group -> joined hosts, which is much more space efficient
1509- # than event ID -> joined hosts.
1510- #
1511- # Note: We have to cache event ID -> prev state group, as we don't
1512- # store that in the DB.
1513- #
1514- # Note: We set the state group -> joined hosts cache if it hasn't been
1515- # set for a while, so that the expiry time is reset.
1505+ # If external cache is enabled we should always have this.
1506+ assert self ._external_cache_joined_hosts_updates is not None
15161507
1517- state_entry = await self .state .resolve_state_groups_for_events (
1518- event .room_id , event_ids = event .prev_event_ids ()
1519- )
1508+ # We actually store two mappings, event ID -> prev state group,
1509+ # state group -> joined hosts, which is much more space efficient
1510+ # than event ID -> joined hosts.
1511+ #
1512+ # Note: We have to cache event ID -> prev state group, as we don't
1513+ # store that in the DB.
1514+ #
1515+ # Note: We set the state group -> joined hosts cache if it hasn't been
1516+ # set for a while, so that the expiry time is reset.
15201517
1521- if state_entry .state_group :
1522- await self ._external_cache .set (
1523- "event_to_prev_state_group" ,
1524- event .event_id ,
1525- state_entry .state_group ,
1526- expiry_ms = 60 * 60 * 1000 ,
1518+ state_entry = await self .state .resolve_state_groups_for_events (
1519+ event .room_id , event_ids = event .prev_event_ids ()
15271520 )
15281521
1529- if state_entry .state_group in self ._external_cache_joined_hosts_updates :
1530- return
1522+ if state_entry .state_group :
1523+ await self ._external_cache .set (
1524+ "event_to_prev_state_group" ,
1525+ event .event_id ,
1526+ state_entry .state_group ,
1527+ expiry_ms = 60 * 60 * 1000 ,
1528+ )
15311529
1532- state = await state_entry .get_state (
1533- self ._storage_controllers .state , StateFilter .all ()
1534- )
1535- with opentracing .start_active_span ("get_joined_hosts" ):
1536- joined_hosts = await self .store .get_joined_hosts (
1537- event .room_id , state , state_entry
1530+ if state_entry .state_group in self ._external_cache_joined_hosts_updates :
1531+ return
1532+
1533+ state = await state_entry .get_state (
1534+ self ._storage_controllers .state , StateFilter .all ()
15381535 )
1536+ with opentracing .start_active_span ("get_joined_hosts" ):
1537+ joined_hosts = await self .store .get_joined_hosts (
1538+ event .room_id , state , state_entry
1539+ )
15391540
1540- # Note that the expiry times must be larger than the expiry time in
1541- # _external_cache_joined_hosts_updates.
1542- await self ._external_cache .set (
1543- "get_joined_hosts" ,
1544- str (state_entry .state_group ),
1545- list (joined_hosts ),
1546- expiry_ms = 60 * 60 * 1000 ,
1547- )
1541+ # Note that the expiry times must be larger than the expiry time in
1542+ # _external_cache_joined_hosts_updates.
1543+ await self ._external_cache .set (
1544+ "get_joined_hosts" ,
1545+ str (state_entry .state_group ),
1546+ list (joined_hosts ),
1547+ expiry_ms = 60 * 60 * 1000 ,
1548+ )
15481549
1549- self ._external_cache_joined_hosts_updates [state_entry .state_group ] = None
1550+ self ._external_cache_joined_hosts_updates [
1551+ state_entry .state_group
1552+ ] = None
15501553
15511554 async def _validate_canonical_alias (
15521555 self ,
0 commit comments