Scoop API

scoop:scoop.http.server:0.1.0

src/http2_activity.scoop

Selected production source · src/http2_activity.scoop

1package scoop.http.server
3import scoop.collections.*
4import scoop.http.asyncio.Http2Notice
5import scoop.platform.time.MonotonicTimeSource
6import scoop.time.*
8internal 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 = true
13 private var head: (Int, TimeMark)? = None
14 private var idle: TimeMark? = None
15 private var draining: Boolean = false
16 private var drainStarted: Boolean = false
17 private var peerDraining: Boolean = false
18 private var problem: Throwable? = None
19 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? = None
26 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 job
39 }
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 None
67 var selected: Http2ServerDeadline? = None
68 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 selected
79 }
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 = None
85 else if (!present(idle)) idle = Some(MonotonicTimeSource.markNow())
86 }
89internal class Http2ServerDeadline(val operation: String, val limit: Duration, val started: TimeMark) {
90 val end: TimeMark get() = started + limit
91 fun remaining(): Duration = remainingServerTime(Some(limit), started)!!