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(auth-storage): Thread safety #99

Merged
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
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ buildscript {
classpath(libs.org.jetbrains.kotlin.serialization.plugin)
classpath(libs.com.squareup.sqldelight.gradle.plugin)
classpath(libs.com.google.osdetector.gradle.plugin)
classpath(libs.org.jetbrains.kotlinx.atomicfu.plugin)
}
}

Expand Down
1 change: 1 addition & 0 deletions data/persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ plugins {
id("com.squareup.sqldelight")
id("social.androiddev.code-quality")
kotlin("plugin.serialization")
id("kotlinx-atomicfu")
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ package social.androiddev.common.persistence.localstorage
import com.russhwolf.settings.Settings
import com.russhwolf.settings.get
import com.russhwolf.settings.set
import kotlinx.atomicfu.locks.ReentrantLock
import kotlinx.atomicfu.locks.reentrantLock
import kotlinx.atomicfu.locks.withLock
import kotlinx.serialization.Serializable
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.json.Json

internal class DodoAuthStorageImpl(
private val settings: Settings,
private val json: Json
private val json: Json,
private val lock: ReentrantLock = reentrantLock()
) : DodoAuthStorage {

override var currentDomain: String?
get() = settings[KEY_DOMAIN_CACHE]
set(value) {
Expand All @@ -31,29 +34,31 @@ internal class DodoAuthStorageImpl(
* The user can set up multiple accounts on their device. So we
* key the AccessToken by the unique server/domain
*/
private var diskCache: Map<String, AccessToken>
private var diskCache: LinkedHashMap<String, AccessToken>
get() {
return settings
.getStringOrNull(KEY_ACCESS_TOKENS_CACHE)
?.let { str ->
json.decodeFromString(ListSerializer(AccessToken.serializer()), str)
.associateBy { it.server }
.associateByTo(linkedMapOf()) { it.server }
}
?: mutableMapOf()
?: linkedMapOf()
crocsandcoffee marked this conversation as resolved.
Show resolved Hide resolved
}
set(value) {
val list = value.map { it.value }
settings[KEY_ACCESS_TOKENS_CACHE] =
json.encodeToString(ListSerializer(AccessToken.serializer()), list)
}
private val memCache: LinkedHashMap<String, AccessToken> by lazy { diskCache }

private val memCache: MutableMap<String, AccessToken> by lazy { diskCache.toMutableMap() }

override fun getAccessToken(server: String): String? = memCache[server]?.token
override fun getAccessToken(server: String): String? =
lock.withLock { memCache[server]?.token }

override suspend fun saveAccessToken(server: String, token: String) {
memCache[server] = AccessToken(token = token, server = server)
diskCache = memCache
lock.withLock {
memCache[server] = AccessToken(token = token, server = server)
diskCache = memCache
}
}

private companion object {
Expand Down
10 changes: 7 additions & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@
# plugins
org-jetbrains-compose = "1.2.1"
org-jetbrains-kotlin = "1.7.20"
org-jetbrains-kotlinx-coroutines = "1.6.4"

# Need to match with kotlin version used: https://github.com/Kotlin/kotlinx-atomicfu/blob/0.18.5/gradle.properties
org-jetbrains-kotlinx-atomicfu = "0.18.5"
org-jetbrains-kotlinx-serialization = "1.4.1"
com-android-tools-build = "7.2.2"
com-squareup-sqldelight = "1.5.3"
com-diffplug-spotless = "6.11.0"
com-google-osdetector = "1.7.1"
# libraries
io-ktor = "2.1.3"
com-google-truth = "1.1.3"
org-jetbrains-kotlinx = "1.4.1"
org-jetbrains-kotlinx-coroutines = "1.6.4"
androidx-core = "1.3.1"
androidx-appcompat = "1.5.1"
androidx-activity = "1.6.1"
Expand All @@ -28,6 +31,7 @@ org-jetbrains-compose-gradle-plugin = { module = "org.jetbrains.compose:compose-
org-jetbrains-kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "org-jetbrains-kotlin" }
com-android-tools-build-gradle = { module = "com.android.tools.build:gradle", version.ref = "com-android-tools-build" }
org-jetbrains-kotlin-serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlinx-atomicfu-plugin = { module = "org.jetbrains.kotlinx:atomicfu-gradle-plugin", version.ref = "org-jetbrains-kotlinx-atomicfu" }
com-squareup-sqldelight-gradle-plugin = { module = "com.squareup.sqldelight:gradle-plugin", version.ref = "com-squareup-sqldelight" }
com-diffplug-spotless-gradle-plugin = { module = "com.diffplug.spotless:spotless-plugin-gradle", version.ref = "com-diffplug-spotless" }
com-google-osdetector-gradle-plugin = { module = "com.google.gradle:osdetector-gradle-plugin", version.ref = "com-google-osdetector" }
Expand All @@ -49,7 +53,7 @@ io-insert-koin-android = { module = "io.insert-koin:koin-android", version.ref =
kotlinx-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "org-jetbrains-kotlinx-coroutines" }
kotlinx-coroutines-javafx = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-javafx", version.ref = "org-jetbrains-kotlinx-coroutines" }
org-jetbrains-kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "org-jetbrains-kotlinx-coroutines" }
org-jetbrains-kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "org-jetbrains-kotlinx" }
org-jetbrains-kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "org-jetbrains-kotlinx-serialization" }

org-jetbrains-kotlin-test-junit = { module = "org.jetbrains.kotlin:kotlin-test-junit", version.ref = "org-jetbrains-kotlin" }
org-jetbrains-kotlin-test-common = { module = "org.jetbrains.kotlin:kotlin-test-common", version.ref = "org-jetbrains-kotlin" }
Expand Down