Scoop API

scoop:scoop.http.client:0.1.0

src/sse/close.scoop

Selected production source · src/sse/close.scoop

1package scoop.http.client
3import scoop.asyncio.EventLoop
4import scoop.asyncio.IoClosedException
6internal fun stopClientSse(state: ClientSseState, reason: Throwable, explicit: Boolean = false) {
7 val job: Job? =
8 state.client.lock.withLock {
9 if (state.closed || state.finished)
10 return@withLock None
11 if (!clientPresent(state.failure)) {
12 state.failure = Some(reason);
13 state.explicitClose = explicit
14 }
15 state.closing = true
16 state.active
17 }
18 job?.cancel()
19 startClientSseClose(state)
22internal suspend fun closeClientSse(state: ClientSseState) {
23 if (state.client.lock.withLock { state.closed })
24 return
25 checkClientSseClose(state.client, Some(state))
26 clientJob(state.client.loop, false)
27 stopClientSse(state, IoClosedException(), true)
28 awaitClientSseClose(state)
29 val failure = state.client.lock.withLock { state.cleanupFailure }
30 if (clientPresent(failure))
31 throw failure!!
34internal suspend fun awaitClientSseClose(state: ClientSseState) {
35 suspendCoroutine<Unit> {
36 continuation ->
37 val closed =
38 state.client.lock.withLock {
39 if (state.closed)
40 true
41 else {
42 state.closeWaiters.add(continuation);
43 false
44 }
45 }
46 if (closed)
47 continuation.resume(Unit)
48 }
51internal fun startClientSseClose(state: ClientSseState) {
52 val job: Job? =
53 state.client.lock.withLock {
54 if (!state.closing || state.running || state.cleanupStarted)
55 return@withLock None
56 state.cleanupStarted = true
57 Some(state.client.scope!!.job)
58 }
59 if (!clientPresent(job))
60 return
61 val owner: Job = job!!
62 val loop: EventLoop = state.client.loop
63 context(loop) {
64 context(owner) {
65 startCoroutine<Unit>(
66 suspend {
67 nonCancellableScope<Unit> {
68 val timer =
69 state.client.lock.withLock {
70 val saved = state.timer;
71 state.timer = None;
72 saved
73 }
74 timer?.cancel()
75 timer?.join()
76 closeClientSseResponse(state)
77 }
78 },
79 ClientSseCloseCompletion(state)
80 )
81 }
82 }
85private class ClientSseCloseCompletion(val state: ClientSseState): Continuation<Unit> {
86 public override fun resume(value: Unit) {
87 finish(None)
88 }
89 public override fun resumeWithException(exception: Throwable) {
90 finish(Some(exception))
91 }
93 private fun finish(error: Throwable?) {
94 val waiters =
95 state.client.lock.withLock {
96 if (clientPresent(error)) {
97 state.cleanupFailure =
98 when (state.failure) {
99 case Some(val primary) -> collectClientSecondary(
100 primary,
101 state.cleanupFailure,
102 error!!
103 )
104 case None -> appendClientCleanup(state.cleanupFailure, error!!)
105 }
106 }
107 state.closed = true
108 if (state.registered) {
109 var index = 0L
110 while (index < state.client.eventSources.size) {
111 if (state.client.eventSources[index] === state) {
112 state.client.eventSources.removeAt(index);
113 break
114 }
115 index += 1L
116 }
117 state.registered = false
118 }
119 if (state.client.closing && clientPresent(state.cleanupFailure))
120 state.client.closeFailure =
121 appendClientCleanup(state.client.closeFailure, state.cleanupFailure!!)
122 val saved = state.closeWaiters.toArray()
123 state.closeWaiters.clear()
124 saved
125 }
126 wakeClient(state.client)
127 for (waiter in waiters)
128 waiter.resume(Unit)
129 }