Scoop API

scoop:scoop.http.client:0.1.0

src/timeouts/progress.scoop

Selected production source · src/timeouts/progress.scoop

1package scoop.http.client
3import scoop.asyncio.*
4import scoop.http.HttpVersion
5import scoop.platform.memory.NativeByteBuffer
7internal suspend fun sendClientBytes(call: ClientCall, destination: ClientExchange, buffer: NativeByteBuffer, count: Long): Long {
8 val before = if (call.tracksProgress) buffer.readPosition else 0L
9 val written = try { destination.writeFrom(buffer, count) }
10 catch (error: Throwable) { recordClientUploadIoFailure(call, error, destination.version == HttpVersion.Http2); throw error }
11 finally {
12 if (call.tracksProgress)
13 call.lock.withLock { call.requestBodyBytes = addClientProgress(call.requestBodyBytes, buffer.readPosition - before) }
14 Unit
15 }
16 if (call.tracksProgress) requireClientCall(call)
17 if (written > 0L && clientPresent(call.observation)) emitClientEvent(call, HttpCallEventKind.RequestProgress)
18 return written
21internal suspend fun receiveClientBytes(call: ClientCall, source: AsyncByteSource, buffer: NativeByteBuffer,
22 count: Long, encoded: Boolean): Long {
23 if (!call.tracksProgress) return source.readInto(buffer, count)
24 val before = buffer.writePosition
25 try { return source.readInto(buffer, count) }
26 finally {
27 val amount = buffer.writePosition - before
28 call.lock.withLock {
29 if (encoded) call.encodedResponseBodyBytes = addClientProgress(call.encodedResponseBodyBytes, amount)
30 else call.responseBodyBytes = addClientProgress(call.responseBodyBytes, amount)
31 }
32 }
35internal class ClientResponseWireSource(private val call: ClientCall) : AsyncByteSource {
36 public override suspend fun readInto(buffer: NativeByteBuffer, count: Long): Long =
37 receiveClientBytes(call, call.responseStream!!, buffer, count, true)
40private fun addClientProgress(previous: Long, count: Long): Long =
41 if (count > Long.MAX_VALUE - previous) Long.MAX_VALUE else previous + count