Patrik Ziduliak
07/29/2024, 1:43 PMJsonReader
and produce a Flow<Row>
:
val eventFlow = flow {
while (jsonReader.hasNext()) {
// ... stuff
val row = parseRow(jsonReader, ...)
emit(row)
}
Then, in PipelineContext.handleResultFlow
, I want to send them in a streamed response. I am using com.google.gson.stream.JsonWriter
, but I also tried just using strings. I tried both respondOutputStream
and respondBytesWriter
, but neither seems to work to allow streaming the response to the client:
private suspend fun PipelineContext<Unit, ApplicationCall>.handleResultFlow(
flow: Flow<QSResult.Row>,
...
) {
//...
call.respondBytesWriter(contentType = ContentType.Application.Json) {
val outputStream = this.toOutputStream()
val jsonWriter = JsonWriter((OutputStreamWriter(outputStream)))
jsonWriter.setIndent("") // Disable pretty printing for efficiency
coroutineScope {
launch(<http://Dispatchers.IO|Dispatchers.IO>) {
jsonWriter.beginObject()
// ...
flow.collect { row ->
jsonWriter.beginObject()
// ... write the actual object
jsonWriter.endObject()
jsonWriter.flush()
}
jsonWriter.endArray()
jsonWriter.endObject()
jsonWriter.flush()
}
}.join()
}
I tried also installing the PartialContent
plugin.
How can I make sure that this response is sent to the client streamed? Which part of the code could be causing a potential issue?Aleksei Tirman [JB]
07/29/2024, 3:12 PMOutputStream
?Patrik Ziduliak
07/29/2024, 4:50 PMwriteStringUtf8
with ByteWriteChannel
directlyAleksei Tirman [JB]
07/29/2024, 6:50 PMPatrik Ziduliak
07/30/2024, 5:51 AMbyte[]
objectsAleksei Tirman [JB]
07/30/2024, 7:54 AM--no-buffer
option with curl to disable buffering of the response body?