Scoop API

scoop:scoop.http.server:0.1.0

src/request_deadline.scoop

Selected production source · src/request_deadline.scoop

1package scoop.http.server
3import scoop.asyncio.*
4import scoop.platform.time.MonotonicTimeSource
5import scoop.time.*
7internal class RequestDeadline(initial: Duration?, private val operation: String = "request") {
8 private val started: TimeMark = MonotonicTimeSource.markNow()
9 private val loop: EventLoop = currentEventLoop()
10 private val lock = ServerLock()
11 private var duration: Duration? = initial
12 private var owner: Job? = None
13 private var alarm: RequestAlarm? = None
14 private var timer: Disposable? = None
15 private var failure: Throwable? = None
16 private var stopped = false
18 val selectedFailure: Throwable? get() = lock.withLock { failure }
20 fun start(job: Job) {
21 requireServerDuration(duration)
22 val previous =
23 lock.withLock {
24 owner = Some(job)
25 replace(duration)
26 }
27 previous?.dispose()
28 }
30 fun set(value: Duration?) {
31 requireServerDuration(value)
32 val previous = lock.withLock { replace(value) }
33 previous?.dispose()
34 }
36 // Called with lock held; timer disposal remains outside the state transition.
37 private fun replace(value: Duration?): Disposable? {
38 if (stopped || present(failure))
39 throw IllegalStateException(Some("request deadline is no longer active"))
40 val job = owner!!
41 val next =
42 if (present(value))
43 Some(RequestAlarm(value!!))
44 else
45 None
46 val registration =
47 when (next) {
48 case Some(val target) -> context(job) {
49 context(loop) {
50 Some(
51 loop.schedule(
52 remainingServerTime(value, started)!!,
53 { error -> expire(target, error) }
54 )
55 )
56 }
57 }
58 case None -> None
59 }
60 val saved = timer
61 duration = value
62 alarm = next
63 timer = registration
64 return saved
65 }
67 private fun expire(target: RequestAlarm, error: Throwable?) {
68 val job =
69 lock.withLock {
70 if (stopped || !present(alarm) || alarm!! !== target)
71 None
72 else {
73 failure = Some(error ?: HttpServerTimeoutException(operation, target.limit))
74 alarm = None
75 timer = None
76 owner
77 }
78 }
79 job?.cancel()
80 }
82 fun stop() {
83 val previous =
84 lock.withLock {
85 stopped = true
86 alarm = None
87 owner = None
88 val saved = timer
89 timer = None
90 saved
91 }
92 previous?.dispose()
93 }
96private class RequestAlarm(val limit: Duration)
98internal suspend fun executeRequestPipeline(
99 application: HttpApplication,
100 request: HttpRequest,
101 output: ResponseOutput,
102 requestId: String,
103 info: HttpConnectionInfo,
104 duration: Duration?,
105 result: RequestExecution
106) {
107 val deadline = RequestDeadline(duration)
108 try {
109 coroutineScope<Unit> {
110 deadline.start(job)
111 val ctx = HttpContext(request, output, requestId, job, info, deadline)
112 result.context = Some(ctx)
113 application.execute(ctx)
114 }
115 } catch (error: CancellationException) {
116 throw deadline.selectedFailure ?: error
117 } finally {
118 // The request's entire structured subtree has finished before disarming the timer.
119 deadline.stop()
120 }