Scoop API

scoop:scoop.http.client:0.1.0

src/sse/state.scoop

Selected production source · src/sse/state.scoop

1package scoop.http.client
3import scoop.asyncio.IoClosedException
4import scoop.http.HttpResponseHead
5import scoop.http.asyncio.SseSource
6import scoop.platform.time.MonotonicTimeSource
7import scoop.time.Duration
8import scoop.url.Url
10internal class ClientSseInfo(var url: Url, var head: HttpResponseHead)
12internal class ClientSseState(
13 val client: ClientState,
14 val original: HttpRequest,
15 val options: HttpSseOptions,
16 initialId: String
17) {
18 var sequence: ClientRequestSequence = ClientRequestSequence(client.config, original)
19 var lastCall: ClientCall? = None
20 var response: HttpResponse? = None
21 var reader: SseSource? = None
22 var info: ClientSseInfo? = None
23 var lastEventId: String = initialId
24 var delay: Duration =
25 when (options.reconnect) {
26 case Some(val value) -> value.initialDelay;
27 case None -> Duration.ZERO
28 }
29 var reconnects: Long = 0L
30 var registered: Boolean = false
31 var running: Boolean = false
32 var active: Job? = None
33 var timer: Job? = None
34 var closing: Boolean = false
35 var closed: Boolean = false
36 var finished: Boolean = false
37 var explicitClose: Boolean = false
38 var cleanupStarted: Boolean = false
39 var failure: Throwable? = None
40 var cleanupFailure: Throwable? = None
41 val closeWaiters: ArrayList<Continuation<Unit>> = ArrayList<Continuation<Unit>>()
43 fun requireActive() {
44 client.lock.withLock {
45 if (clientPresent(failure))
46 throw failure!!
47 if (closing && !finished)
48 throw IoClosedException()
49 Unit
50 }
51 ensureActive()
52 val limit = sequence.timeouts.total
53 if (clientPresent(limit) && MonotonicTimeSource.markNow() - sequence.started!! >= limit!!)
54 throw timeout()
55 }
57 fun timeout(): HttpTimeoutException {
58 val(current, last) = client.lock.withLock { (sequence, lastCall) }
59 return clientSequenceTimeout(client, current, last)
60 }
63internal fun recordClientSseFailure(state: ClientSseState, error: Throwable) {
64 state.client.lock.withLock {
65 state.finished = false
66 val primary = state.failure
67 if (!clientPresent(primary)) {
68 if (error is ResourceUseException) {
69 state.failure = Some(error.primaryFailure)
70 state.cleanupFailure =
71 collectClientSecondary(
72 error.primaryFailure,
73 state.cleanupFailure,
74 error.cleanupFailure
75 )
76 } else
77 state.failure = Some(error)
78 } else
79 state.cleanupFailure = collectClientSecondary(primary!!, state.cleanupFailure, error)
80 state.closing = true
81 }
84internal fun clientSseFailure(state: ClientSseState): Throwable? =
85 when (state.cleanupFailure) {
86 case Some(val cleanup) -> Some(clientCombined(state.failure, cleanup))
87 case None -> state.failure
88 }
90internal fun checkClientSseClose(client: ClientState, source: ClientSseState? = None) {
91 val bound = contextOrNone<ClientSseState>()
92 if (
93 clientPresent(bound) &&
94 bound!!.client ===
95 client &&
96 (!clientPresent(source) || bound!! === source!!) &&
97 client.lock.withLock { bound!!.running }
98 )
99 throw IllegalStateException(
100 Some("cannot await SSE or client close from an active event-source operation")
101 )