From 2bd2d654c6ed6be68365881f9fb971687522709a Mon Sep 17 00:00:00 2001 From: paregi12 Date: Sun, 28 Jun 2026 03:15:30 +0530 Subject: [PATCH] Refactor CfSessionCache to use kotlinx.atomicfu locks for KMP compatibility --- .../runtime/network/CloudflareKiller.kt | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/network/CloudflareKiller.kt b/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/network/CloudflareKiller.kt index 7199ffa47..126226b0e 100644 --- a/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/network/CloudflareKiller.kt +++ b/composeApp/src/fullCommonMain/kotlin/com/nuvio/app/features/plugins/runtime/network/CloudflareKiller.kt @@ -1,6 +1,7 @@ package com.nuvio.app.features.plugins.runtime.network -import kotlin.concurrent.Synchronized +import kotlinx.atomicfu.locks.SynchronizedObject +import kotlinx.atomicfu.locks.synchronized import kotlinx.coroutines.sync.Mutex import kotlinx.serialization.json.Json import kotlinx.serialization.json.buildJsonObject @@ -42,39 +43,37 @@ internal fun createPlatformWebViewSolver(): WebViewSolver = platformWebViewSolve internal object CfSessionCache { private val sessions = mutableMapOf() private val hostLocks = mutableMapOf() + private val lock = SynchronizedObject() - @Synchronized - fun getMutex(host: String): Mutex = hostLocks.getOrPut(host) { Mutex() } + fun getMutex(host: String): Mutex = synchronized(lock) { + hostLocks.getOrPut(host) { Mutex() } + } - @Synchronized - fun get(host: String, pluginId: String): CfSolveResult? { + fun get(host: String, pluginId: String): CfSolveResult? = synchronized(lock) { sessions[host]?.let { return it } val persisted = runCatching { com.nuvio.app.features.plugins.PluginStorage.loadCfSession(host) }.getOrNull() val parsed = persisted?.let(::jsonStringToCfSolveResult) ?: return null sessions[host] = parsed - return parsed + parsed } - @Synchronized - fun put(host: String, pluginId: String, result: CfSolveResult) { + fun put(host: String, pluginId: String, result: CfSolveResult) = synchronized(lock) { sessions[host] = result runCatching { com.nuvio.app.features.plugins.PluginStorage.saveCfSession(host, result.toJsonString()) } } - @Synchronized - fun evict(host: String, pluginId: String) { + fun evict(host: String, pluginId: String) = synchronized(lock) { sessions.remove(host) runCatching { com.nuvio.app.features.plugins.PluginStorage.removeCfSession(host) } } - @Synchronized - fun evictIfSame(host: String, result: CfSolveResult) { + fun evictIfSame(host: String, result: CfSolveResult) = synchronized(lock) { if (sessions[host] == result) { sessions.remove(host) runCatching { @@ -83,8 +82,7 @@ internal object CfSessionCache { } } - @Synchronized - fun clear() { + fun clear() = synchronized(lock) { sessions.clear() } }