122122
123123
124124class BasePresenceHandler (abc .ABC ):
125- """Parts of the PresenceHandler that are shared between workers and master"""
125+ """Parts of the PresenceHandler that are shared between workers and presence
126+ writer"""
126127
127128 def __init__ (self , hs : "HomeServer" ):
128129 self .clock = hs .get_clock ()
@@ -309,17 +310,25 @@ def __init__(self, hs):
309310 super ().__init__ (hs )
310311 self .hs = hs
311312
313+ self ._presence_writer_instance = hs .config .worker .writers .presence [0 ]
314+
312315 self ._presence_enabled = hs .config .use_presence
313316
317+ # Route presence EDUs to the right worker
318+ hs .get_federation_registry ().register_instances_for_edu (
319+ "m.presence" ,
320+ hs .config .worker .writers .presence ,
321+ )
322+
314323 # The number of ongoing syncs on this process, by user id.
315324 # Empty if _presence_enabled is false.
316325 self ._user_to_num_current_syncs = {} # type: Dict[str, int]
317326
318327 self .notifier = hs .get_notifier ()
319328 self .instance_id = hs .get_instance_id ()
320329
321- # user_id -> last_sync_ms. Lists the users that have stopped syncing
322- # but we haven't notified the master of that yet
330+ # user_id -> last_sync_ms. Lists the users that have stopped syncing but
331+ # we haven't notified the presence writer of that yet
323332 self .users_going_offline = {}
324333
325334 self ._bump_active_client = ReplicationBumpPresenceActiveTime .make_client (hs )
@@ -352,31 +361,32 @@ def send_user_sync(self, user_id, is_syncing, last_sync_ms):
352361 )
353362
354363 def mark_as_coming_online (self , user_id ):
355- """A user has started syncing. Send a UserSync to the master, unless they
356- had recently stopped syncing.
364+ """A user has started syncing. Send a UserSync to the presence writer,
365+ unless they had recently stopped syncing.
357366
358367 Args:
359368 user_id (str)
360369 """
361370 going_offline = self .users_going_offline .pop (user_id , None )
362371 if not going_offline :
363- # Safe to skip because we haven't yet told the master they were offline
372+ # Safe to skip because we haven't yet told the presence writer they
373+ # were offline
364374 self .send_user_sync (user_id , True , self .clock .time_msec ())
365375
366376 def mark_as_going_offline (self , user_id ):
367- """A user has stopped syncing. We wait before notifying the master as
368- its likely they'll come back soon. This allows us to avoid sending
369- a stopped syncing immediately followed by a started syncing notification
370- to the master
377+ """A user has stopped syncing. We wait before notifying the presence
378+ writer as its likely they'll come back soon. This allows us to avoid
379+ sending a stopped syncing immediately followed by a started syncing
380+ notification to the presence writer
371381
372382 Args:
373383 user_id (str)
374384 """
375385 self .users_going_offline [user_id ] = self .clock .time_msec ()
376386
377387 def send_stop_syncing (self ):
378- """Check if there are any users who have stopped syncing a while ago
379- and haven't come back yet. If there are poke the master about them.
388+ """Check if there are any users who have stopped syncing a while ago and
389+ haven't come back yet. If there are poke the presence writer about them.
380390 """
381391 now = self .clock .time_msec ()
382392 for user_id , last_sync_ms in list (self .users_going_offline .items ()):
@@ -492,9 +502,12 @@ async def set_state(self, target_user, state, ignore_status_msg=False):
492502 if not self .hs .config .use_presence :
493503 return
494504
495- # Proxy request to master
505+ # Proxy request to instance that writes presence
496506 await self ._set_state_client (
497- user_id = user_id , state = state , ignore_status_msg = ignore_status_msg
507+ instance_name = self ._presence_writer_instance ,
508+ user_id = user_id ,
509+ state = state ,
510+ ignore_status_msg = ignore_status_msg ,
498511 )
499512
500513 async def bump_presence_active_time (self , user ):
@@ -505,9 +518,11 @@ async def bump_presence_active_time(self, user):
505518 if not self .hs .config .use_presence :
506519 return
507520
508- # Proxy request to master
521+ # Proxy request to instance that writes presence
509522 user_id = user .to_string ()
510- await self ._bump_active_client (user_id = user_id )
523+ await self ._bump_active_client (
524+ instance_name = self ._presence_writer_instance , user_id = user_id
525+ )
511526
512527
513528class PresenceHandler (BasePresenceHandler ):
@@ -1909,7 +1924,7 @@ def __init__(self, hs: "HomeServer", presence_handler: BasePresenceHandler):
19091924 self ._queue_presence_updates = True
19101925
19111926 # Whether this instance is a presence writer.
1912- self ._presence_writer = hs .config .worker .worker_app is None
1927+ self ._presence_writer = self . _instance_name in hs .config .worker .writers . presence
19131928
19141929 # The FederationSender instance, if this process sends federation traffic directly.
19151930 self ._federation = None
@@ -1957,7 +1972,7 @@ def send_presence_to_destinations(
19571972 Will forward to the local federation sender (if there is one) and queue
19581973 to send over replication (if there are other federation sender instances.).
19591974
1960- Must only be called on the master process.
1975+ Must only be called on the presence writer process.
19611976 """
19621977
19631978 # This should only be called on a presence writer.
@@ -2003,10 +2018,11 @@ async def get_replication_rows(
20032018 We return rows in the form of `(destination, user_id)` to keep the size
20042019 of each row bounded (rather than returning the sets in a row).
20052020
2006- On workers this will query the master process via HTTP replication.
2021+ On workers this will query the presence writer process via HTTP replication.
20072022 """
20082023 if instance_name != self ._instance_name :
2009- # If not local we query over http replication from the master
2024+ # If not local we query over http replication from the presence
2025+ # writer
20102026 result = await self ._repl_client (
20112027 instance_name = instance_name ,
20122028 stream_name = PresenceFederationStream .NAME ,
0 commit comments