scoop:scoop.http.client:0.1.0
src/lifetime/client_state.scoop
Selected production source · src/lifetime/client_state.scoop
1
package scoop.http.client3
import scoop.asyncio.EventLoop4
import scoop.collections.ArrayDeque5
import scoop.tls.TlsClientContext7
internal class ClientState(val loop: EventLoop, val config: HttpClientConfig, val tls: TlsClientContext, val cachePartition: String?) {8
val lock: ClientLock = ClientLock()9
val calls: ArrayList<ClientCall> = ArrayList<ClientCall>()10
val retryWaits: ArrayList<Job> = ArrayList<Job>()11
val downloads: ArrayList<ClientDownload> = ArrayList<ClientDownload>()12
val eventSources: ArrayList<ClientSseState> = ArrayList<ClientSseState>()13
val webSockets: ArrayList<ClientWebSocketState> = ArrayList<ClientWebSocketState>()14
val invocations: ArrayList<ClientInvocation> = ArrayList<ClientInvocation>()15
var webSocketTls: TlsClientContext? = None16
val bearerEntries: ArrayList<ClientBearerEntry> = ArrayList<ClientBearerEntry>()17
val digestEntries: ArrayList<ClientDigestEntry> = ArrayList<ClientDigestEntry>()18
val waiting: ArrayDeque<ClientCall> = ArrayDeque<ClientCall>()19
val upgrades: ArrayDeque<ClientUpgrade> = ArrayDeque<ClientUpgrade>()20
val connections: ArrayList<ClientPoolEntry> = ArrayList<ClientPoolEntry>()21
var scope: CoroutineScope? = None22
var ownerHold: Continuation<Unit>? = None23
var wake: Continuation<Unit>? = None24
var dirty: Boolean = false25
var adopted: Boolean = false26
var closing: Boolean = false27
var closed: Boolean = false28
var closeFailure: Throwable? = None29
val closeWaiters: ArrayList<Continuation<Unit>> = ArrayList<Continuation<Unit>>()30
}32
internal fun wakeClient(state: ClientState) {33
val wake = state.lock.withLock { state.dirty = true; val saved = state.wake; state.wake = None; saved }34
if (clientPresent(wake)) wake!!.resume(Unit)35
}37
internal fun stopClient(state: ClientState) {38
val (calls, retryWaits, bearers, downloads, eventSources, webSockets, invocations) = state.lock.withLock {39
state.closing = true40
val active = ArrayList<ClientBearerFlight>()41
for (entry in state.bearerEntries) if (clientPresent(entry.flight)) active.add(entry.flight!!)42
for (download in state.downloads) {43
if (!download.committing && !clientPresent(download.failure)) download.failure = Some(HttpClientClosedException())44
}45
for (source in state.eventSources) {46
if (!source.finished && !clientPresent(source.failure)) source.failure = Some(HttpClientClosedException())47
}48
(state.calls.toArray(), state.retryWaits.toArray(), active.toArray(), state.downloads.toArray(), state.eventSources.toArray(),49
state.webSockets.toArray(), state.invocations.toArray())50
}51
for (call in calls) cancelClientCall(call, HttpClientClosedException(), true)52
for (waiting in retryWaits) waiting.cancel()53
for (flight in bearers) stopClientBearerFlight(flight)54
for (download in downloads) stopClientDownload(download, HttpClientClosedException())55
for (source in eventSources) stopClientSse(source, HttpClientClosedException())56
for (session in webSockets) stopClientWebSocket(session)57
for (invocation in invocations) stopClientInvocation(invocation, HttpClientClosedException())58
wakeClient(state)59
}