Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Types
import de.gesellix.docker.response.JsonChunksReader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
Expand All @@ -17,6 +18,7 @@ import okio.Socket
import okio.appendingSink
import okio.buffer
import java.io.File
import java.io.IOException
import java.io.InputStream
import java.lang.reflect.Type
import java.nio.file.Files
Expand All @@ -37,6 +39,26 @@ fun Response?.isTcpUpgrade(): Boolean {
this.header("Upgrade")?.contains("tcp", ignoreCase = true) ?: false
}

/**
* Runs a blocking read via runInterruptible on Dispatchers.IO, so that cancelling the collecting coroutine
* (e.g. a withTimeout() or an explicit Cancellable.cancel()) interrupts the thread blocked in the read.
*
* Socket implementations report such an interrupt as an IOException (e.g. InterruptedIOException for
* named pipes, ClosedByInterruptException for channels) instead of the InterruptedException
* runInterruptible translates to a cancellation. When the coroutine has been cancelled, that IOException
* is only a consequence of the cancellation, so we report the cancellation instead of a failed read.
*/
@PublishedApi
internal suspend fun <T> readInterruptible(read: () -> T): T =
withContext(Dispatchers.IO) {
try {
runInterruptible { read() }
} catch (e: IOException) {
ensureActive()
throw e
}
}

fun ResponseBody?.consumeFile(): File? {
if (this == null) {
return null
Expand Down Expand Up @@ -71,8 +93,8 @@ inline fun <reified T : Any?> ResponseBody?.consumeStream(mediaType: String?): F
// thread blocked in the underlying read, rather than only taking effect at the next suspension
// point -- which never arrives if the read itself never returns. JsonChunksReader.hasNext()
// already checks Thread.currentThread().isInterrupted() for exactly this reason.
while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) {
val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(T::class.java) } }
while (readInterruptible { reader.hasNext() }) {
val next = readInterruptible { reader.readNext(T::class.java) }
emit(next)
}
this@consumeStream.closeQuietly()
Expand All @@ -98,8 +120,8 @@ fun Socket?.consumeFrames(mediaType: String?): Flow<Frame> {
// See the comment in consumeStream(): runInterruptible+Dispatchers.IO makes cancellation
// (withTimeout(), Cancellable.cancel()) actually interrupt the thread blocked in the underlying
// socket read, which FrameReader.hasNext() already anticipates via its interrupted-flag check.
while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) {
val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(Frame::class.java) } }
while (readInterruptible { reader.hasNext() }) {
val next = readInterruptible { reader.readNext(Frame::class.java) }
emit(next)
}
source.closeQuietly()
Expand All @@ -124,8 +146,8 @@ fun ResponseBody?.consumeFrames(mediaType: String?): Flow<Frame> {
val reader = FrameReader(source(), mediaType)
val events = flow {
// See the comment in consumeStream()/Socket.consumeFrames() above.
while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) {
val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(Frame::class.java) } }
while (readInterruptible { reader.hasNext() }) {
val next = readInterruptible { reader.readNext(Frame::class.java) }
emit(next)
}
this@consumeFrames.closeQuietly()
Expand Down
Loading