scoop:scoop.http.asyncio:0.1.0
src/http1_close.scoop
Selected production source · src/http1_close.scoop
1
package scoop.http.asyncio3
import scoop.asyncio.*5
internal fun terminateH1(state: H1State, primary: Throwable) {6
if (!state.closing)7
state.primary = Some(primary)8
state.closing = true9
}11
internal suspend fun closeH1(state: H1State) {12
if (state.lock.withLock { state.closed })13
return14
httpJob(state.loop, false)15
suspendCoroutine<Unit> {16
continuation ->17
val closed =18
state.lock.withLock {19
if (state.closed)20
true21
else {22
state.closeWaiters.add(continuation)23
terminateH1(state, IoClosedException())24
false25
}26
}27
if (closed)28
continuation.resume(Unit)29
else30
driveH1Close(state)31
}32
}34
internal fun driveH1Close(state: H1State) {35
val actions = ArrayList<() -> Unit>()36
state.lock.withLock {37
if (state.closed || !state.closing)38
return@withLock39
retireH1IdleAlarms(state, actions)40
if (!state.transportClosing) {41
state.transportClosing = true42
actions.add { startH1Close(state) }43
}44
if (state.transportClosed && !state.readBusy && !state.writeBusy)45
finishH1Close(state, actions)46
Unit47
}48
for (action in actions)49
action()50
}52
private fun startH1Close(state: H1State) {53
val job = state.ownerJob!!54
val loop = state.loop55
context(job) {56
context(loop) {57
startCoroutine<Unit>(suspend { state.owner.close() }, H1CloseCompletion(state))58
}59
}60
}62
private class H1CloseCompletion(val state: H1State): Continuation<Unit> {63
public override fun resume(value: Unit) {64
finish(None)65
}66
public override fun resumeWithException(exception: Throwable) {67
finish(Some(exception))68
}69
private fun finish(failure: Throwable?) {70
state.lock.withLock {71
state.transportClosed = true72
state.closeFailure = failure73
}74
driveH1Close(state)75
}76
}78
internal fun finishH1Close(state: H1State, actions: ArrayList<() -> Unit>) {79
retireH1IdleAlarms(state, actions)80
try {81
releaseH1Buffers(state)82
}83
catch (error: Throwable) {84
state.closeFailure =85
Some(86
when (state.closeFailure) {87
case Some(val primary) -> ResourceUseException(primary, error)88
case None -> error89
}90
)91
}92
state.closed = true93
if (present(state.incoming))94
detachH1Body(state.incoming!!.life, actions)95
if (present(state.outgoing))96
detachH1Body(state.outgoing!!.life, actions)97
state.incoming = None98
state.outgoing = None99
state.events.clear()100
state.requests.clear()101
val ownerListener = state.ownerListener102
val ownerWaiter = state.ownerWaiter103
state.ownerListener = None104
state.ownerWaiter = None105
state.ownerJob = None106
actions.add {107
if (present(ownerListener))108
ownerListener!!.dispose()109
if (present(ownerWaiter))110
ownerWaiter!!.resume(Unit)111
}112
val failure = state.closeFailure113
for (waiter in state.closeWaiters)114
actions.add {115
when (failure) {116
case Some(val error) -> waiter.resumeWithException(error);117
case None -> waiter.resume(Unit)118
}119
}120
state.closeWaiters.clear()121
}123
private fun detachH1Body(life: H1BodyLifetime, actions: ArrayList<() -> Unit>) {124
val listener = life.listener125
life.listener = None126
life.disposed = true127
if (present(listener))128
actions.add { listener!!.dispose() }129
}131
internal fun releaseH1Buffers(state: H1State) {132
try {133
state.decoder.close()134
}135
finally {136
try {137
state.encoder.close()138
}139
finally {140
try {141
state.input.close()142
} finally {143
state.output.close()144
}145
}146
}147
}