Skip to content

Commit 342a84e

Browse files
committed
fix(model-client): inline small changes in one request
1 parent ede07b6 commit 342a84e

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,9 @@ class ModelClientV2(
301301
return IStream.useSequencesSuspending {
302302
version.write()
303303
val objects = version.fullDiff(baseVersion)
304-
val delta = if (true /* objects.size > 1000 */) {
305-
// large HTTP requests and large Json objects don't scale well
306-
pushObjects(branch.repositoryId, objects.map { it.hash to it.serialize() })
307-
VersionDelta(version.getContentHash(), null)
308-
} else {
309-
VersionDelta(version.getContentHash(), null, objectsMap = objects.toMap({ it.hash }, { it.serialize() }).getSuspending())
310-
}
304+
// large HTTP requests and large Json objects don't scale well
305+
val lastChunk = pushObjects(branch.repositoryId, objects.map { it.hash to it.serialize() }, returnLastChunk = true)
306+
val delta = VersionDelta(version.getContentHash(), null, objectsMap = lastChunk.toMap())
311307
httpClient.preparePost {
312308
url {
313309
takeFrom(baseUrl)
@@ -323,13 +319,18 @@ class ModelClientV2(
323319
}
324320

325321
override suspend fun pushObjects(repository: RepositoryId, objects: Sequence<ObjectHashAndSerializedObject>) {
326-
pushObjects(repository, IStream.many(objects))
322+
pushObjects(repository, IStream.many(objects), false)
327323
}
328324

329-
private suspend fun pushObjects(repository: RepositoryId, objects: IStream.Many<ObjectHashAndSerializedObject>) {
325+
/**
326+
* If the last chunk is smaller than #minBodySize, then the remaining objects are returned for inlining in the
327+
* main request.
328+
*/
329+
private suspend fun pushObjects(repository: RepositoryId, objects: IStream.Many<ObjectHashAndSerializedObject>, returnLastChunk: Boolean): List<ObjectHashAndSerializedObject> {
330330
LOG.debug { "${clientId.toString(16)}.pushObjects($repository)" }
331331
val maxBodySize = 2 * 1024 * 1024
332332
val chunkContent = StringBuilder()
333+
val chunkEntries = ArrayList<ObjectHashAndSerializedObject>()
333334

334335
suspend fun sendChunk() {
335336
httpClient.put {
@@ -341,6 +342,7 @@ class ModelClientV2(
341342
setBody(chunkContent.toString())
342343
}
343344
chunkContent.clear()
345+
chunkEntries.clear()
344346
}
345347

346348
objects.asSequence().forEach { entry ->
@@ -350,8 +352,12 @@ class ModelClientV2(
350352
}
351353
if (chunkContent.isNotEmpty()) chunkContent.append('\n')
352354
chunkContent.append(entry.first).append('\n').append(entry.second)
355+
chunkEntries.add(entry)
356+
}
357+
if (chunkContent.isNotEmpty() && !returnLastChunk) {
358+
sendChunk()
353359
}
354-
if (chunkContent.isNotEmpty()) sendChunk()
360+
return chunkEntries
355361
}
356362

357363
override suspend fun pull(branch: BranchReference, lastKnownVersion: IVersion?): IVersion {

0 commit comments

Comments
 (0)