@@ -237,8 +237,8 @@ impl FinishCheckerHandle {
237
237
}
238
238
}
239
239
240
- pub struct MasWriter < ' c > {
241
- conn : LockedMasDatabase < ' c > ,
240
+ pub struct MasWriter {
241
+ conn : LockedMasDatabase ,
242
242
writer_pool : WriterConnectionPool ,
243
243
244
244
indices_to_restore : Vec < IndexDescription > ,
@@ -378,7 +378,7 @@ pub async fn is_syn2mas_in_progress(conn: &mut PgConnection) -> Result<bool, Err
378
378
}
379
379
}
380
380
381
- impl < ' conn > MasWriter < ' conn > {
381
+ impl MasWriter {
382
382
/// Creates a new MAS writer.
383
383
///
384
384
/// # Errors
@@ -389,7 +389,7 @@ impl<'conn> MasWriter<'conn> {
389
389
#[ allow( clippy:: missing_panics_doc) ] // not real
390
390
#[ tracing:: instrument( name = "syn2mas.mas_writer.new" , skip_all) ]
391
391
pub async fn new (
392
- mut conn : LockedMasDatabase < ' conn > ,
392
+ mut conn : LockedMasDatabase ,
393
393
mut writer_connections : Vec < PgConnection > ,
394
394
) -> Result < Self , Error > {
395
395
// Given that we don't have any concurrent transactions here,
@@ -544,7 +544,7 @@ impl<'conn> MasWriter<'conn> {
544
544
545
545
#[ tracing:: instrument( skip_all, fields( indicatif. pb_show) ) ]
546
546
async fn restore_indices (
547
- conn : & mut LockedMasDatabase < ' _ > ,
547
+ conn : & mut LockedMasDatabase ,
548
548
indices_to_restore : & [ IndexDescription ] ,
549
549
constraints_to_restore : & [ ConstraintDescription ] ,
550
550
) -> Result < ( ) , Error > {
@@ -577,7 +577,7 @@ impl<'conn> MasWriter<'conn> {
577
577
///
578
578
/// - If the database connection experiences an error.
579
579
#[ tracing:: instrument( skip_all) ]
580
- pub async fn finish ( mut self ) -> Result < ( ) , Error > {
580
+ pub async fn finish ( mut self ) -> Result < PgConnection , Error > {
581
581
self . write_buffer_finish_checker . check_all_finished ( ) ?;
582
582
583
583
// Commit all writer transactions to the database.
@@ -614,12 +614,13 @@ impl<'conn> MasWriter<'conn> {
614
614
. await
615
615
. into_database ( "ending MAS transaction" ) ?;
616
616
617
- self . conn
617
+ let conn = self
618
+ . conn
618
619
. unlock ( )
619
620
. await
620
621
. into_database ( "could not unlock MAS database" ) ?;
621
622
622
- Ok ( ( ) )
623
+ Ok ( conn )
623
624
}
624
625
625
626
/// Write a batch of users to the database.
@@ -1087,33 +1088,33 @@ const WRITE_BUFFER_BATCH_SIZE: usize = 4096;
1087
1088
1088
1089
/// A function that can accept and flush buffers from a `MasWriteBuffer`.
1089
1090
/// Intended uses are the methods on `MasWriter` such as `write_users`.
1090
- type WriteBufferFlusher < ' conn , T > =
1091
- for <' a > fn ( & ' a mut MasWriter < ' conn > , Vec < T > ) -> BoxFuture < ' a , Result < ( ) , Error > > ;
1091
+ type WriteBufferFlusher < T > =
1092
+ for <' a > fn ( & ' a mut MasWriter , Vec < T > ) -> BoxFuture < ' a , Result < ( ) , Error > > ;
1092
1093
1093
1094
/// A buffer for writing rows to the MAS database.
1094
1095
/// Generic over the type of rows.
1095
- pub struct MasWriteBuffer < ' conn , T > {
1096
+ pub struct MasWriteBuffer < T > {
1096
1097
rows : Vec < T > ,
1097
- flusher : WriteBufferFlusher < ' conn , T > ,
1098
+ flusher : WriteBufferFlusher < T > ,
1098
1099
finish_checker_handle : FinishCheckerHandle ,
1099
1100
}
1100
1101
1101
- impl < ' conn , T > MasWriteBuffer < ' conn , T > {
1102
- pub fn new ( writer : & MasWriter , flusher : WriteBufferFlusher < ' conn , T > ) -> Self {
1102
+ impl < T > MasWriteBuffer < T > {
1103
+ pub fn new ( writer : & MasWriter , flusher : WriteBufferFlusher < T > ) -> Self {
1103
1104
MasWriteBuffer {
1104
1105
rows : Vec :: with_capacity ( WRITE_BUFFER_BATCH_SIZE ) ,
1105
1106
flusher,
1106
1107
finish_checker_handle : writer. write_buffer_finish_checker . handle ( ) ,
1107
1108
}
1108
1109
}
1109
1110
1110
- pub async fn finish ( mut self , writer : & mut MasWriter < ' conn > ) -> Result < ( ) , Error > {
1111
+ pub async fn finish ( mut self , writer : & mut MasWriter ) -> Result < ( ) , Error > {
1111
1112
self . flush ( writer) . await ?;
1112
1113
self . finish_checker_handle . declare_finished ( ) ;
1113
1114
Ok ( ( ) )
1114
1115
}
1115
1116
1116
- pub async fn flush ( & mut self , writer : & mut MasWriter < ' conn > ) -> Result < ( ) , Error > {
1117
+ pub async fn flush ( & mut self , writer : & mut MasWriter ) -> Result < ( ) , Error > {
1117
1118
if self . rows . is_empty ( ) {
1118
1119
return Ok ( ( ) ) ;
1119
1120
}
@@ -1123,7 +1124,7 @@ impl<'conn, T> MasWriteBuffer<'conn, T> {
1123
1124
Ok ( ( ) )
1124
1125
}
1125
1126
1126
- pub async fn write ( & mut self , writer : & mut MasWriter < ' conn > , row : T ) -> Result < ( ) , Error > {
1127
+ pub async fn write ( & mut self , writer : & mut MasWriter , row : T ) -> Result < ( ) , Error > {
1127
1128
self . rows . push ( row) ;
1128
1129
if self . rows . len ( ) >= WRITE_BUFFER_BATCH_SIZE {
1129
1130
self . flush ( writer) . await ?;
@@ -1235,10 +1236,7 @@ mod test {
1235
1236
/// Runs some code with a `MasWriter`.
1236
1237
///
1237
1238
/// The callback is responsible for `finish`ing the `MasWriter`.
1238
- async fn make_mas_writer < ' conn > (
1239
- pool : & PgPool ,
1240
- main_conn : & ' conn mut PgConnection ,
1241
- ) -> MasWriter < ' conn > {
1239
+ async fn make_mas_writer ( pool : & PgPool , main_conn : PgConnection ) -> MasWriter {
1242
1240
let mut writer_conns = Vec :: new ( ) ;
1243
1241
for _ in 0 ..2 {
1244
1242
writer_conns. push (
@@ -1260,8 +1258,8 @@ mod test {
1260
1258
/// Tests writing a single user, without a password.
1261
1259
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" ) ]
1262
1260
async fn test_write_user ( pool : PgPool ) {
1263
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1264
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1261
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1262
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1265
1263
1266
1264
writer
1267
1265
. write_users ( vec ! [ MasNewUser {
@@ -1275,7 +1273,7 @@ mod test {
1275
1273
. await
1276
1274
. expect ( "failed to write user" ) ;
1277
1275
1278
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1276
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1279
1277
1280
1278
assert_db_snapshot ! ( & mut conn) ;
1281
1279
}
@@ -1285,8 +1283,8 @@ mod test {
1285
1283
async fn test_write_user_with_password ( pool : PgPool ) {
1286
1284
const USER_ID : Uuid = Uuid :: from_u128 ( 1u128 ) ;
1287
1285
1288
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1289
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1286
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1287
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1290
1288
1291
1289
writer
1292
1290
. write_users ( vec ! [ MasNewUser {
@@ -1309,16 +1307,16 @@ mod test {
1309
1307
. await
1310
1308
. expect ( "failed to write password" ) ;
1311
1309
1312
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1310
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1313
1311
1314
1312
assert_db_snapshot ! ( & mut conn) ;
1315
1313
}
1316
1314
1317
1315
/// Tests writing a single user, with an e-mail address associated.
1318
1316
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" ) ]
1319
1317
async fn test_write_user_with_email ( pool : PgPool ) {
1320
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1321
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1318
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1319
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1322
1320
1323
1321
writer
1324
1322
. write_users ( vec ! [ MasNewUser {
@@ -1342,7 +1340,7 @@ mod test {
1342
1340
. await
1343
1341
. expect ( "failed to write e-mail" ) ;
1344
1342
1345
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1343
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1346
1344
1347
1345
assert_db_snapshot ! ( & mut conn) ;
1348
1346
}
@@ -1351,8 +1349,8 @@ mod test {
1351
1349
/// associated.
1352
1350
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" ) ]
1353
1351
async fn test_write_user_with_unsupported_threepid ( pool : PgPool ) {
1354
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1355
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1352
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1353
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1356
1354
1357
1355
writer
1358
1356
. write_users ( vec ! [ MasNewUser {
@@ -1376,7 +1374,7 @@ mod test {
1376
1374
. await
1377
1375
. expect ( "failed to write phone number (unsupported threepid)" ) ;
1378
1376
1379
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1377
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1380
1378
1381
1379
assert_db_snapshot ! ( & mut conn) ;
1382
1380
}
@@ -1386,8 +1384,8 @@ mod test {
1386
1384
/// real migration, this is done by running a provider sync first.
1387
1385
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" , fixtures( "upstream_provider" ) ) ]
1388
1386
async fn test_write_user_with_upstream_provider_link ( pool : PgPool ) {
1389
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1390
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1387
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1388
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1391
1389
1392
1390
writer
1393
1391
. write_users ( vec ! [ MasNewUser {
@@ -1412,16 +1410,16 @@ mod test {
1412
1410
. await
1413
1411
. expect ( "failed to write link" ) ;
1414
1412
1415
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1413
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1416
1414
1417
1415
assert_db_snapshot ! ( & mut conn) ;
1418
1416
}
1419
1417
1420
1418
/// Tests writing a single user, with a device (compat session).
1421
1419
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" ) ]
1422
1420
async fn test_write_user_with_device ( pool : PgPool ) {
1423
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1424
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1421
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1422
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1425
1423
1426
1424
writer
1427
1425
. write_users ( vec ! [ MasNewUser {
@@ -1450,16 +1448,16 @@ mod test {
1450
1448
. await
1451
1449
. expect ( "failed to write compat session" ) ;
1452
1450
1453
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1451
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1454
1452
1455
1453
assert_db_snapshot ! ( & mut conn) ;
1456
1454
}
1457
1455
1458
1456
/// Tests writing a single user, with a device and an access token.
1459
1457
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" ) ]
1460
1458
async fn test_write_user_with_access_token ( pool : PgPool ) {
1461
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1462
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1459
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1460
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1463
1461
1464
1462
writer
1465
1463
. write_users ( vec ! [ MasNewUser {
@@ -1499,7 +1497,7 @@ mod test {
1499
1497
. await
1500
1498
. expect ( "failed to write access token" ) ;
1501
1499
1502
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1500
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1503
1501
1504
1502
assert_db_snapshot ! ( & mut conn) ;
1505
1503
}
@@ -1508,8 +1506,8 @@ mod test {
1508
1506
/// refresh token.
1509
1507
#[ sqlx:: test( migrator = "mas_storage_pg::MIGRATOR" ) ]
1510
1508
async fn test_write_user_with_refresh_token ( pool : PgPool ) {
1511
- let mut conn = pool. acquire ( ) . await . unwrap ( ) ;
1512
- let mut writer = make_mas_writer ( & pool, & mut conn) . await ;
1509
+ let conn = pool. acquire ( ) . await . unwrap ( ) . detach ( ) ;
1510
+ let mut writer = make_mas_writer ( & pool, conn) . await ;
1513
1511
1514
1512
writer
1515
1513
. write_users ( vec ! [ MasNewUser {
@@ -1560,7 +1558,7 @@ mod test {
1560
1558
. await
1561
1559
. expect ( "failed to write refresh token" ) ;
1562
1560
1563
- writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1561
+ let mut conn = writer. finish ( ) . await . expect ( "failed to finish MasWriter" ) ;
1564
1562
1565
1563
assert_db_snapshot ! ( & mut conn) ;
1566
1564
}
0 commit comments