π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mob][photos] Coalesce photo change sync and album refreshes
ashil-pilot51a2a0c3d68a
diff --git a/mobile/apps/photos/changes/reduce-backup-album-refreshes.md b/mobile/apps/photos/changes/reduce-backup-album-refreshes.mdnew file mode 100644index 0000000000..377cae4d50--- /dev/null+++ b/mobile/apps/photos/changes/reduce-backup-album-refreshes.md@@ -0,0 +1 @@+- Improved backup performance for large photo libraries.diff --git a/mobile/apps/photos/lib/services/sync/local_sync_scheduler.dart b/mobile/apps/photos/lib/services/sync/local_sync_scheduler.dartnew file mode 100644index 0000000000..c42de92aeb--- /dev/null+++ b/mobile/apps/photos/lib/services/sync/local_sync_scheduler.dart@@ -0,0 +1,46 @@+import "dart:async";++class LocalSyncOperation {+ LocalSyncOperation(this._sync);++ final Future<void> Function() _sync;+ Completer<void>? _running;++ Future<void>? get current => _running?.future;++ Future<void> run() {+ final running = _running;+ if (running != null) {+ return running.future;+ }++ final completion = Completer<void>();+ _running = completion;+ completion.complete(+ Future<void>.sync(_sync).whenComplete(() {+ _running = null;+ }),+ );+ return completion.future;+ }+}++class LocalChangeSyncScheduler {+ LocalChangeSyncScheduler(this._sync);++ final Future<void> Function() _sync;+ late final _operation = LocalSyncOperation(_drain);+ bool _requested = false;++ Future<void> request() {+ _requested = true;+ return _operation.run();+ }++ Future<void> _drain() async {+ do {+ _requested = false;+ await _sync();+ } while (_requested);+ }+}diff --git a/mobile/apps/photos/lib/services/sync/local_sync_service.dart b/mobile/apps/photos/lib/services/sync/local_sync_service.dartindex a63ee6b910..c520f2d007 100644--- a/mobile/apps/photos/lib/services/sync/local_sync_service.dart+++ b/mobile/apps/photos/lib/services/sync/local_sync_service.dart@@ -25,6 +25,7 @@ import "package:photos/services/ignored_files_service.dart";import "package:photos/services/sync/import/diff.dart";import "package:photos/services/sync/import/local_assets.dart";import "package:photos/services/sync/import/model.dart";+import "package:photos/services/sync/local_sync_scheduler.dart";import "package:photos/services/sync/origin_fetch_tracker.dart";import "package:shared_preferences/shared_preferences.dart";import "package:synchronized/synchronized.dart";@@ -34,7 +35,8 @@ class LocalSyncService {final _logger = Logger("LocalSyncService");final _db = FilesDB.instance;late SharedPreferences _prefs;- Completer<void>? _existingSync;+ late final _syncOperation = LocalSyncOperation(_sync);+ late final _changeSyncScheduler = LocalChangeSyncScheduler(_syncLocalChanges);late Debouncer _changeCallbackDebouncer;StreamSubscription<PermissionGrantedEvent>? _permissionGrantedSubscription;bool _isChangeCallbackRegistered = false;@@ -69,13 +71,23 @@ class LocalSyncService {if (isLocalGalleryMode) {// Local gallery onboarding grants permission without explicitly// invoking SyncService, so trigger local import right away.- unawaited(checkAndSync());+ checkAndSync().ignore();}});}}- Future<void> sync() async {+ Future<void> sync() {+ final existingSync = _syncOperation.current;+ if (existingSync != null) {+ _logger.warning("Sync already in progress, skipping.");+ return existingSync;+ }++ return _syncOperation.run();+ }++ Future<void> _sync() async {if (!permissionService.hasGrantedPermissions()) {_logger.info("Skipping local sync since permission has not been granted");return;@@ -92,11 +104,6 @@ class LocalSyncService {}}_registerChangeCallback();- if (_existingSync != null) {- _logger.warning("Sync already in progress, skipping.");- return _existingSync!.future;- }- _existingSync = Completer<void>();final int ownerID = Configuration.instance.getUserIDV2();// Local sync must not race downloads; that can create incorrect FilesDB@@ -160,9 +167,6 @@ class LocalSyncService {final duration = Duration(microseconds: endTime - startTime);_logger.info("Load took " + duration.inMilliseconds.toString() + "ms");});-- _existingSync?.complete();- _existingSync = null;}Future<bool> _refreshDeviceFolderCountAndCover({@@ -491,20 +495,29 @@ class LocalSyncService {PhotoManager.addChangeCallback((value) async {_logger.info("Something changed on disk");_changeCallbackDebouncer.run(() async {- unawaited(checkAndSync());+ checkAndSync().ignore();});});PhotoManager.startChangeNotify();}- Future<void> checkAndSync() async {- if (_existingSync != null) {- await _existingSync!.future;- }- if (permissionService.hasGrantedLimitedPermissions()) {- unawaited(syncAll());- } else {- unawaited(sync().then((value) => _refreshDeviceFolderCountAndCover()));+ Future<void> checkAndSync() => _changeSyncScheduler.request();++ Future<void> _syncLocalChanges() async {+ try {+ final existingSync = _syncOperation.current;+ if (existingSync != null) {+ await existingSync;+ }+ if (permissionService.hasGrantedLimitedPermissions()) {+ await syncAll();+ } else {+ await sync();+ await _refreshDeviceFolderCountAndCover();+ }+ } catch (error, stackTrace) {+ _logger.severe("Failed to sync local photo changes", error, stackTrace);+ rethrow;}}}diff --git a/mobile/apps/photos/test/services/sync/local_sync_scheduler_test.dart b/mobile/apps/photos/test/services/sync/local_sync_scheduler_test.dartnew file mode 100644index 0000000000..485bb43651--- /dev/null+++ b/mobile/apps/photos/test/services/sync/local_sync_scheduler_test.dart@@ -0,0 +1,144 @@+import "dart:async";++import "package:flutter_test/flutter_test.dart";+import "package:photos/services/sync/local_sync_scheduler.dart";++void main() {+ test(+ "a failed local scan releases joined callers and permits retry",+ () async {+ final scan = Completer<void>();+ final failure = StateError("local scan failed");+ var attempts = 0;+ final operation = LocalSyncOperation(() async {+ attempts++;+ if (attempts == 1) await scan.future;+ });++ final first = operation.run();+ final joined = operation.run();+ expect(attempts, 1);+ final expectations = [+ expectLater(first, throwsA(same(failure))),+ expectLater(joined, throwsA(same(failure))),+ ];+ scan.completeError(failure);+ await Future.wait(expectations);++ await operation.run();+ expect(attempts, 2);+ expect(operation.current, isNull);+ },+ );++ test("coalesces callbacks through import and album refresh", () async {+ final imports = List.generate(2, (_) => Completer<void>());+ final refreshes = List.generate(2, (_) => Completer<void>());+ final importStarted = List.generate(2, (_) => Completer<void>());+ final refreshStarted = List.generate(2, (_) => Completer<void>());+ final library = <String>{"existing"};+ final imported = <Set<String>>[];+ var passes = 0;+ var active = 0;+ var maxActive = 0;+ final scheduler = LocalChangeSyncScheduler(() async {+ final pass = passes++;+ active++;+ if (active > maxActive) maxActive = active;+ imported.add(Set.of(library));+ importStarted[pass].complete();+ await imports[pass].future;+ refreshStarted[pass].complete();+ await refreshes[pass].future;+ active--;+ });++ final requests = List.generate(200, (_) => scheduler.request());+ await importStarted[0].future;+ expect(passes, 1);+ imports[0].complete();+ await refreshStarted[0].future;++ library.add("added during refresh");+ requests.addAll(List.generate(200, (_) => scheduler.request()));+ var completed = false;+ final completion = Future.wait(requests).then((_) => completed = true);+ await Future<void>.delayed(Duration.zero);+ expect(passes, 1);+ expect(completed, isFalse);++ refreshes[0].complete();+ await importStarted[1].future;+ expect(imported.last, contains("added during refresh"));+ expect(completed, isFalse);+ imports[1].complete();+ await refreshStarted[1].future;+ expect(completed, isFalse);+ refreshes[1].complete();+ await completion;++ expect(passes, 2);+ expect(maxActive, 1);+ expect(completed, isTrue);+ });++ test("picks up another change during a follow-up pass", () async {+ final gates = List.generate(3, (_) => Completer<void>());+ final started = List.generate(3, (_) => Completer<void>());+ var libraryRevision = 0;+ final importedRevisions = <int>[];+ final scheduler = LocalChangeSyncScheduler(() async {+ final pass = importedRevisions.length;+ importedRevisions.add(libraryRevision);+ started[pass].complete();+ await gates[pass].future;+ });++ final requests = [scheduler.request()];+ await started[0].future;+ libraryRevision++;+ requests.add(scheduler.request());+ gates[0].complete();+ await started[1].future;++ libraryRevision++;+ requests.add(scheduler.request());+ gates[1].complete();+ await started[2].future;+ gates[2].complete();+ await Future.wait(requests);++ expect(importedRevisions, [0, 1, 2]);+ });++ test(+ "reports a failed refresh to waiters and allows a later retry",+ () async {+ final refreshStarted = Completer<void>();+ final refresh = Completer<void>();+ final failure = StateError("album refresh failed");+ var attempts = 0;+ final scheduler = LocalChangeSyncScheduler(() async {+ attempts++;+ if (attempts == 1) {+ refreshStarted.complete();+ await refresh.future;+ throw failure;+ }+ });++ final first = scheduler.request();+ await refreshStarted.future;+ final waiting = scheduler.request();+ final expectations = [+ expectLater(first, throwsA(same(failure))),+ expectLater(waiting, throwsA(same(failure))),+ ];+ refresh.complete();+ await Future.wait(expectations);++ await scheduler.request();+ expect(attempts, 2);+ },+ );+}