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 all 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
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,34 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## 2024-07-10

### Changes

---

Packages with breaking changes:

- There are no breaking changes in this release.

Packages with other changes:

- [`powersync` - `v1.3.0-alpha.9`](#powersync---v130-alpha9)
- [`powersync_attachments_helper` - `v0.3.0-alpha.4`](#powersync_attachments_helper---v030-alpha4)

Packages with dependency updates only:

> Packages listed below depend on other packages in this workspace that have had changes. Their versions have been incremented to bump the minimum dependency versions of the packages they depend upon in this project.

- `powersync_attachments_helper` - `v0.3.0-alpha.4`

---

#### `powersync` - `v1.3.0-alpha.9`

- Updated sqlite_async to use Navigator locks for limiting sync stream implementions in multiple tabs


## 2024-07-04

### Changes
Expand Down
6 changes: 3 additions & 3 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 @@ -480,10 +480,10 @@ packages:
dependency: "direct main"
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
sha256: "79e636c857ed43f6cd5e5be72b36967a29f785daa63ff5b078bd34f74f44cb54"
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.8.1"
stack_trace:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions demos/supabase-anonymous-auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ dependencies:
flutter:
sdk: flutter

powersync: 1.3.0-alpha.8
powersync: 1.3.0-alpha.9
path_provider: ^2.1.1
supabase_flutter: ^2.0.2
path: ^1.8.3
logging: ^1.2.0
sqlite_async: ^0.8.0
sqlite_async: ^0.8.1
universal_io: ^2.2.2

dev_dependencies:
Expand Down
6 changes: 3 additions & 3 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 @@ -480,10 +480,10 @@ packages:
dependency: "direct main"
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
sha256: "79e636c857ed43f6cd5e5be72b36967a29f785daa63ff5b078bd34f74f44cb54"
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.8.1"
stack_trace:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions demos/supabase-edge-function-auth/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ dependencies:
flutter:
sdk: flutter

powersync: 1.3.0-alpha.8
powersync: 1.3.0-alpha.9
path_provider: ^2.1.1
supabase_flutter: ^2.0.2
path: ^1.8.3
logging: ^1.2.0
sqlite_async: ^0.8.0
sqlite_async: ^0.8.1
universal_io: ^2.2.2

dev_dependencies:
Expand Down
6 changes: 3 additions & 3 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 @@ -536,10 +536,10 @@ packages:
dependency: transitive
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
sha256: "79e636c857ed43f6cd5e5be72b36967a29f785daa63ff5b078bd34f74f44cb54"
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.8.1"
stack_trace:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion demos/supabase-simple-chat/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies:

supabase_flutter: ^1.10.25
timeago: ^3.6.0
powersync: 1.3.0-alpha.8
powersync: 1.3.0-alpha.9
path_provider: ^2.1.1
path: ^1.8.3
logging: ^1.2.0
Expand Down
8 changes: 4 additions & 4 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 @@ -591,10 +591,10 @@ packages:
dependency: "direct main"
description:
name: sqlite_async
sha256: "7c5a9bec86b6f5b7511b9ba30974fa7ea470aee2dc0d5b7021f6321a439a8d63"
sha256: "79e636c857ed43f6cd5e5be72b36967a29f785daa63ff5b078bd34f74f44cb54"
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.8.1"
stack_trace:
dependency: transitive
description:
Expand Down
6 changes: 3 additions & 3 deletions demos/supabase-todolist/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ environment:
dependencies:
flutter:
sdk: flutter
powersync_attachments_helper: ^0.3.0-alpha.3
powersync_attachments_helper: ^0.3.0-alpha.4

powersync: 1.3.0-alpha.8
powersync: 1.3.0-alpha.9
path_provider: ^2.1.1
supabase_flutter: ^2.0.1
path: ^1.8.3
logging: ^1.2.0
camera: ^0.10.5+7
image: ^4.1.3
universal_io: ^2.2.2
sqlite_async: ^0.8.0
sqlite_async: ^0.8.1

dev_dependencies:
flutter_test:
Expand Down
4 changes: 4 additions & 0 deletions packages/powersync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.0-alpha.9

- Updated sqlite_async to use Navigator locks for limiting sync stream implementations in multiple tabs

## 1.3.0-alpha.8

- **FIX**(powersync-attachements-helper): pubspec file (#29).
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
5 changes: 3 additions & 2 deletions packages/powersync/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: powersync
version: 1.3.0-alpha.8
version: 1.3.0-alpha.9
homepage: https://powersync.com
repository: https://github.com/powersync-ja/powersync.dart
description: PowerSync Flutter SDK - keep PostgreSQL databases in sync with on-device SQLite databases.
Expand All @@ -10,7 +10,8 @@ dependencies:
flutter:
sdk: flutter

sqlite_async: ^0.8.0
sqlite_async: ^0.8.1

universal_io: ^2.0.0
sqlite3_flutter_libs: ^0.5.15
meta: ^1.0.0
Expand Down
4 changes: 4 additions & 0 deletions packages/powersync_attachments_helper/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.0-alpha.4

- Update a dependency to the latest release.

## 0.3.0-alpha.3

- Update a dependency to the latest release.
Expand Down
4 changes: 2 additions & 2 deletions packages/powersync_attachments_helper/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: powersync_attachments_helper
description: A helper library for handling attachments when using PowerSync.
version: 0.3.0-alpha.3
version: 0.3.0-alpha.4
repository: https://github.com/powersync-ja/powersync.dart
homepage: https://www.powersync.com/
environment:
Expand All @@ -10,7 +10,7 @@ dependencies:
flutter:
sdk: flutter

powersync: 1.3.0-alpha.8
powersync: 1.3.0-alpha.9
logging: ^1.2.0
sqlite3: "^2.4.4"
path_provider: ^2.0.13
Expand Down
Loading