Hi, everyone! I am using Ktor and I'm working on ...
# ktor
p
Hi, everyone! I am using Ktor and I'm working on processing streamed data from my DB (Druid). I am trying to process the data and send them to the client in a streamed fashion, however, it seems like the response is not being streamed. I receive a streamed response, and with Gson's
JsonReader
and produce a
Flow<Row>
:
Copy code
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:
Copy code
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?
a
Can you try writing data to the channel without converting it to the
OutputStream
?
p
Still the same issue if I use
writeStringUtf8
with
ByteWriteChannel
directly
a
How do you test streaming?
p
I just use curl / postman for sending a request for a large query. I used VirtualVM for memory profiling and it shows me the heap getting used up by
byte[]
objects
a
Have you used the
--no-buffer
option with curl to disable buffering of the response body?