scoop:scoop.http.server:0.1.0
src/http2_activity.scoop
Selected production source · src/http2_activity.scoop
1
package scoop.http.server3
import scoop.collections.*4
import scoop.http.asyncio.Http2Notice5
import scoop.platform.time.MonotonicTimeSource6
import scoop.time.*8
internal class ServerHttp2(private val options: HttpServerOptions, private val started: TimeMark) {9
private val lock: ServerLock = ServerLock()10
private val queued: HashSet<Int> = HashSet<Int>()11
private val requests: HashMap<Int, Job> = HashMap<Int, Job>()12
private var first: Boolean = true13
private var head: (Int, TimeMark)? = None14
private var idle: TimeMark? = None15
private var draining: Boolean = false16
private var drainStarted: Boolean = false17
private var peerDraining: Boolean = false18
private var problem: Throwable? = None19
val changes: ServerSignal = ServerSignal()21
val failure: Throwable? get() = lock.withLock { problem }23
fun observe(event: Http2Notice) {24
val cancel: Job? = lock.withLock {25
var job: Job? = None26
when (event) {27
case RequestHeadStarted(val id) -> { head = Some((id, MonotonicTimeSource.markNow())) }28
case RequestHeadReceived(val id) -> { clearHead(id); first = false; queued.add(id) }29
case StreamFailure(val id, _) -> { clearHead(id); queued.remove(id); job = requests[id] }30
case StreamClosed(val id, val code) -> {31
clearHead(id)32
if (code != 0U) queued.remove(id)33
}34
case GoAway(_, _, _, _) -> { peerDraining = true; head = None }35
else -> { return@withLock None }36
}37
updateIdle()38
job39
}40
if (present(cancel)) cancel!!.cancel()41
changes.signal()42
}44
fun register(id: Int, job: Job) {45
lock.withLock { queued.remove(id); requests[id] = job; updateIdle() }46
changes.signal()47
}49
fun finished(id: Int) {50
lock.withLock { queued.remove(id); requests.remove(id); updateIdle() }51
changes.signal()52
}54
fun requestDrain() {55
lock.withLock { draining = true }56
changes.signal()57
}59
fun takeDrainRequest(): Boolean = lock.withLock {60
if (!draining || drainStarted) false else { drainStarted = true; true }61
}63
fun fail(error: Throwable) { lock.withLock { if (!present(problem)) problem = Some(error); Unit } }65
fun nextDeadline(): Http2ServerDeadline? = lock.withLock {66
if (draining || peerDraining) return@withLock None67
var selected: Http2ServerDeadline? = None68
if (first && present(options.firstRequestHeadTimeout))69
selected = Some(Http2ServerDeadline("first HTTP/2 request head", options.firstRequestHeadTimeout!!, started))70
if (present(head) && present(options.http2HeadTimeout)) {71
val candidate = Http2ServerDeadline("HTTP/2 request head", options.http2HeadTimeout!!, head!!._2)72
if (!present(selected) || candidate.end < selected!!.end) selected = Some(candidate)73
}74
if (present(idle) && present(options.keepAliveIdleTimeout)) {75
val candidate = Http2ServerDeadline("HTTP/2 idle connection", options.keepAliveIdleTimeout!!, idle!!)76
if (!present(selected) || candidate.end < selected!!.end) selected = Some(candidate)77
}78
selected79
}81
private fun clearHead(id: Int) { if (present(head) && head!!._1 == id) head = None }83
private fun updateIdle() {84
if (first || present(head) || queued.size != 0L || requests.size != 0L) idle = None85
else if (!present(idle)) idle = Some(MonotonicTimeSource.markNow())86
}87
}89
internal class Http2ServerDeadline(val operation: String, val limit: Duration, val started: TimeMark) {90
val end: TimeMark get() = started + limit91
fun remaining(): Duration = remainingServerTime(Some(limit), started)!!92
}