Skip to content

Commit b408d87

Browse files
committed
fix(model-server): uploading a new version always failed
Json.decodeFromStream doesn't like to be executed on the thread that handles the HTTP requests.
1 parent b58adbe commit b408d87

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

model-client/src/commonMain/kotlin/org/modelix/model/AutoTransactionsBranch.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ class AutoTransactionsBranch(private val branch: IBranch) : IBranch by branch, I
3535
override fun unwrapBranch(): IBranch = branch
3636
}
3737

38-
3938
open class AutoTransaction(override val branch: IBranch) : ITransaction {
4039
override val tree: ITree
4140
get() = branch.computeReadT { it.tree }

model-client/src/commonMain/kotlin/org/modelix/model/client2/ReplicatedModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ class ReplicatedModel(val client: IModelClientV2, val branchRef: BranchReference
143143
}
144144

145145
companion object {
146-
private val LOG = mu.KotlinLogging.logger { }
146+
private val LOG = mu.KotlinLogging.logger { }
147147
}
148148
}
149149

model-server/src/main/kotlin/org/modelix/model/server/handlers/ModelReplicationServer.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,23 @@ package org.modelix.model.server.handlers
1616

1717
import io.ktor.http.*
1818
import io.ktor.server.application.*
19-
import io.ktor.server.auth.*
2019
import io.ktor.server.plugins.*
21-
import io.ktor.server.plugins.cors.routing.*
22-
import io.ktor.server.plugins.statuspages.*
2320
import io.ktor.server.request.*
2421
import io.ktor.server.response.*
2522
import io.ktor.server.routing.*
2623
import io.ktor.server.websocket.*
2724
import io.ktor.util.pipeline.*
2825
import io.ktor.websocket.*
26+
import kotlinx.coroutines.Dispatchers
2927
import kotlinx.coroutines.Job
28+
import kotlinx.coroutines.withContext
3029
import kotlinx.serialization.ExperimentalSerializationApi
3130
import kotlinx.serialization.encodeToString
3231
import kotlinx.serialization.json.DecodeSequenceMode
3332
import kotlinx.serialization.json.Json
34-
import kotlinx.serialization.json.decodeFromStream
3533
import kotlinx.serialization.json.decodeToSequence
3634
import kotlinx.serialization.json.encodeToStream
37-
import org.modelix.authorization.*
35+
import org.modelix.authorization.getUserName
3836
import org.modelix.model.lazy.RepositoryId
3937
import org.modelix.model.persistent.HashUtil
4038
import org.modelix.model.server.api.ModelQuery
@@ -121,7 +119,7 @@ class ModelReplicationServer(val repositoriesManager: RepositoriesManager) {
121119
call.respondDelta(versionHash, baseVersionHash)
122120
}
123121
post {
124-
val deltaFromClient = Json.decodeFromStream<VersionDelta>(call.receiveStream())
122+
val deltaFromClient = call.receive<VersionDelta>()
125123
storeClient.putAll(deltaFromClient.objects.associateBy { HashUtil.sha256(it) })
126124
val mergedHash = repositoriesManager.mergeChanges(branchRef(), deltaFromClient.versionHash)
127125
call.respondDelta(mergedHash, deltaFromClient.versionHash)
@@ -168,11 +166,14 @@ class ModelReplicationServer(val repositoriesManager: RepositoriesManager) {
168166
}
169167
route("objects") {
170168
post {
171-
Json.decodeToSequence<String>(call.receiveStream(), DecodeSequenceMode.ARRAY_WRAPPED)
172-
.chunked(5000)
173-
.forEach { values ->
174-
storeClient.putAll(values.associateBy { HashUtil.sha256(it) }, true)
175-
}
169+
withContext(Dispatchers.IO) {
170+
Json.decodeToSequence<String>(call.receiveStream(), DecodeSequenceMode.ARRAY_WRAPPED)
171+
.chunked(5000)
172+
.forEach { values ->
173+
storeClient.putAll(values.associateBy { HashUtil.sha256(it) }, true)
174+
}
175+
}
176+
call.respondText("OK")
176177
}
177178
get("{hash}") {
178179
val key = call.parameters["hash"]!!

0 commit comments

Comments
 (0)