14
14
"""A replication client for use by synapse workers.
15
15
"""
16
16
import logging
17
- from typing import TYPE_CHECKING , Dict , List , Optional , Set , Tuple
17
+ from typing import TYPE_CHECKING , Dict , Iterable , List , Optional , Set , Tuple
18
18
19
19
from twisted .internet .defer import Deferred
20
+ from twisted .internet .interfaces import IAddress , IConnector
20
21
from twisted .internet .protocol import ReconnectingClientFactory
22
+ from twisted .python .failure import Failure
21
23
22
24
from synapse .api .constants import EventTypes
23
25
from synapse .federation import send_queue
@@ -79,10 +81,10 @@ def __init__(
79
81
80
82
hs .get_reactor ().addSystemEventTrigger ("before" , "shutdown" , self .stopTrying )
81
83
82
- def startedConnecting (self , connector ) :
84
+ def startedConnecting (self , connector : IConnector ) -> None :
83
85
logger .info ("Connecting to replication: %r" , connector .getDestination ())
84
86
85
- def buildProtocol (self , addr ) :
87
+ def buildProtocol (self , addr : IAddress ) -> ClientReplicationStreamProtocol :
86
88
logger .info ("Connected to replication: %r" , addr )
87
89
return ClientReplicationStreamProtocol (
88
90
self .hs ,
@@ -92,11 +94,11 @@ def buildProtocol(self, addr):
92
94
self .command_handler ,
93
95
)
94
96
95
- def clientConnectionLost (self , connector , reason ) :
97
+ def clientConnectionLost (self , connector : IConnector , reason : Failure ) -> None :
96
98
logger .error ("Lost replication conn: %r" , reason )
97
99
ReconnectingClientFactory .clientConnectionLost (self , connector , reason )
98
100
99
- def clientConnectionFailed (self , connector , reason ) :
101
+ def clientConnectionFailed (self , connector : IConnector , reason : Failure ) -> None :
100
102
logger .error ("Failed to connect to replication: %r" , reason )
101
103
ReconnectingClientFactory .clientConnectionFailed (self , connector , reason )
102
104
@@ -131,7 +133,7 @@ def __init__(self, hs: "HomeServer"):
131
133
132
134
async def on_rdata (
133
135
self , stream_name : str , instance_name : str , token : int , rows : list
134
- ):
136
+ ) -> None :
135
137
"""Called to handle a batch of replication data with a given stream token.
136
138
137
139
By default this just pokes the slave store. Can be overridden in subclasses to
@@ -252,14 +254,16 @@ async def on_rdata(
252
254
# loop. (This maintains the order so no need to resort)
253
255
waiting_list [:] = waiting_list [index_of_first_deferred_not_called :]
254
256
255
- async def on_position (self , stream_name : str , instance_name : str , token : int ):
257
+ async def on_position (
258
+ self , stream_name : str , instance_name : str , token : int
259
+ ) -> None :
256
260
await self .on_rdata (stream_name , instance_name , token , [])
257
261
258
262
# We poke the generic "replication" notifier to wake anything up that
259
263
# may be streaming.
260
264
self .notifier .notify_replication ()
261
265
262
- def on_remote_server_up (self , server : str ):
266
+ def on_remote_server_up (self , server : str ) -> None :
263
267
"""Called when get a new REMOTE_SERVER_UP command."""
264
268
265
269
# Let's wake up the transaction queue for the server in case we have
@@ -269,7 +273,7 @@ def on_remote_server_up(self, server: str):
269
273
270
274
async def wait_for_stream_position (
271
275
self , instance_name : str , stream_name : str , position : int
272
- ):
276
+ ) -> None :
273
277
"""Wait until this instance has received updates up to and including
274
278
the given stream position.
275
279
"""
@@ -304,7 +308,7 @@ async def wait_for_stream_position(
304
308
"Finished waiting for repl stream %r to reach %s" , stream_name , position
305
309
)
306
310
307
- def stop_pusher (self , user_id , app_id , pushkey ) :
311
+ def stop_pusher (self , user_id : str , app_id : str , pushkey : str ) -> None :
308
312
if not self ._notify_pushers :
309
313
return
310
314
@@ -316,13 +320,13 @@ def stop_pusher(self, user_id, app_id, pushkey):
316
320
logger .info ("Stopping pusher %r / %r" , user_id , key )
317
321
pusher .on_stop ()
318
322
319
- async def start_pusher (self , user_id , app_id , pushkey ) :
323
+ async def start_pusher (self , user_id : str , app_id : str , pushkey : str ) -> None :
320
324
if not self ._notify_pushers :
321
325
return
322
326
323
327
key = "%s:%s" % (app_id , pushkey )
324
328
logger .info ("Starting pusher %r / %r" , user_id , key )
325
- return await self ._pusher_pool .start_pusher_by_id (app_id , pushkey , user_id )
329
+ await self ._pusher_pool .start_pusher_by_id (app_id , pushkey , user_id )
326
330
327
331
328
332
class FederationSenderHandler :
@@ -353,10 +357,12 @@ def __init__(self, hs: "HomeServer"):
353
357
354
358
self ._fed_position_linearizer = Linearizer (name = "_fed_position_linearizer" )
355
359
356
- def wake_destination (self , server : str ):
360
+ def wake_destination (self , server : str ) -> None :
357
361
self .federation_sender .wake_destination (server )
358
362
359
- async def process_replication_rows (self , stream_name , token , rows ):
363
+ async def process_replication_rows (
364
+ self , stream_name : str , token : int , rows : list
365
+ ) -> None :
360
366
# The federation stream contains things that we want to send out, e.g.
361
367
# presence, typing, etc.
362
368
if stream_name == "federation" :
@@ -384,11 +390,12 @@ async def process_replication_rows(self, stream_name, token, rows):
384
390
for host in hosts :
385
391
self .federation_sender .send_device_messages (host )
386
392
387
- async def _on_new_receipts (self , rows ):
393
+ async def _on_new_receipts (
394
+ self , rows : Iterable [ReceiptsStream .ReceiptsStreamRow ]
395
+ ) -> None :
388
396
"""
389
397
Args:
390
- rows (Iterable[synapse.replication.tcp.streams.ReceiptsStream.ReceiptsStreamRow]):
391
- new receipts to be processed
398
+ rows: new receipts to be processed
392
399
"""
393
400
for receipt in rows :
394
401
# we only want to send on receipts for our own users
@@ -408,7 +415,7 @@ async def _on_new_receipts(self, rows):
408
415
)
409
416
await self .federation_sender .send_read_receipt (receipt_info )
410
417
411
- async def update_token (self , token ) :
418
+ async def update_token (self , token : int ) -> None :
412
419
"""Update the record of where we have processed to in the federation stream.
413
420
414
421
Called after we have processed a an update received over replication. Sends
@@ -428,7 +435,7 @@ async def update_token(self, token):
428
435
429
436
run_as_background_process ("_save_and_send_ack" , self ._save_and_send_ack )
430
437
431
- async def _save_and_send_ack (self ):
438
+ async def _save_and_send_ack (self ) -> None :
432
439
"""Save the current federation position in the database and send an ACK
433
440
to master with where we're up to.
434
441
"""
0 commit comments