scoop:scoop.http.client:0.1.0
src/timeouts/deadline.scoop
Selected production source · src/timeouts/deadline.scoop
1
package scoop.http.client3
import scoop.platform.time.MonotonicTimeSource4
import scoop.time.Duration5
import scoop.time.TimeMark7
internal class ClientDeadline(val call: ClientCall, val phase: HttpTimeoutPhase, val limit: Duration, val started: TimeMark) {8
var active: Boolean = true9
var timer: Job? = None10
}12
internal fun startClientDeadline(call: ClientCall, scope: CoroutineScope, phase: HttpTimeoutPhase,13
limit: Duration?, started: TimeMark? = None): ClientDeadline? {14
if (limit == None) return None15
val deadline = ClientDeadline(call, phase, limit!!, started ?: MonotonicTimeSource.markNow())16
call.lock.withLock {17
call.deadlines.add(deadline)18
if (phase == HttpTimeoutPhase.PoolAcquire) call.poolDeadline = Some(deadline)19
Unit20
}21
val timer = scope.launch suspend {22
try {23
val remaining = deadline.limit - (MonotonicTimeSource.markNow() - deadline.started)24
if (remaining > Duration.ZERO) call.client.loop.sleep(remaining)25
expireClientDeadline(deadline)26
} catch (_: CancellationException) {27
} catch (error: Throwable) { failClientCall(call, error) }28
}29
val stopped = call.lock.withLock { deadline.timer = Some(timer); !deadline.active }30
if (stopped) timer.cancel()31
return Some(deadline)32
}34
private fun expireClientDeadline(deadline: ClientDeadline) {35
val call = deadline.call36
val now = MonotonicTimeSource.markNow()37
val error = call.lock.withLock { if (deadline.active) selectExpiredClientDeadline(call, now) else None }38
if (clientPresent(error)) cancelClientCall(call, error!!)39
}41
internal fun finishClientDeadline(deadline: ClientDeadline?) {42
if (!clientPresent(deadline)) return43
val value = deadline!!44
try {45
val elapsed = MonotonicTimeSource.markNow() - value.started46
val error = value.call.lock.withLock {47
if (elapsed >= value.limit) selectClientTimeout(value, elapsed)48
else { value.active = false; None }49
}50
if (clientPresent(error)) cancelClientCall(value.call, error!!)51
} catch (error: Throwable) { failClientCall(value.call, error) }52
finally { stopClientDeadline(deadline) }53
}55
internal fun stopClientDeadline(deadline: ClientDeadline?) {56
if (!clientPresent(deadline)) return57
val value = deadline!!58
val timer = value.call.lock.withLock {59
value.active = false60
val saved = value.timer; value.timer = None; saved61
}62
if (clientPresent(timer)) timer!!.cancel()63
}65
// The pool planner holds call.lock while granting capacity.66
internal fun finishClientPoolWait(call: ClientCall, now: TimeMark): HttpTimeoutException? {67
val expired = selectExpiredClientDeadline(call, now)68
if (clientPresent(expired)) return expired69
if (clientPresent(call.poolDeadline)) call.poolDeadline!!.active = false70
observeClientPoolGranted(call, now)71
return None72
}74
internal fun stopClientDeadlines(call: ClientCall, keepTotal: Boolean = false) {75
val deadlines = call.lock.withLock {76
val saved = call.deadlines.toArray()77
call.deadlines.clear(); call.poolDeadline = None78
if (keepTotal) for (deadline in saved) if (deadline.phase == HttpTimeoutPhase.Total) call.deadlines.add(deadline)79
saved80
}81
for (deadline in deadlines) {82
if (keepTotal && deadline.phase == HttpTimeoutPhase.Total) continue83
if (deadline.phase == HttpTimeoutPhase.Total) finishClientDeadline(Some(deadline))84
else stopClientDeadline(Some(deadline))85
}86
}