scoop:scoop.http.server:0.1.0
src/server_http2.scoop
Selected production source · src/server_http2.scoop
1
package scoop.http.server3
import scoop.asyncio.*4
import scoop.http.*5
import scoop.http.asyncio.*6
import scoop.time.TimeMark8
internal suspend fun <T> serveHttp2(9
state: ServerState,10
entry: ServerConnection,11
transport: T,12
scope: CoroutineScope,13
started: TimeMark14
) where T: AsyncByteStream, T: AsyncCloseable {15
val options = state.options16
val activity = ServerHttp2(options, started)17
val draining =18
state.lock.withLock {19
entry.http2 = Some(activity);20
state.phase != ServerPhase.Running21
}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? = None35
val monitor = scope.launch suspend { watchServerHttp2(activity, connection, scope.job) }36
try {37
coroutineScope<Unit> {38
val requests = this40
while (true) {41
val incoming = connection.acceptRequest()42
if (!present(incoming))43
break44
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
continue50
}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 ?: error78
failure = Some(cause)79
throw cause80
} finally {81
monitor.cancel()82
closeServerResource(state, connection, failure)83
}84
}