1414# limitations under the License.
1515
1616import logging
17-
18- from twisted .internet import defer
17+ from typing import Dict , Optional , Tuple
1918
2019from synapse .metrics .background_process_metrics import wrap_as_background_process
2120from synapse .storage ._base import SQLBaseStore
@@ -82,21 +81,19 @@ def __init__(self, database: DatabasePool, db_conn, hs):
8281 "devices_last_seen" , self ._devices_last_seen_update
8382 )
8483
85- @defer .inlineCallbacks
86- def _remove_user_ip_nonunique (self , progress , batch_size ):
84+ async def _remove_user_ip_nonunique (self , progress , batch_size ):
8785 def f (conn ):
8886 txn = conn .cursor ()
8987 txn .execute ("DROP INDEX IF EXISTS user_ips_user_ip" )
9088 txn .close ()
9189
92- yield self .db_pool .runWithConnection (f )
93- yield self .db_pool .updates ._end_background_update (
90+ await self .db_pool .runWithConnection (f )
91+ await self .db_pool .updates ._end_background_update (
9492 "user_ips_drop_nonunique_index"
9593 )
9694 return 1
9795
98- @defer .inlineCallbacks
99- def _analyze_user_ip (self , progress , batch_size ):
96+ async def _analyze_user_ip (self , progress , batch_size ):
10097 # Background update to analyze user_ips table before we run the
10198 # deduplication background update. The table may not have been analyzed
10299 # for ages due to the table locks.
@@ -106,14 +103,13 @@ def _analyze_user_ip(self, progress, batch_size):
106103 def user_ips_analyze (txn ):
107104 txn .execute ("ANALYZE user_ips" )
108105
109- yield self .db_pool .runInteraction ("user_ips_analyze" , user_ips_analyze )
106+ await self .db_pool .runInteraction ("user_ips_analyze" , user_ips_analyze )
110107
111- yield self .db_pool .updates ._end_background_update ("user_ips_analyze" )
108+ await self .db_pool .updates ._end_background_update ("user_ips_analyze" )
112109
113110 return 1
114111
115- @defer .inlineCallbacks
116- def _remove_user_ip_dupes (self , progress , batch_size ):
112+ async def _remove_user_ip_dupes (self , progress , batch_size ):
117113 # This works function works by scanning the user_ips table in batches
118114 # based on `last_seen`. For each row in a batch it searches the rest of
119115 # the table to see if there are any duplicates, if there are then they
@@ -140,7 +136,7 @@ def get_last_seen(txn):
140136 return None
141137
142138 # Get a last seen that has roughly `batch_size` since `begin_last_seen`
143- end_last_seen = yield self .db_pool .runInteraction (
139+ end_last_seen = await self .db_pool .runInteraction (
144140 "user_ips_dups_get_last_seen" , get_last_seen
145141 )
146142
@@ -275,15 +271,14 @@ def remove(txn):
275271 txn , "user_ips_remove_dupes" , {"last_seen" : end_last_seen }
276272 )
277273
278- yield self .db_pool .runInteraction ("user_ips_dups_remove" , remove )
274+ await self .db_pool .runInteraction ("user_ips_dups_remove" , remove )
279275
280276 if last :
281- yield self .db_pool .updates ._end_background_update ("user_ips_remove_dupes" )
277+ await self .db_pool .updates ._end_background_update ("user_ips_remove_dupes" )
282278
283279 return batch_size
284280
285- @defer .inlineCallbacks
286- def _devices_last_seen_update (self , progress , batch_size ):
281+ async def _devices_last_seen_update (self , progress , batch_size ):
287282 """Background update to insert last seen info into devices table
288283 """
289284
@@ -346,12 +341,12 @@ def _devices_last_seen_update_txn(txn):
346341
347342 return len (rows )
348343
349- updated = yield self .db_pool .runInteraction (
344+ updated = await self .db_pool .runInteraction (
350345 "_devices_last_seen_update" , _devices_last_seen_update_txn
351346 )
352347
353348 if not updated :
354- yield self .db_pool .updates ._end_background_update ("devices_last_seen" )
349+ await self .db_pool .updates ._end_background_update ("devices_last_seen" )
355350
356351 return updated
357352
@@ -460,25 +455,25 @@ def _update_client_ips_batch_txn(self, txn, to_update):
460455 # Failed to upsert, log and continue
461456 logger .error ("Failed to insert client IP %r: %r" , entry , e )
462457
463- @defer .inlineCallbacks
464- def get_last_client_ip_by_device (self , user_id , device_id ):
458+ async def get_last_client_ip_by_device (
459+ self , user_id : str , device_id : Optional [str ]
460+ ) -> Dict [Tuple [str , str ], dict ]:
465461 """For each device_id listed, give the user_ip it was last seen on
466462
467463 Args:
468- user_id (str)
469- device_id (str) : If None fetches all devices for the user
464+ user_id: The user to fetch devices for.
465+ device_id: If None fetches all devices for the user
470466
471467 Returns:
472- defer.Deferred: resolves to a dict, where the keys
473- are (user_id, device_id) tuples. The values are also dicts, with
474- keys giving the column names
468+ A dictionary mapping a tuple of (user_id, device_id) to dicts, with
469+ keys giving the column names from the devices table.
475470 """
476471
477472 keyvalues = {"user_id" : user_id }
478473 if device_id is not None :
479474 keyvalues ["device_id" ] = device_id
480475
481- res = yield self .db_pool .simple_select_list (
476+ res = await self .db_pool .simple_select_list (
482477 table = "devices" ,
483478 keyvalues = keyvalues ,
484479 retcols = ("user_id" , "ip" , "user_agent" , "device_id" , "last_seen" ),
@@ -500,8 +495,7 @@ def get_last_client_ip_by_device(self, user_id, device_id):
500495 }
501496 return ret
502497
503- @defer .inlineCallbacks
504- def get_user_ip_and_agents (self , user ):
498+ async def get_user_ip_and_agents (self , user ):
505499 user_id = user .to_string ()
506500 results = {}
507501
@@ -511,7 +505,7 @@ def get_user_ip_and_agents(self, user):
511505 user_agent , _ , last_seen = self ._batch_row_update [key ]
512506 results [(access_token , ip )] = (user_agent , last_seen )
513507
514- rows = yield self .db_pool .simple_select_list (
508+ rows = await self .db_pool .simple_select_list (
515509 table = "user_ips" ,
516510 keyvalues = {"user_id" : user_id },
517511 retcols = ["access_token" , "ip" , "user_agent" , "last_seen" ],
0 commit comments