Scoop API

scoop:scoop.http.client:0.1.0

src/lifetime/client_state.scoop

Selected production source · src/lifetime/client_state.scoop

1package scoop.http.client
3import scoop.asyncio.EventLoop
4import scoop.collections.ArrayDeque
5import scoop.tls.TlsClientContext
7internal 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? = None
16 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? = None
22 var ownerHold: Continuation<Unit>? = None
23 var wake: Continuation<Unit>? = None
24 var dirty: Boolean = false
25 var adopted: Boolean = false
26 var closing: Boolean = false
27 var closed: Boolean = false
28 var closeFailure: Throwable? = None
29 val closeWaiters: ArrayList<Continuation<Unit>> = ArrayList<Continuation<Unit>>()
32internal 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)
37internal fun stopClient(state: ClientState) {
38 val (calls, retryWaits, bearers, downloads, eventSources, webSockets, invocations) = state.lock.withLock {
39 state.closing = true
40 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)