diff --git a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt index 66f1072..0b8a279 100644 --- a/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt +++ b/feature/chat/chat-backend/src/main/kotlin/ru/shadowsparky/chat/backend/data/GigaChatService.kt @@ -8,6 +8,8 @@ import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.http.ContentType import io.ktor.http.contentType +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import org.koin.core.annotation.Single @@ -21,6 +23,7 @@ class GigaChatService( private val tokenManager: GigaChatTokenManager, private val httpClientFactory: HttpClientFactory ) : ChatService { + private val mutex = Mutex() private val httpClient by lazy { httpClientFactory.create( BearerAuthProvider( @@ -36,7 +39,7 @@ class GigaChatService( ) } - override suspend fun getCompletion(userId: Long, messages: List): String { + override suspend fun getCompletion(userId: Long, messages: List): String = mutex.withLock { val clientId = "vbox-$userId" val staticSessionId = UUID.nameUUIDFromBytes(clientId.toByteArray()).toString() val requestBody = GigaChatRequest(