Scoop API

scoop:scoop.http.server:0.1.0

src/http1_head_deadline.scoop

Selected production source · src/http1_head_deadline.scoop

1package scoop.http.server
3import scoop.asyncio.*
4import scoop.platform.time.MonotonicTimeSource
5import scoop.time.*
7// Header failure ends this connection, so its existing Job owns the wait and cancellation.
8internal class Http1HeadDeadline(private val job: Job): Closeable {
9 private val loop = currentEventLoop()
10 private val lock = ServerLock()
11 private var waiting = false
12 private var closed = false
13 private var started: TimeMark = MonotonicTimeSource.markNow()
14 private var limit: Duration? = None
15 private var failure: Throwable? = None
16 private var alarm: Http1HeadAlarm? = None
18 fun begin(duration: Duration?) {
19 requireServerDuration(duration)
20 job.ensureActive()
21 lock.withLock {
22 if (closed || waiting || present(failure))
23 throw IllegalStateException(Some("HTTP/1 header wait is not available"))
24 started = MonotonicTimeSource.markNow()
25 limit = duration
26 waiting = true
27 if (present(duration))
28 armLocked(started, duration!!)
29 Unit
30 }
31 }
33 // The lock selects completion against timeout before another request may start.
34 fun finish(): Throwable? =
35 lock.withLock {
36 waiting = false
37 limit = None
38 failure
39 }
41 private fun armLocked(now: TimeMark, remaining: Duration) {
42 when (alarm) {
43 case Some(val existing) -> {
44 if (existing.delay - (now - existing.started) <= remaining)
45 return
46 existing.notification?.dispose()
47 }
48 case None -> {}
49 }
50 val next = Http1HeadAlarm(now, remaining)
51 alarm = Some(next)
52 context(job) {
53 context(loop) {
54 next.notification = Some(loop.schedule(remaining, { error -> tick(next, error) }))
55 }
56 }
57 }
59 private fun tick(selected: Http1HeadAlarm, error: Throwable?) {
60 val cancel =
61 try {
62 lock.withLock {
63 if (closed || !present(alarm) || alarm!! !== selected)
64 return@withLock false
65 alarm = None
66 selected.notification = None
67 if (!waiting || !present(limit))
68 return@withLock false
69 val duration = limit!!
70 val now = MonotonicTimeSource.markNow()
71 val remaining = duration - (now - started)
72 if (present(error) || remaining <= Duration.ZERO) {
73 failure =
74 Some(
75 error ?: HttpServerTimeoutException("HTTP/1 request head", duration)
76 )
77 waiting = false
78 true
79 } else {
80 armLocked(now, remaining)
81 false
82 }
83 }
84 } catch (failure: Throwable) {
85 lock.withLock {
86 if (closed || !waiting)
87 false
88 else {
89 this.failure = Some(failure)
90 waiting = false
91 true
92 }
93 }
94 }
95 if (cancel)
96 job.cancel()
97 }
99 public override fun close() {
100 val previous =
101 lock.withLock {
102 closed = true
103 waiting = false
104 limit = None
105 val saved = alarm
106 alarm = None
107 saved
108 }
109 if (present(previous))
110 previous!!.notification?.dispose()
111 }
114private class Http1HeadAlarm(val started: TimeMark, val delay: Duration) {
115 var notification: Disposable? = None