scoop:scoop.http.client:0.1.0
src/interceptors/lifetime.scoop
Selected production source · src/interceptors/lifetime.scoop
1
package scoop.http.client3
import scoop.platform.time.MonotonicTimeSource4
import scoop.time.Duration6
internal suspend fun interceptClientSequence(7
client: ClientState,8
request: HttpRequest,9
sequence: ClientRequestSequence10
): 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 = this19
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
Unit26
}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
client37
)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 error45
}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
None60
else61
invocation.selected62
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
false77
else {78
invocation.published = true;79
true80
}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!!91
}93
private 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 = true99
invocation.client.invocations.add(invocation)100
}101
}103
private fun unregisterClientInvocation(invocation: ClientInvocation) {104
val client = invocation.client105
client.lock.withLock {106
if (!invocation.registered)107
return@withLock108
var index = 0L109
while (index < client.invocations.size) {110
if (client.invocations[index] === invocation) {111
client.invocations.removeAt(index);112
break113
}114
index += 1L115
}116
invocation.registered = false;117
invocation.job = None118
if (client.closing && clientPresent(invocation.cleanupFailure))119
client.closeFailure =120
appendClientCleanup(client.closeFailure, invocation.cleanupFailure!!)121
Unit122
}123
wakeClient(client)124
}126
private fun startClientInvocationTimer(invocation: ClientInvocation, scope: CoroutineScope): Job? {127
val limit = invocation.sequence.timeouts.total ?: return None128
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
)145
}