scoop:scoop.http.client:0.1.0
src/lifetime/client_owner.scoop
Selected production source · src/lifetime/client_owner.scoop
1
package scoop.http.client3
import scoop.concurrent.*5
internal suspend fun startClientOwner(state: ClientState, parent: CoroutineScope, caller: Job) {6
val ready = Channel<Unit>(1L)7
val task =8
launchClientTask(parent, { _ -> ready.close() }) {9
try {10
suspendCoroutine<Unit> {11
hold ->12
state.lock.withLock {13
state.ownerHold = Some(hold)14
state.adopted = true15
}16
startCoroutine<Unit>(17
{ runClientOwner(state, ready) },18
ClientOwnerCompletion(state)19
)20
}21
} finally {22
ready.close()23
}24
}25
val cancelled = caller.invokeOnCancellation { _ -> task.cancel() }26
try {27
nonCancellableScope { ready.receive() }28
} catch (error: ChannelClosedException) {29
caller.ensureActive()30
task.ensureActive()31
state.lock.withLock { throw (state.closeFailure ?: HttpClientClosedException()) }32
} finally {33
cancelled.dispose()34
ready.close()35
}36
}38
private suspend fun runClientOwner(state: ClientState, ready: Channel<Unit>) {39
coroutineScope {40
val scope = this42
state.lock.withLock { state.scope = Some(scope) }43
val listener = scope.job.invokeOnCancellation { _ -> stopClient(state) }44
ready.trySend(Unit)45
try {46
driveClientPool(state, scope)47
} catch (error: Throwable) {48
state.lock.withLock {49
if (!(error is CancellationException))50
state.closeFailure = Some(clientCombined(state.closeFailure, error))51
Unit52
}53
stopClient(state)54
driveClientPool(state, scope)55
} finally {56
listener.dispose()57
}58
}59
}61
private class ClientOwnerCompletion(val state: ClientState): Continuation<Unit> {62
public override fun resume(value: Unit) {63
finish(None)64
}65
public override fun resumeWithException(exception: Throwable) {66
finish(Some(exception))67
}68
private fun finish(error: Throwable?) {69
var failure = state.lock.withLock { state.closeFailure }70
if (clientPresent(error) && !(error!! is CancellationException))71
failure = Some(clientCombined(failure, error!!))72
try {73
state.tls.close()74
}75
catch (cleanup: Throwable) {76
failure = Some(clientCombined(failure, cleanup))77
}78
try {79
state.webSocketTls?.close()80
}81
catch (cleanup: Throwable) {82
failure = Some(clientCombined(failure, cleanup))83
}84
val finalFailure = failure85
val(hold, waiters) =86
state.lock.withLock {87
state.closing = true;88
state.closed = true;89
state.closeFailure = finalFailure90
val saved = state.ownerHold91
state.ownerHold = None;92
state.scope = None;93
state.wake = None;94
state.waiting.clear();95
state.upgrades.clear();96
state.bearerEntries.clear()97
state.digestEntries.clear()98
val waiting = state.closeWaiters.toArray()99
state.closeWaiters.clear()100
(saved, waiting)101
}102
if (clientPresent(hold))103
hold!!.resume(Unit)104
for (waiter in waiters) {105
if (clientPresent(finalFailure))106
waiter.resumeWithException(finalFailure!!)107
else108
waiter.resume(Unit)109
}110
}111
}113
internal suspend fun closeClient(state: ClientState) {114
if (state.lock.withLock { state.closed })115
return116
checkClientDownloadClose(state)117
checkClientSseClose(state)118
checkClientWebSocketClose(state)119
checkClientInvocationClose(state)120
checkClientAttemptClose(state)121
checkClientObserverClose(state)122
clientJob(state.loop, false)123
suspendCoroutine<Unit> {124
continuation ->125
val closed =126
state.lock.withLock {127
if (state.closed)128
true129
else {130
state.closeWaiters.add(continuation);131
false132
}133
}134
if (closed)135
continuation.resume(Unit)136
else137
stopClient(state)138
}139
}