Skip to content

Commit 4b8b909

Browse files
committed
refactor(details): improve thread safety and resource management in TranslationRepositoryImpl
This commit enhances the `TranslationRepositoryImpl` by introducing thread-safe cache access using a Mutex and ensuring proper disposal of the HTTP client. - **refactor(details)**: Implemented `AutoCloseable` in `TranslationRepositoryImpl` to properly close the `httpClient`. - **refactor(details)**: Added `Mutex` to synchronize access to the translation cache, preventing potential race conditions. - **refactor(details)**: Removed unused `GitHubStoreLogger` dependency from `TranslationRepositoryImpl`.
1 parent b332a5d commit 4b8b909

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

feature/details/data/src/commonMain/kotlin/zed/rainxch/details/data/repository/TranslationRepositoryImpl.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,26 @@ import io.ktor.client.HttpClient
44
import io.ktor.client.request.get
55
import io.ktor.client.request.parameter
66
import io.ktor.client.statement.bodyAsText
7+
import kotlinx.coroutines.sync.Mutex
8+
import kotlinx.coroutines.sync.withLock
79
import kotlinx.serialization.json.Json
810
import kotlinx.serialization.json.jsonArray
911
import kotlinx.serialization.json.jsonPrimitive
1012
import zed.rainxch.core.data.network.createPlatformHttpClient
1113
import zed.rainxch.core.data.services.LocalizationManager
12-
import zed.rainxch.core.domain.logging.GitHubStoreLogger
1314
import zed.rainxch.core.domain.model.ProxyConfig
1415
import zed.rainxch.details.domain.model.TranslationResult
1516
import zed.rainxch.details.domain.repository.TranslationRepository
1617

1718
class TranslationRepositoryImpl(
18-
private val logger: GitHubStoreLogger,
1919
private val localizationManager: LocalizationManager
20-
) : TranslationRepository {
20+
) : TranslationRepository, AutoCloseable {
2121

2222
private val httpClient: HttpClient = createPlatformHttpClient(ProxyConfig.None)
2323

2424
private val json = Json { ignoreUnknownKeys = true; isLenient = true }
2525

26+
private val cacheMutex = Mutex()
2627
private val cache = LinkedHashMap<String, TranslationResult>(50, 0.75f, true)
2728
private val maxCacheSize = 50
2829
private val maxChunkSize = 4500
@@ -33,7 +34,7 @@ class TranslationRepositoryImpl(
3334
sourceLanguage: String
3435
): TranslationResult {
3536
val cacheKey = "${text.hashCode()}:$targetLanguage"
36-
cache[cacheKey]?.let { return it }
37+
cacheMutex.withLock { cache[cacheKey] }?.let { return it }
3738

3839
val chunks = chunkText(text)
3940
val translatedChunks = mutableListOf<String>()
@@ -52,11 +53,13 @@ class TranslationRepositoryImpl(
5253
detectedSourceLanguage = detectedLang
5354
)
5455

55-
if (cache.size >= maxCacheSize) {
56-
val firstKey = cache.keys.first()
57-
cache.remove(firstKey)
56+
cacheMutex.withLock {
57+
if (cache.size >= maxCacheSize) {
58+
val firstKey = cache.keys.first()
59+
cache.remove(firstKey)
60+
}
61+
cache[cacheKey] = result
5862
}
59-
cache[cacheKey] = result
6063
return result
6164
}
6265

@@ -164,4 +167,8 @@ class TranslationRepositoryImpl(
164167
chunks.add(Pair(currentChunk.toString(), "\n"))
165168
}
166169
}
170+
171+
override fun close() {
172+
httpClient.close()
173+
}
167174
}

0 commit comments

Comments
 (0)