From c43f93b4f11c1ce2c00baa35b5f9046a34563ab5 Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Fri, 5 Apr 2024 16:29:07 +0200 Subject: [PATCH 1/6] reload credentials when invalidated --- .../connectors/PowerSyncBackendConnector.kt | 5 ++- .../kotlin/com/powersync/sync/SyncStream.kt | 32 +++++++++++++------ demos/hello-powersync/iosApp/Podfile.lock | 2 +- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt b/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt index 08632477..1d85e864 100644 --- a/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt +++ b/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt @@ -25,7 +25,9 @@ abstract class PowerSyncBackendConnector { * These credentials may have expired already. */ suspend fun getCredentialsCached(): PowerSyncCredentials? { - cachedCredentials?.let { return it } + if (cachedCredentials != null) { + return cachedCredentials + } return prefetchCredentials() } @@ -51,6 +53,7 @@ abstract class PowerSyncBackendConnector { fetchRequest = fetchRequest ?: GlobalScope.async { fetchCredentials().also { value -> cachedCredentials = value + fetchRequest = null } } diff --git a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt index a6014a6d..62426950 100644 --- a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt +++ b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt @@ -17,8 +17,10 @@ import io.ktor.client.request.get import io.ktor.client.request.headers import io.ktor.client.request.preparePost import io.ktor.client.request.setBody +import io.ktor.client.statement.bodyAsText import io.ktor.http.ContentType import io.ktor.http.HttpHeaders +import io.ktor.http.HttpStatusCode import io.ktor.http.contentType import io.ktor.utils.io.* import kotlinx.coroutines.delay @@ -84,15 +86,21 @@ class SyncStream( connector.invalidateCredentials() invalidCredentials = false } - streamingSyncIteration() + val state = streamingSyncIteration() + if (!state.retry) { + break; + } } catch (e: Exception) { println("SyncStream::streamingSync Error: $e") invalidCredentials = true + updateStatus( + downloadError = e + ) + } finally { updateStatus( connected = false, connecting = true, downloading = false, - downloadError = e ) delay(retryDelay) } @@ -180,6 +188,14 @@ class SyncStream( } request.execute { httpResponse -> + if (httpResponse.status.value == 401) { + connector.invalidateCredentials() + } + + if (httpResponse.status != HttpStatusCode.OK) { + throw RuntimeException("Received error when connecting to sync stream: ${httpResponse.bodyAsText()}") + } + val channel: ByteReadChannel = httpResponse.body() while (!channel.isClosedForRead) { @@ -191,7 +207,7 @@ class SyncStream( } } - private suspend fun streamingSyncIteration() { + private suspend fun streamingSyncIteration(): SyncStreamState { val bucketEntries = bucketStorage.getBucketStates() val initialBuckets = mutableMapOf() @@ -211,14 +227,11 @@ class SyncStream( buckets = initialBuckets.map { (bucket, after) -> BucketRequest(bucket, after) }, ) - streamingSyncRequest(req).retryWhen { cause, attempt -> - println("SyncStream::streamingSyncIteration Error: $cause") - delay(retryDelay) - println("SyncStream::streamingSyncIteration Retrying attempt: $attempt") - true - }.collect { value -> + streamingSyncRequest(req).collect { value -> handleInstruction(value, state) } + + return state; } private suspend fun handleInstruction( @@ -368,6 +381,7 @@ class SyncStream( if (tokenExpiresIn <= 0) { // Connection would be closed automatically right after this println("Token expiring reconnect") + connector.invalidateCredentials() state.retry = true return state } diff --git a/demos/hello-powersync/iosApp/Podfile.lock b/demos/hello-powersync/iosApp/Podfile.lock index 2f512b40..8c576a1c 100644 --- a/demos/hello-powersync/iosApp/Podfile.lock +++ b/demos/hello-powersync/iosApp/Podfile.lock @@ -20,4 +20,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 4680f51fbb293d1385fb2467ada435cc1f16ab3d -COCOAPODS: 1.14.3 +COCOAPODS: 1.13.0 From c22f31af1ed0263288a83df43b70fe26a6578686 Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Fri, 5 Apr 2024 16:45:40 +0200 Subject: [PATCH 2/6] revert test change --- .../com/powersync/connectors/PowerSyncBackendConnector.kt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt b/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt index 1d85e864..73dde9fd 100644 --- a/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt +++ b/core/src/commonMain/kotlin/com/powersync/connectors/PowerSyncBackendConnector.kt @@ -25,9 +25,7 @@ abstract class PowerSyncBackendConnector { * These credentials may have expired already. */ suspend fun getCredentialsCached(): PowerSyncCredentials? { - if (cachedCredentials != null) { - return cachedCredentials - } + cachedCredentials?.let { return it } return prefetchCredentials() } From 027b5233a7df2280ac4bd002444b4d51fd083233 Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Mon, 8 Apr 2024 11:21:29 +0200 Subject: [PATCH 3/6] update lock --- demos/hello-powersync/iosApp/Podfile.lock | 2 +- demos/hello-powersync/iosApp/iosApp/Info.plist | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/demos/hello-powersync/iosApp/Podfile.lock b/demos/hello-powersync/iosApp/Podfile.lock index 8c576a1c..ebc94350 100644 --- a/demos/hello-powersync/iosApp/Podfile.lock +++ b/demos/hello-powersync/iosApp/Podfile.lock @@ -20,4 +20,4 @@ SPEC CHECKSUMS: PODFILE CHECKSUM: 4680f51fbb293d1385fb2467ada435cc1f16ab3d -COCOAPODS: 1.13.0 +COCOAPODS: 1.15.2 diff --git a/demos/hello-powersync/iosApp/iosApp/Info.plist b/demos/hello-powersync/iosApp/iosApp/Info.plist index 412e3781..aaf5e182 100644 --- a/demos/hello-powersync/iosApp/iosApp/Info.plist +++ b/demos/hello-powersync/iosApp/iosApp/Info.plist @@ -2,6 +2,8 @@ + CADisableMinimumFrameDurationOnPhone + CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleExecutable @@ -20,8 +22,6 @@ 1 LSRequiresIPhoneOS - CADisableMinimumFrameDurationOnPhone - UIApplicationSceneManifest UIApplicationSupportsMultipleScenes From b8fbbff9778cca42be24b77552970b37fe4cbfef Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Mon, 8 Apr 2024 11:24:16 +0200 Subject: [PATCH 4/6] comment about retries --- .../commonMain/kotlin/com/powersync/sync/SyncStream.kt | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt index 62426950..97714ac2 100644 --- a/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt +++ b/core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt @@ -86,10 +86,12 @@ class SyncStream( connector.invalidateCredentials() invalidCredentials = false } - val state = streamingSyncIteration() - if (!state.retry) { - break; - } + streamingSyncIteration() +// val state = streamingSyncIteration() +// TODO: We currently always retry +// if (!state.retry) { +// break; +// } } catch (e: Exception) { println("SyncStream::streamingSync Error: $e") invalidCredentials = true From c773ed470f6e53b98b44b13cd85506a578a38c0f Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Mon, 8 Apr 2024 16:12:53 +0200 Subject: [PATCH 5/6] bump version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 79faefdd..fe49db18 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ development=true RELEASE_SIGNING_ENABLED=true # Library config GROUP=com.powersync -LIBRARY_VERSION=0.0.1-ALPHA5 +LIBRARY_VERSION=0.0.1-ALPHA6 GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git # POM POM_URL=https://github.com/powersync-ja/powersync-kotlin/ From 5b845b88241e0072235d208a422ec2fcd501324a Mon Sep 17 00:00:00 2001 From: Steven Ontong Date: Mon, 8 Apr 2024 16:28:14 +0200 Subject: [PATCH 6/6] bump to alpha 7 --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index fe49db18..2a9da6f5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ development=true RELEASE_SIGNING_ENABLED=true # Library config GROUP=com.powersync -LIBRARY_VERSION=0.0.1-ALPHA6 +LIBRARY_VERSION=0.0.1-ALPHA7 GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git # POM POM_URL=https://github.com/powersync-ja/powersync-kotlin/