Scoop API

scoop:scoop.http.client:0.1.0

src/interceptors/state.scoop

Selected production source · src/interceptors/state.scoop

1package scoop.http.client
3import scoop.asyncio.AsyncCloseable
4import scoop.platform.time.MonotonicTimeSource
6internal class ClientInvocationResource(val resource: AsyncCloseable) {
7 var closed: Boolean = false
8}
10internal class ClientInvocation(
11 val client: ClientState,
12 val caller: Job,
13 val sequence: ClientRequestSequence
14) {
15 var job: Job? = None
16 var registered: Boolean = false
17 var published: Boolean = false
18 var selected: HttpCallResult? = None
19 var failure: Throwable? = None
20 var cleanupFailure: Throwable? = None
21 val resources: ArrayList<ClientInvocationResource> = ArrayList<ClientInvocationResource>()
23 fun requireActive() {
24 val error = client.lock.withLock { failure }
25 if (clientPresent(error))
26 throw error!!
27 caller.ensureActive()
28 ensureActive()
29 val limit = sequence.timeouts.total
30 if (clientPresent(limit) && MonotonicTimeSource.markNow() - sequence.started!! >= limit!!)
31 throw clientSequenceTimeout(client, sequence, None)
32 }
35internal fun stopClientInvocation(invocation: ClientInvocation, reason: Throwable) {
36 val job: Job? =
37 invocation.client.lock.withLock {
38 if (invocation.published)
39 return@withLock None
40 if (!clientPresent(invocation.failure))
41 invocation.failure = Some(reason)
42 invocation.job
43 }
44 job?.cancel()
47internal fun recordClientInvocationFailure(invocation: ClientInvocation, error: Throwable) {
48 invocation.client.lock.withLock {
49 if (!clientPresent(invocation.failure))
50 invocation.failure = Some(error)
51 else
52 invocation.cleanupFailure =
53 collectClientSecondary(invocation.failure!!, invocation.cleanupFailure, error)
54 Unit
55 }
58internal fun clientInvocationFailure(invocation: ClientInvocation): Throwable? =
59 invocation.client.lock.withLock {
60 if (clientPresent(invocation.cleanupFailure))
61 Some(clientCombined(invocation.failure, invocation.cleanupFailure!!))
62 else
63 invocation.failure
64 }
66internal fun checkClientInvocationClose(client: ClientState) {
67 val invocation = contextOrNone<ClientInvocation>()
68 if (
69 clientPresent(invocation) &&
70 invocation!!.client ===
71 client &&
72 client.lock.withLock { invocation!!.registered }
73 )
74 throw IllegalStateException(Some("cannot await client.close from its active interceptor"))