14
14
# limitations under the License.
15
15
16
16
import logging
17
-
18
- from twisted .internet import defer
17
+ from typing import Dict , Optional , Tuple
19
18
20
19
from synapse .metrics .background_process_metrics import wrap_as_background_process
21
20
from synapse .storage ._base import SQLBaseStore
@@ -82,21 +81,19 @@ def __init__(self, database: DatabasePool, db_conn, hs):
82
81
"devices_last_seen" , self ._devices_last_seen_update
83
82
)
84
83
85
- @defer .inlineCallbacks
86
- def _remove_user_ip_nonunique (self , progress , batch_size ):
84
+ async def _remove_user_ip_nonunique (self , progress , batch_size ):
87
85
def f (conn ):
88
86
txn = conn .cursor ()
89
87
txn .execute ("DROP INDEX IF EXISTS user_ips_user_ip" )
90
88
txn .close ()
91
89
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 (
94
92
"user_ips_drop_nonunique_index"
95
93
)
96
94
return 1
97
95
98
- @defer .inlineCallbacks
99
- def _analyze_user_ip (self , progress , batch_size ):
96
+ async def _analyze_user_ip (self , progress , batch_size ):
100
97
# Background update to analyze user_ips table before we run the
101
98
# deduplication background update. The table may not have been analyzed
102
99
# for ages due to the table locks.
@@ -106,14 +103,13 @@ def _analyze_user_ip(self, progress, batch_size):
106
103
def user_ips_analyze (txn ):
107
104
txn .execute ("ANALYZE user_ips" )
108
105
109
- yield self .db_pool .runInteraction ("user_ips_analyze" , user_ips_analyze )
106
+ await self .db_pool .runInteraction ("user_ips_analyze" , user_ips_analyze )
110
107
111
- yield self .db_pool .updates ._end_background_update ("user_ips_analyze" )
108
+ await self .db_pool .updates ._end_background_update ("user_ips_analyze" )
112
109
113
110
return 1
114
111
115
- @defer .inlineCallbacks
116
- def _remove_user_ip_dupes (self , progress , batch_size ):
112
+ async def _remove_user_ip_dupes (self , progress , batch_size ):
117
113
# This works function works by scanning the user_ips table in batches
118
114
# based on `last_seen`. For each row in a batch it searches the rest of
119
115
# the table to see if there are any duplicates, if there are then they
@@ -140,7 +136,7 @@ def get_last_seen(txn):
140
136
return None
141
137
142
138
# 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 (
144
140
"user_ips_dups_get_last_seen" , get_last_seen
145
141
)
146
142
@@ -275,15 +271,14 @@ def remove(txn):
275
271
txn , "user_ips_remove_dupes" , {"last_seen" : end_last_seen }
276
272
)
277
273
278
- yield self .db_pool .runInteraction ("user_ips_dups_remove" , remove )
274
+ await self .db_pool .runInteraction ("user_ips_dups_remove" , remove )
279
275
280
276
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" )
282
278
283
279
return batch_size
284
280
285
- @defer .inlineCallbacks
286
- def _devices_last_seen_update (self , progress , batch_size ):
281
+ async def _devices_last_seen_update (self , progress , batch_size ):
287
282
"""Background update to insert last seen info into devices table
288
283
"""
289
284
@@ -346,12 +341,12 @@ def _devices_last_seen_update_txn(txn):
346
341
347
342
return len (rows )
348
343
349
- updated = yield self .db_pool .runInteraction (
344
+ updated = await self .db_pool .runInteraction (
350
345
"_devices_last_seen_update" , _devices_last_seen_update_txn
351
346
)
352
347
353
348
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" )
355
350
356
351
return updated
357
352
@@ -460,25 +455,25 @@ def _update_client_ips_batch_txn(self, txn, to_update):
460
455
# Failed to upsert, log and continue
461
456
logger .error ("Failed to insert client IP %r: %r" , entry , e )
462
457
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 ]:
465
461
"""For each device_id listed, give the user_ip it was last seen on
466
462
467
463
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
470
466
471
467
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.
475
470
"""
476
471
477
472
keyvalues = {"user_id" : user_id }
478
473
if device_id is not None :
479
474
keyvalues ["device_id" ] = device_id
480
475
481
- res = yield self .db_pool .simple_select_list (
476
+ res = await self .db_pool .simple_select_list (
482
477
table = "devices" ,
483
478
keyvalues = keyvalues ,
484
479
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):
500
495
}
501
496
return ret
502
497
503
- @defer .inlineCallbacks
504
- def get_user_ip_and_agents (self , user ):
498
+ async def get_user_ip_and_agents (self , user ):
505
499
user_id = user .to_string ()
506
500
results = {}
507
501
@@ -511,7 +505,7 @@ def get_user_ip_and_agents(self, user):
511
505
user_agent , _ , last_seen = self ._batch_row_update [key ]
512
506
results [(access_token , ip )] = (user_agent , last_seen )
513
507
514
- rows = yield self .db_pool .simple_select_list (
508
+ rows = await self .db_pool .simple_select_list (
515
509
table = "user_ips" ,
516
510
keyvalues = {"user_id" : user_id },
517
511
retcols = ["access_token" , "ip" , "user_agent" , "last_seen" ],
0 commit comments