Scoop API

scoop:scoop.http.asyncio:0.1.0

src/websocket/close_waits.scoop

Selected production source · src/websocket/close_waits.scoop

1package scoop.http.asyncio
3// Release application waiters before waiting for their operation cleanup.
4internal fun finishWsOperations(state: WsState, actions: ArrayList<() -> Unit>) {
5 val failure = state.failure ?: wsClosed(state)
6 if (present(state.reader)) {
7 failWsWait(state.reader!!.wait, state.failure ?: wsInterrupted(state), actions)
8 state.reader = None
9 }
10 if (present(state.receiving)) {
11 if (wsClean(state)) succeedWsWait(state.receiving!!, None, actions)
12 else failWsWait(state.receiving!!, failure, actions)
13 state.receiving = None
14 }
15 if (present(state.writer)) { failWsWait(state.writer!!.wait, failure, actions); state.writer = None }
18internal suspend fun observeWsClosed(state: WsState) {
19 checkWsCloseContext(state)
20 httpJob(state.loop, true)
21 suspendCancellableCoroutine<Unit> { continuation ->
22 val actions = ArrayList<() -> Unit>()
23 state.lock.withLock {
24 if (state.closed) finishWsObserver(continuation, state.closeFailure, actions)
25 else state.closeObservers.add(continuation)
26 }
27 continuation.invokeOnCancellation { _ ->
28 state.lock.withLock {
29 var index = 0L
30 while (index < state.closeObservers.size) {
31 if (state.closeObservers[index] === continuation) { state.closeObservers.removeAt(index); break }
32 index += 1L
33 }
34 }
35 }
36 for (action in actions) action()
37 }
40internal fun finishWsObservers(state: WsState, actions: ArrayList<() -> Unit>) {
41 for (observer in state.closeObservers) finishWsObserver(observer, state.closeFailure, actions)
42 state.closeObservers.clear()
45private fun finishWsObserver(observer: CancellableContinuation<Unit>, failure: Throwable?, actions: ArrayList<() -> Unit>) {
46 actions.add {
47 if (present(failure)) observer.tryResumeWithException(failure!!) else observer.tryResume(Unit)
48 Unit
49 }