scoop:scoop.http.client:0.1.0
src/sse/close.scoop
Selected production source · src/sse/close.scoop
1
package scoop.http.client3
import scoop.asyncio.EventLoop4
import scoop.asyncio.IoClosedException6
internal 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 None11
if (!clientPresent(state.failure)) {12
state.failure = Some(reason);13
state.explicitClose = explicit14
}15
state.closing = true16
state.active17
}18
job?.cancel()19
startClientSseClose(state)20
}22
internal suspend fun closeClientSse(state: ClientSseState) {23
if (state.client.lock.withLock { state.closed })24
return25
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!!32
}34
internal suspend fun awaitClientSseClose(state: ClientSseState) {35
suspendCoroutine<Unit> {36
continuation ->37
val closed =38
state.client.lock.withLock {39
if (state.closed)40
true41
else {42
state.closeWaiters.add(continuation);43
false44
}45
}46
if (closed)47
continuation.resume(Unit)48
}49
}51
internal fun startClientSseClose(state: ClientSseState) {52
val job: Job? =53
state.client.lock.withLock {54
if (!state.closing || state.running || state.cleanupStarted)55
return@withLock None56
state.cleanupStarted = true57
Some(state.client.scope!!.job)58
}59
if (!clientPresent(job))60
return61
val owner: Job = job!!62
val loop: EventLoop = state.client.loop63
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
saved73
}74
timer?.cancel()75
timer?.join()76
closeClientSseResponse(state)77
}78
},79
ClientSseCloseCompletion(state)80
)81
}82
}83
}85
private 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 = true108
if (state.registered) {109
var index = 0L110
while (index < state.client.eventSources.size) {111
if (state.client.eventSources[index] === state) {112
state.client.eventSources.removeAt(index);113
break114
}115
index += 1L116
}117
state.registered = false118
}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
saved125
}126
wakeClient(state.client)127
for (waiter in waiters)128
waiter.resume(Unit)129
}130
}