Skip to content

[Feature] Implement web sync locks #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions demos/supabase-anonymous-auth/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ packages:
path: "../../packages/powersync"
relative: true
source: path
version: "1.3.0-alpha.7"
version: "1.3.0-alpha.8"
realtime_client:
dependency: transitive
description:
Expand Down Expand Up @@ -479,10 +479,11 @@ packages:
sqlite_async:
dependency: "direct main"
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
url: "https://pub.dev"
source: hosted
path: "packages/sqlite_async"
ref: "feat/navigator-locks"
resolved-ref: "588d92f3dc98ab2629d2b420ad607639b835214b"
url: "https://github.com/powersync-ja/sqlite_async.dart.git"
source: git
version: "0.8.0"
stack_trace:
dependency: transitive
Expand Down
6 changes: 5 additions & 1 deletion demos/supabase-anonymous-auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ dependencies:
supabase_flutter: ^2.0.2
path: ^1.8.3
logging: ^1.2.0
sqlite_async: ^0.8.0
sqlite_async:
git:
url: https://github.com/powersync-ja/sqlite_async.dart.git
ref: feat/navigator-locks
path: packages/sqlite_async
universal_io: ^2.2.2

dev_dependencies:
Expand Down
11 changes: 6 additions & 5 deletions demos/supabase-edge-function-auth/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ packages:
path: "../../packages/powersync"
relative: true
source: path
version: "1.3.0-alpha.7"
version: "1.3.0-alpha.8"
realtime_client:
dependency: transitive
description:
Expand Down Expand Up @@ -479,10 +479,11 @@ packages:
sqlite_async:
dependency: "direct main"
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
url: "https://pub.dev"
source: hosted
path: "packages/sqlite_async"
ref: "feat/navigator-locks"
resolved-ref: "588d92f3dc98ab2629d2b420ad607639b835214b"
url: "https://github.com/powersync-ja/sqlite_async.dart.git"
source: git
version: "0.8.0"
stack_trace:
dependency: transitive
Expand Down
6 changes: 5 additions & 1 deletion demos/supabase-edge-function-auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ dependencies:
supabase_flutter: ^2.0.2
path: ^1.8.3
logging: ^1.2.0
sqlite_async: ^0.8.0
sqlite_async:
git:
url: https://github.com/powersync-ja/sqlite_async.dart.git
ref: feat/navigator-locks
path: packages/sqlite_async
universal_io: ^2.2.2

dev_dependencies:
Expand Down
11 changes: 6 additions & 5 deletions demos/supabase-simple-chat/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ packages:
path: "../../packages/powersync"
relative: true
source: path
version: "1.3.0-alpha.7"
version: "1.3.0-alpha.8"
realtime_client:
dependency: transitive
description:
Expand Down Expand Up @@ -535,10 +535,11 @@ packages:
sqlite_async:
dependency: transitive
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
url: "https://pub.dev"
source: hosted
path: "packages/sqlite_async"
ref: "feat/navigator-locks"
resolved-ref: "588d92f3dc98ab2629d2b420ad607639b835214b"
url: "https://github.com/powersync-ja/sqlite_async.dart.git"
source: git
version: "0.8.0"
stack_trace:
dependency: transitive
Expand Down
13 changes: 7 additions & 6 deletions demos/supabase-todolist/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,14 @@ packages:
path: "../../packages/powersync"
relative: true
source: path
version: "1.3.0-alpha.7"
version: "1.3.0-alpha.8"
powersync_attachments_helper:
dependency: "direct main"
description:
path: "../../packages/powersync_attachments_helper"
relative: true
source: path
version: "0.3.0-alpha.2"
version: "0.3.0-alpha.3"
realtime_client:
dependency: transitive
description:
Expand Down Expand Up @@ -590,10 +590,11 @@ packages:
sqlite_async:
dependency: "direct main"
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
url: "https://pub.dev"
source: hosted
path: "packages/sqlite_async"
ref: "feat/navigator-locks"
resolved-ref: "588d92f3dc98ab2629d2b420ad607639b835214b"
url: "https://github.com/powersync-ja/sqlite_async.dart.git"
source: git
version: "0.8.0"
stack_trace:
dependency: transitive
Expand Down
6 changes: 5 additions & 1 deletion demos/supabase-todolist/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ dependencies:
camera: ^0.10.5+7
image: ^4.1.3
universal_io: ^2.2.2
sqlite_async: ^0.8.0
sqlite_async:
git:
url: https://github.com/powersync-ja/sqlite_async.dart.git
ref: feat/navigator-locks
path: packages/sqlite_async

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,17 +134,19 @@ class PowerSyncDatabaseImpl

await isInitialized;

// TODO multitab support
// TODO better multitab support
final storage = BucketStorage(database);

final sync = StreamingSyncImplementation(
adapter: storage,
credentialsCallback: connector.getCredentialsCached,
invalidCredentialsCallback: connector.fetchCredentials,
uploadCrud: () => connector.uploadData(this),
updateStream: updates,
retryDelay: Duration(seconds: 3),
client: FetchClient(mode: RequestMode.cors));
client: FetchClient(mode: RequestMode.cors),
// Only allows 1 sync implementation to run at a time per database
// This should be global (across tabs) when using Navigator locks.
identifier: database.openFactory.path);
sync.statusStream.listen((event) {
setStatus(event);
});
Expand Down
47 changes: 30 additions & 17 deletions packages/powersync/lib/src/streaming_sync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:http/http.dart' as http;
import 'package:powersync/src/abort_controller.dart';
import 'package:powersync/src/exceptions.dart';
import 'package:powersync/src/log_internal.dart';
import 'package:sqlite_async/mutex.dart';

import 'bucket_storage.dart';
import 'connector.dart';
Expand Down Expand Up @@ -37,14 +38,22 @@ class StreamingSyncImplementation {

SyncStatus lastStatus = const SyncStatus();

final Mutex syncMutex, crudMutex;

StreamingSyncImplementation(
{required this.adapter,
required this.credentialsCallback,
this.invalidCredentialsCallback,
required this.uploadCrud,
required this.updateStream,
required this.retryDelay,
required http.Client client}) {
required http.Client client,

/// A unique identifier for this streaming sync implementation
/// A good value is typically the DB file path which it will mutate when syncing.
String? identifier = "unknown"})
: syncMutex = Mutex(identifier: "sync-$identifier"),
crudMutex = Mutex(identifier: "crud-$identifier") {
_client = client;
statusStream = _statusStreamController.stream;
}
Expand All @@ -65,8 +74,10 @@ class StreamingSyncImplementation {
await invalidCredentialsCallback!();
invalidCredentials = false;
}
await streamingSyncIteration(abortController: abortController);
// Continue immediately
// Protect sync iterations with exclusivity (if a valid Mutex is provided)
await syncMutex.lock(
() => streamingSyncIteration(abortController: abortController),
timeout: retryDelay);
} catch (e, stacktrace) {
final message = _syncErrorMessage(e);
isolateLogger.warning('Sync error: $message', e, stacktrace);
Expand Down Expand Up @@ -110,21 +121,23 @@ class StreamingSyncImplementation {
}

Future<bool> uploadCrudBatch() async {
if ((await adapter.hasCrud())) {
_updateStatus(uploading: true);
await uploadCrud();
return false;
} else {
// This isolate is the only one triggering
final updated = await adapter.updateLocalTarget(() async {
return getWriteCheckpoint();
});
if (updated) {
_localPingController.add(null);
}
return crudMutex.lock(() async {
if ((await adapter.hasCrud())) {
_updateStatus(uploading: true);
await uploadCrud();
return false;
} else {
// This isolate is the only one triggering
final updated = await adapter.updateLocalTarget(() async {
return getWriteCheckpoint();
});
if (updated) {
_localPingController.add(null);
}

return true;
}
return true;
}
}, timeout: retryDelay);
}

Future<String> getWriteCheckpoint() async {
Expand Down
7 changes: 6 additions & 1 deletion packages/powersync/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ dependencies:
flutter:
sdk: flutter

sqlite_async: ^0.8.0
sqlite_async:
git:
url: https://github.com/powersync-ja/sqlite_async.dart.git
ref: feat/navigator-locks
path: packages/sqlite_async

universal_io: ^2.0.0
sqlite3_flutter_libs: ^0.5.15
meta: ^1.0.0
Expand Down
Loading