scoop:scoop.http.asyncio:0.1.0
src/websocket/close_waits.scoop
Selected production source · src/websocket/close_waits.scoop
1
package scoop.http.asyncio3
// Release application waiters before waiting for their operation cleanup.4
internal 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 = None9
}10
if (present(state.receiving)) {11
if (wsClean(state)) succeedWsWait(state.receiving!!, None, actions)12
else failWsWait(state.receiving!!, failure, actions)13
state.receiving = None14
}15
if (present(state.writer)) { failWsWait(state.writer!!.wait, failure, actions); state.writer = None }16
}18
internal 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 = 0L30
while (index < state.closeObservers.size) {31
if (state.closeObservers[index] === continuation) { state.closeObservers.removeAt(index); break }32
index += 1L33
}34
}35
}36
for (action in actions) action()37
}38
}40
internal fun finishWsObservers(state: WsState, actions: ArrayList<() -> Unit>) {41
for (observer in state.closeObservers) finishWsObserver(observer, state.closeFailure, actions)42
state.closeObservers.clear()43
}45
private 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
Unit49
}50
}