scoop:scoop.http.asyncio:0.1.0
src/http1_idle.scoop
Selected production source · src/http1_idle.scoop
1
package scoop.http.asyncio3
import scoop.http.HttpProtocol4
import scoop.platform.time.MonotonicTimeSource5
import scoop.time.Duration6
import scoop.time.TimeMark8
// The connection retains this clock; an absent limit ends the current operation.9
internal class H1Idle(val reading: Boolean, initialLimit: Duration) {10
var limit: Duration? = Some(initialLimit)11
var progress: TimeMark? = None13
fun stop() {14
limit = None15
progress = None16
}18
fun remaining(now: TimeMark): Duration = limit!! - (now - progress!!)20
fun expired(now: TimeMark): HttpIoTimeoutException? {21
val elapsed = now - progress!!22
return if (elapsed >= limit!!)23
Some(timeout(elapsed))24
else25
None26
}28
fun timeout(elapsed: Duration): HttpIoTimeoutException =29
HttpIoTimeoutException(30
if (reading)31
HttpIoDirection.Read32
else33
HttpIoDirection.Write,34
HttpProtocol.Http1,35
0,36
limit!!,37
elapsed38
)39
}41
// A wakeup belongs to the connection, never to an operation that may already have returned.42
internal class H1IdleAlarm(val started: TimeMark, val delay: Duration) {43
var notification: Disposable? = None44
}46
internal fun beginH1Io(state: H1State, reading: Boolean) {47
val error = state.lock.withLock { beginH1IoLocked(state, reading) }48
if (present(error)) {49
driveH1Close(state)50
throw error!!51
}52
}54
private fun beginH1IoLocked(state: H1State, reading: Boolean): HttpIoTimeoutException? {55
h1Open(state)56
val idle =57
if (reading)58
state.readIdle59
else60
state.writeIdle61
if (!present(idle) || !present(idle!!.limit))62
return None63
val value = idle!!64
val limit = value.limit!!65
val now = MonotonicTimeSource.markNow()66
val start = !present(value.progress)67
val expired =68
if (start) {69
value.progress = Some(now)70
if (limit == Duration.ZERO)71
Some(value.timeout(Duration.ZERO))72
else73
None74
} else75
value.expired(now)76
if (present(expired))77
terminateH1(state, expired!!)78
else79
if (start)80
armH1IdleLocked(state, reading, now, limit)81
return expired82
}84
internal fun progressH1Io(state: H1State, reading: Boolean, progressed: Boolean) {85
val error = state.lock.withLock { progressH1IoLocked(state, reading, progressed) }86
if (present(error)) {87
driveH1Close(state)88
throw error!!89
}90
}92
private fun progressH1IoLocked(93
state: H1State,94
reading: Boolean,95
progressed: Boolean96
): HttpIoTimeoutException? {97
h1Open(state)98
val idle =99
if (reading)100
state.readIdle101
else102
state.writeIdle103
if (!present(idle) || !present(idle!!.limit))104
return None105
val clock = idle!!106
val now = MonotonicTimeSource.markNow()107
val expired = clock.expired(now)108
if (present(expired))109
terminateH1(state, expired!!)110
else111
if (progressed)112
clock.progress = Some(now)113
return expired114
}116
// The state lock serializes replacement, callback selection, and connection cleanup.117
private fun armH1IdleLocked(state: H1State, reading: Boolean, now: TimeMark, remaining: Duration) {118
val previous =119
if (reading)120
state.readAlarm121
else122
state.writeAlarm123
when (previous) {124
case Some(val alarm) -> if (alarm.delay - (now - alarm.started) <= remaining)125
return126
case None -> {}127
}128
val alarm = H1IdleAlarm(now, remaining)129
if (reading)130
state.readAlarm = Some(alarm)131
else132
state.writeAlarm = Some(alarm)133
if (present(previous))134
previous!!.notification?.dispose()135
val job = state.ownerJob!!136
val loop = state.loop137
context(job) {138
context(loop) {139
alarm.notification =140
Some(141
loop.schedule(142
remaining,143
{ failure -> finishH1IdleTick(state, reading, alarm, failure) }144
)145
)146
}147
}148
}150
private fun finishH1IdleTick(151
state: H1State,152
reading: Boolean,153
alarm: H1IdleAlarm,154
failure: Throwable?155
) {156
try {157
state.lock.withLock {158
val current =159
if (reading)160
state.readAlarm161
else162
state.writeAlarm163
when (current) {164
case Some(val active) -> if (active !== alarm)165
return@withLock166
case None -> return@withLock167
}168
if (reading)169
state.readAlarm = None170
else171
state.writeAlarm = None172
alarm.notification = None173
if (state.closing)174
return@withLock175
val idle =176
if (reading)177
state.readIdle178
else179
state.writeIdle180
if (!present(idle) || !present(idle!!.progress))181
return@withLock182
val clock = idle!!183
val now = MonotonicTimeSource.markNow()184
val error: Throwable? =185
when (failure) {186
case Some(val value) -> Some(value)187
case None -> when (clock.expired(now)) {188
case Some(val timeout) -> Some(timeout)189
case None -> None190
}191
}192
if (present(error))193
terminateH1(state, error!!)194
else195
armH1IdleLocked(state, reading, now, clock.remaining(now))196
}197
} catch (error: Throwable) {198
state.lock.withLock { terminateH1(state, error) }199
}200
driveH1Close(state)201
}203
// Called under the state lock; disposal runs after the lock is released.204
internal fun retireH1IdleAlarms(state: H1State, actions: ArrayList<() -> Unit>) {205
val read = state.readAlarm206
val write = state.writeAlarm207
state.readAlarm = None208
state.writeAlarm = None209
if (present(read)) {210
val timer = read!!.notification211
if (present(timer))212
actions.add { timer!!.dispose() }213
}214
if (present(write)) {215
val timer = write!!.notification216
if (present(timer))217
actions.add { timer!!.dispose() }218
}219
}