We have code similar to the following in our ktor ...
# ktor
s
We have code similar to the following in our ktor services where we receive files from the user in an upload endpoint:
Copy code
route(UPLOAD_PATH) {
    post {
        call.receiveMultipart().forEachPart { part ->
            if (part is PartData.FileItem) {
                call.respond(
                    uploadService.store(part)
                )
            }
        }
    }
}
The stack is fully in ktor, so what
uploadService.store()
does contains no blocking code. In a code review it was suggested that this code still could be made more robust with wrapping it in a
withContext(<http://Dispatchers.IO|Dispatchers.IO>)
block like this:
Copy code
route(UPLOAD_PATH) {
    post {
        withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
            call.receiveMultipart().forEachPart { part ->
                if (part is PartData.FileItem) {
                    call.respond(
                        uploadService.store(part)
                    )
                }
            }
        }
    }
}
Is this really necessary? My understanding of coroutines tells me that this is not an improvement when the stack is coroutines all the way down, but am I wrong?
m
if
uploadService.store
does not block then there's no need for
IO
indeed
c
this question would better fit in #C1CFAFJSK, but if
store
is a suspend function, then the implementation of
store
is responsible to use the right dispatcher.
➕ 1