Scoop API

scoop:scoop.http.client:0.1.0

src/interceptors/lifetime.scoop

Selected production source · src/interceptors/lifetime.scoop

1package scoop.http.client
3import scoop.platform.time.MonotonicTimeSource
4import scoop.time.Duration
6internal suspend fun interceptClientSequence(
7 client: ClientState,
8 request: HttpRequest,
9 sequence: ClientRequestSequence
10): HttpCallResult {
11 if (client.config.interceptors.size == 0L)
12 return runClientSequence(client, request, sequence)
13 val invocation = ClientInvocation(client, clientJob(client.loop, true), sequence)
14 try {
15 context(invocation) {
16 coroutineScope<Unit> {
17 val scope = this
19 registerClientInvocation(invocation, scope.job)
20 val listener =
21 scope.job.invokeOnCancellation {
22 reason -> client.lock.withLock {
23 if (!clientPresent(invocation.failure))
24 invocation.failure = Some(reason)
25 Unit
26 }
27 }
28 val timer = startClientInvocationTimer(invocation, scope)
29 try {
30 val selected = invokeClientInterceptor(invocation, 0L, request)
31 invocation.selected = Some(selected)
32 if (
33 clientResultIsWebSocket(selected) !=
34 clientPresent(sequence.webSocket) ||
35 clientResultOwner(selected) !==
36 client
37 )
38 throw IllegalStateException(
39 Some("HTTP interceptor result must match this client and request kind")
40 )
41 invocation.requireActive()
42 } catch (error: Throwable) {
43 recordClientInvocationFailure(invocation, error);
44 throw error
45 }
46 finally {
47 timer?.cancel();
48 listener.dispose()
49 }
50 }
51 }
52 invocation.requireActive()
53 } catch (error: Throwable) {
54 recordClientInvocationFailure(invocation, error)
55 }
56 try {
57 val keep: HttpCallResult? =
58 if (clientPresent(clientInvocationFailure(invocation)))
59 None
60 else
61 invocation.selected
62 closeClientInvocationResults(invocation, keep)
63 if (!clientPresent(clientInvocationFailure(invocation))) {
64 try {
65 invocation.requireActive()
66 }
67 catch (error: Throwable) {
68 recordClientInvocationFailure(invocation, error)
69 }
70 }
71 val published =
72 client.lock.withLock {
73 if (client.closing && !clientPresent(invocation.failure))
74 invocation.failure = Some(HttpClientClosedException())
75 if (clientPresent(invocation.failure) || clientPresent(invocation.cleanupFailure))
76 false
77 else {
78 invocation.published = true;
79 true
80 }
81 }
82 if (!published)
83 closeClientInvocationResults(invocation, None)
84 } finally {
85 unregisterClientInvocation(invocation)
86 }
87 val failure = clientInvocationFailure(invocation)
88 if (clientPresent(failure))
89 throw failure!!
90 return invocation.selected!!
93private fun registerClientInvocation(invocation: ClientInvocation, job: Job) {
94 invocation.client.lock.withLock {
95 if (invocation.client.closing)
96 throw HttpClientClosedException()
97 invocation.job = Some(job)
98 invocation.registered = true
99 invocation.client.invocations.add(invocation)
100 }
103private fun unregisterClientInvocation(invocation: ClientInvocation) {
104 val client = invocation.client
105 client.lock.withLock {
106 if (!invocation.registered)
107 return@withLock
108 var index = 0L
109 while (index < client.invocations.size) {
110 if (client.invocations[index] === invocation) {
111 client.invocations.removeAt(index);
112 break
113 }
114 index += 1L
115 }
116 invocation.registered = false;
117 invocation.job = None
118 if (client.closing && clientPresent(invocation.cleanupFailure))
119 client.closeFailure =
120 appendClientCleanup(client.closeFailure, invocation.cleanupFailure!!)
121 Unit
122 }
123 wakeClient(client)
126private fun startClientInvocationTimer(invocation: ClientInvocation, scope: CoroutineScope): Job? {
127 val limit = invocation.sequence.timeouts.total ?: return None
128 return Some(
129 scope.launch suspend {
130 try {
131 val remaining =
132 limit - (MonotonicTimeSource.markNow() - invocation.sequence.started!!)
133 if (remaining > Duration.ZERO)
134 invocation.client.loop.sleep(remaining)
135 stopClientInvocation(
136 invocation,
137 clientSequenceTimeout(invocation.client, invocation.sequence, None)
138 )
139 } catch (error: Throwable) {
140 if (!(error is CancellationException))
141 stopClientInvocation(invocation, error)
142 }
143 }
144 )