Skip to content

Commit

Permalink
Write to ByteChannel within the client scope
Browse files Browse the repository at this point in the history
  • Loading branch information
Stexxe committed Dec 26, 2024
1 parent 1d1878b commit 165fca2
Showing 1 changed file with 14 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import io.ktor.utils.io.core.writeFully
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.io.Buffer

Expand Down Expand Up @@ -141,9 +142,14 @@ public val Logging: ClientPlugin<LoggingConfig> = createClientPlugin("Logging",

if (!isBinary) {
val channel = ByteChannel()
channel.writeFully(firstChunk, 0, firstReadSize)
val copied = body.copyTo(channel)
channel.flushAndClose()

val copied = client.async {
channel.writeFully(firstChunk, 0, firstReadSize)
val copied = body.copyTo(channel)
channel.flushAndClose()
copied
}.await()

return Triple(isBinary, copied + firstReadSize, channel)
}

Expand Down Expand Up @@ -202,8 +208,11 @@ public val Logging: ClientPlugin<LoggingConfig> = createClientPlugin("Logging",
}
is OutgoingContent.WriteChannelContent -> {
val channel = ByteChannel()
content.writeTo(channel)
channel.close()

client.launch {
content.writeTo(channel)
channel.close()
}

val (origChannel, newChannel) = channel.split(client)
logRequestBody(content, content.contentLength, headers, method, newChannel)
Expand Down

0 comments on commit 165fca2

Please sign in to comment.