Skip to content

Commit 09b223c

Browse files
authored
Cleanup of the Datastore-related wrapper after the UserSession SQL migration (dart-lang#9584)
1 parent f34c2f6 commit 09b223c

1 file changed

Lines changed: 28 additions & 44 deletions

File tree

‎app/lib/account/backend.dart‎

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ class AccountBackend {
615615
return null;
616616
}
617617
if (session.isExpired()) {
618-
await _db.userSessions.expire(session.sessionId);
618+
await _expireSession(session.sessionId);
619619
return null;
620620
}
621621
return session;
@@ -676,10 +676,10 @@ class AccountBackend {
676676
/// Deletes sessions associated with a [userId] or [sessionId].
677677
Future<void> deleteUserSessions({String? userId, String? sessionId}) async {
678678
if (sessionId != null) {
679-
await _db.userSessions.expire(sessionId);
679+
await _expireSession(sessionId);
680680
}
681681
if (userId != null) {
682-
await _db.userSessions.expireAllForUserId(userId);
682+
await _expireSessionsForUserId(userId);
683683
}
684684
}
685685

@@ -688,8 +688,16 @@ class AccountBackend {
688688
final now = clock.now().toUtc();
689689
// account for possible clock skew
690690
final ts = now.subtract(Duration(minutes: 15));
691-
final count = await _db.userSessions.expireAllBeforeTimestamp(ts);
692-
_logger.info('Deleted $count UserSession entries.');
691+
final sessionIds = await primaryDatabase.withRetry(
692+
(db) => db.userSessions
693+
.where((s) => s.expires.isBeforeValue(ts))
694+
.select((s) => (s.sessionId,))
695+
.fetch(),
696+
);
697+
for (final sessionId in sessionIds) {
698+
await _expireSession(sessionId);
699+
}
700+
_logger.info('Deleted ${sessionIds.length} UserSession entries.');
693701
}
694702

695703
/// Updates the moderated status of a user.
@@ -724,7 +732,7 @@ class AccountBackend {
724732
tx.insert(mc);
725733
}
726734
});
727-
await _db.userSessions.expireAllForUserId(userId);
735+
await _expireSessionsForUserId(userId);
728736
await purgeAccountCache(userId: userId);
729737
}
730738

@@ -741,58 +749,34 @@ class AccountBackend {
741749
}
742750
return query.run();
743751
}
744-
}
745-
746-
/// Purge [cache] entries for given [userId].
747-
Future<void> purgeAccountCache({required String userId}) async {
748-
await Future.wait([
749-
cache.userPackageLikes(userId).purgeAndRepeat(),
750-
cache.publisherPage(userId).purgeAndRepeat(),
751-
cache.userUploaderOfPackages(userId).purgeAndRepeat(),
752-
]);
753-
}
754-
755-
/// Low-level, narrowly typed data access methods for [UserSessionRow] entity.
756-
extension UserSessionDatastoreDBExt on DatastoreDB {
757-
_UserSessionDataAccess get userSessions => _UserSessionDataAccess();
758-
}
759752

760-
class _UserSessionDataAccess {
761-
/// Scans for all sessions the user has, and invalidates
762-
/// them all.
763-
Future<void> expireAllForUserId(String userId) async {
764-
final rows = await primaryDatabase.withRetry(
753+
/// Scans for all sessions the user has, and invalidates them all.
754+
Future<void> _expireSessionsForUserId(String userId) async {
755+
final sessionIds = await primaryDatabase.withRetry(
765756
(db) => db.userSessions
766757
.where((session) => session.userId.equalsValue(userId))
767758
.select((session) => (session.sessionId,))
768759
.fetch(),
769760
);
770-
for (final sessionId in rows) {
771-
await expire(sessionId);
761+
for (final sessionId in sessionIds) {
762+
await _expireSession(sessionId);
772763
}
773764
}
774765

775766
/// Removes the session data from the SQL store and cache.
776-
Future<void> expire(String sessionId) async {
767+
Future<void> _expireSession(String sessionId) async {
777768
await primaryDatabase.withRetry(
778769
(db) => db.userSessions.delete(sessionId).execute(),
779770
);
780771
await cache.userSessionData(sessionId).purge();
781772
}
773+
}
782774

783-
/// Removes the session data that has expiry before [ts].
784-
///
785-
/// Returns the number of deleted rows.
786-
Future<int> expireAllBeforeTimestamp(DateTime ts) async {
787-
final sessionIds = await primaryDatabase.withRetry(
788-
(db) => db.userSessions
789-
.where((s) => s.expires.isBeforeValue(ts))
790-
.select((s) => (s.sessionId,))
791-
.fetch(),
792-
);
793-
for (final sessionId in sessionIds) {
794-
await expire(sessionId);
795-
}
796-
return sessionIds.length;
797-
}
775+
/// Purge [cache] entries for given [userId].
776+
Future<void> purgeAccountCache({required String userId}) async {
777+
await Future.wait([
778+
cache.userPackageLikes(userId).purgeAndRepeat(),
779+
cache.publisherPage(userId).purgeAndRepeat(),
780+
cache.userUploaderOfPackages(userId).purgeAndRepeat(),
781+
]);
798782
}

0 commit comments

Comments
 (0)