Skip to content
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

[Fix] Refresh Credentials #11

Merged
merged 7 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ abstract class PowerSyncBackendConnector {
fetchRequest = fetchRequest ?: GlobalScope.async {
fetchCredentials().also { value ->
cachedCredentials = value
fetchRequest = null
}
}

Expand Down
32 changes: 24 additions & 8 deletions core/src/commonMain/kotlin/com/powersync/sync/SyncStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,14 +87,22 @@ class SyncStream(
invalidCredentials = false
}
streamingSyncIteration()
// val state = streamingSyncIteration()
// TODO: We currently always retry
// 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)
}
Expand Down Expand Up @@ -180,6 +190,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) {
Expand All @@ -191,7 +209,7 @@ class SyncStream(
}
}

private suspend fun streamingSyncIteration() {
private suspend fun streamingSyncIteration(): SyncStreamState {
val bucketEntries = bucketStorage.getBucketStates()
val initialBuckets = mutableMapOf<String, String>()

Expand All @@ -211,14 +229,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(
Expand Down Expand Up @@ -368,6 +383,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
}
Expand Down
2 changes: 1 addition & 1 deletion demos/hello-powersync/iosApp/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 4680f51fbb293d1385fb2467ada435cc1f16ab3d

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
4 changes: 2 additions & 2 deletions demos/hello-powersync/iosApp/iosApp/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>$(DEVELOPMENT_LANGUAGE)</string>
<key>CFBundleExecutable</key>
Expand All @@ -20,8 +22,6 @@
<string>1</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSceneManifest</key>
<dict>
<key>UIApplicationSupportsMultipleScenes</key>
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
Loading