Scoop API

scoop:scoop.http.server:0.1.0

src/server_http2.scoop

Selected production source · src/server_http2.scoop

1package scoop.http.server
3import scoop.asyncio.*
4import scoop.http.*
5import scoop.http.asyncio.*
6import scoop.time.TimeMark
8internal suspend fun <T> serveHttp2(
9 state: ServerState,
10 entry: ServerConnection,
11 transport: T,
12 scope: CoroutineScope,
13 started: TimeMark
14) where T: AsyncByteStream, T: AsyncCloseable {
15 val options = state.options
16 val activity = ServerHttp2(options, started)
17 val draining =
18 state.lock.withLock {
19 entry.http2 = Some(activity);
20 state.phase != ServerPhase.Running
21 }
22 if (draining)
23 activity.requestDrain()
24 val connection =
25 AsyncHttp2Connection.takeOwnershipAsServer(
26 transport,
27 scope,
28 options.http2Receive,
29 options.http2Limits,
30 options.http2Settings,
31 HttpIoTimeoutOptions(options.bodyReadIdle, options.responseWriteIdle),
32 { event -> activity.observe(event) }
33 )
34 var failure: Throwable? = None
35 val monitor = scope.launch suspend { watchServerHttp2(activity, connection, scope.job) }
36 try {
37 coroutineScope<Unit> {
38 val requests = this
40 while (true) {
41 val incoming = connection.acceptRequest()
42 if (!present(incoming))
43 break
44 val request = incoming!!
45 val requestId = state.beginRequest(entry, true)
46 if (!present(requestId)) {
47 request.stream.close()
48 activity.finished(request.stream.id)
49 continue
50 }
51 val launched = AtomicBoolean(false)
52 try {
53 startServerTask(requests.job, requests) {
54 ready ->
55 launched.store(true)
56 try {
57 ready.ready()
58 executeHttp2Request(state, entry, activity, request, requestId!!)
59 }
60 finally {
61 state.endRequest(entry)
62 }
63 }
64 } finally {
65 if (!launched.load()) {
66 state.endRequest(entry)
67 activity.finished(request.stream.id)
68 request.stream.close()
69 }
70 }
71 }
72 }
73 monitor.join()
74 if (present(activity.failure))
75 throw activity.failure!!
76 } catch (error: Throwable) {
77 val cause = activity.failure ?: error
78 failure = Some(cause)
79 throw cause
80 } finally {
81 monitor.cancel()
82 closeServerResource(state, connection, failure)
83 }