-
Notifications
You must be signed in to change notification settings - Fork 1k
/
NettyApplicationEngine.kt
326 lines (276 loc) · 10.8 KB
/
NettyApplicationEngine.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright 2014-2021 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
*/
package io.ktor.server.netty
import io.ktor.events.*
import io.ktor.server.application.*
import io.ktor.server.engine.*
import io.ktor.util.network.*
import io.ktor.util.pipeline.*
import io.netty.bootstrap.*
import io.netty.channel.*
import io.netty.channel.epoll.*
import io.netty.channel.kqueue.*
import io.netty.channel.nio.*
import io.netty.channel.socket.*
import io.netty.channel.socket.nio.*
import io.netty.handler.codec.http.*
import io.netty.util.concurrent.*
import kotlinx.coroutines.*
import java.lang.reflect.*
import java.net.*
import java.util.concurrent.*
import kotlin.reflect.*
/**
* [ApplicationEngine] implementation for running in a standalone Netty
*/
public class NettyApplicationEngine(
environment: ApplicationEngineEnvironment,
configure: Configuration.() -> Unit = {}
) : BaseApplicationEngine(environment) {
/**
* Configuration for the [NettyApplicationEngine]
*/
public class Configuration : BaseApplicationEngine.Configuration() {
/**
* Size of the queue to store [ApplicationCall] instances that cannot be immediately processed
*/
public var requestQueueLimit: Int = 16
/**
* Number of concurrently running requests from the same http pipeline
*/
public var runningLimit: Int = 32
/**
* Do not create separate call event group and reuse worker group for processing calls
*/
public var shareWorkGroup: Boolean = false
/**
* User-provided function to configure Netty's [ServerBootstrap]
*/
public var configureBootstrap: ServerBootstrap.() -> Unit = {}
/**
* Timeout in seconds for sending responses to client
*/
public var responseWriteTimeoutSeconds: Int = 10
/**
* Timeout in seconds for reading requests from client, "0" is infinite.
*/
public var requestReadTimeoutSeconds: Int = 0
/**
* If set to `true`, enables TCP keep alive for connections so all
* dead client connections will be discarded.
* The timeout period is configured by the system so configure
* your host accordingly.
*/
public var tcpKeepAlive: Boolean = false
/**
* The url limit including query parameters
*/
public var maxInitialLineLength: Int = HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH
/**
* The maximum length of all headers.
* If the sum of the length of each header exceeds this value, a TooLongFrameException will be raised.
*/
public var maxHeaderSize: Int = HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE
/**
* The maximum length of the content or each chunk
*/
public var maxChunkSize: Int = HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE
/**
* User-provided function to configure Netty's [HttpServerCodec]
*/
public var httpServerCodec: () -> HttpServerCodec = this::defaultHttpServerCodec
/**
* User-provided function to configure Netty's [ChannelPipeline]
*/
public var channelPipelineConfig: ChannelPipeline.() -> Unit = {}
/**
* Default function to configure Netty's
*/
private fun defaultHttpServerCodec() = HttpServerCodec(
maxInitialLineLength,
maxHeaderSize,
maxChunkSize
)
}
private val configuration = Configuration().apply(configure)
/**
* [EventLoopGroupProxy] for accepting connections
*/
private val connectionEventGroup: EventLoopGroup by lazy {
customBootstrap.config().group() ?: EventLoopGroupProxy.create(configuration.connectionGroupSize)
}
/**
* [EventLoopGroupProxy] for processing incoming requests and doing engine's internal work
*/
private val workerEventGroup: EventLoopGroup by lazy {
customBootstrap.config().childGroup()?.let {
return@lazy it
}
if (configuration.shareWorkGroup) {
EventLoopGroupProxy.create(configuration.workerGroupSize + configuration.callGroupSize)
} else {
EventLoopGroupProxy.create(configuration.workerGroupSize)
}
}
private val customBootstrap: ServerBootstrap by lazy {
ServerBootstrap().apply(configuration.configureBootstrap)
}
/**
* [EventLoopGroupProxy] for processing [ApplicationCall] instances
*/
private val callEventGroup: EventLoopGroup by lazy {
if (configuration.shareWorkGroup) {
workerEventGroup
} else {
EventLoopGroupProxy.create(configuration.callGroupSize)
}
}
private val nettyDispatcher: CoroutineDispatcher by lazy {
NettyDispatcher
}
private val workerDispatcher by lazy {
workerEventGroup.asCoroutineDispatcher()
}
private var cancellationDeferred: CompletableJob? = null
private var channels: List<Channel>? = null
internal val bootstraps: List<ServerBootstrap> by lazy {
environment.connectors.map(::createBootstrap)
}
private val userContext = environment.parentCoroutineContext +
nettyDispatcher +
NettyApplicationCallHandler.CallHandlerCoroutineName +
DefaultUncaughtExceptionHandler(environment.log)
private fun createBootstrap(connector: EngineConnectorConfig): ServerBootstrap {
return customBootstrap.clone().apply {
if (config().group() == null && config().childGroup() == null) {
group(connectionEventGroup, workerEventGroup)
}
if (config().channelFactory() == null) {
channel(getChannelClass().java)
}
childHandler(
NettyChannelInitializer(
pipeline,
environment,
callEventGroup,
workerDispatcher,
userContext,
connector,
configuration.requestQueueLimit,
configuration.runningLimit,
configuration.responseWriteTimeoutSeconds,
configuration.requestReadTimeoutSeconds,
configuration.httpServerCodec,
configuration.channelPipelineConfig
)
)
if (configuration.tcpKeepAlive) {
option(NioChannelOption.SO_KEEPALIVE, true)
}
}
}
init {
val afterCall = PipelinePhase("After")
pipeline.insertPhaseAfter(EnginePipeline.Call, afterCall)
pipeline.intercept(afterCall) {
(call as? NettyApplicationCall)?.finish()
}
}
override fun start(wait: Boolean): NettyApplicationEngine {
environment.start()
try {
channels = bootstraps.zip(environment.connectors)
.map { it.first.bind(it.second.host, it.second.port) }
.map { it.sync().channel() }
val connectors = channels!!.zip(environment.connectors)
.map { it.second.withPort(it.first.localAddress().port) }
resolvedConnectors.complete(connectors)
} catch (cause: BindException) {
terminate()
throw cause
}
environment.monitor.raiseCatching(ServerReady, environment, environment.log)
cancellationDeferred = stopServerOnCancellation()
if (wait) {
channels?.map { it.closeFuture() }?.forEach { it.sync() }
stop(1, 5, TimeUnit.SECONDS)
}
return this
}
private fun terminate() {
connectionEventGroup.shutdownGracefully().sync()
workerEventGroup.shutdownGracefully().sync()
}
override fun stop(gracePeriodMillis: Long, timeoutMillis: Long) {
cancellationDeferred?.complete()
environment.monitor.raise(ApplicationStopPreparing, environment)
val channelFutures = channels?.mapNotNull { if (it.isOpen) it.close() else null }.orEmpty()
try {
val shutdownConnections =
connectionEventGroup.shutdownGracefully(gracePeriodMillis, timeoutMillis, TimeUnit.MILLISECONDS)
shutdownConnections.await()
val shutdownWorkers =
workerEventGroup.shutdownGracefully(gracePeriodMillis, timeoutMillis, TimeUnit.MILLISECONDS)
if (configuration.shareWorkGroup) {
shutdownWorkers.await()
} else {
val shutdownCall =
callEventGroup.shutdownGracefully(gracePeriodMillis, timeoutMillis, TimeUnit.MILLISECONDS)
shutdownWorkers.await()
shutdownCall.await()
}
environment.stop()
} finally {
channelFutures.forEach { it.sync() }
}
}
override fun toString(): String {
return "Netty($environment)"
}
}
/**
* Transparently allows for the creation of [EventLoopGroup]'s utilising the optimal implementation for
* a given operating system, subject to availability, or falling back to [NioEventLoopGroup] if none is available.
*/
public class EventLoopGroupProxy(
public val channel: KClass<out ServerSocketChannel>,
group: EventLoopGroup
) : EventLoopGroup by group {
public companion object {
public fun create(parallelism: Int): EventLoopGroupProxy {
val defaultFactory = DefaultThreadFactory(EventLoopGroupProxy::class.java, true)
val factory = ThreadFactory { runnable ->
defaultFactory.newThread {
markParkingProhibited()
runnable.run()
}
}
val channelClass = getChannelClass()
return when {
KQueue.isAvailable() -> EventLoopGroupProxy(channelClass, KQueueEventLoopGroup(parallelism, factory))
Epoll.isAvailable() -> EventLoopGroupProxy(channelClass, EpollEventLoopGroup(parallelism, factory))
else -> EventLoopGroupProxy(channelClass, NioEventLoopGroup(parallelism, factory))
}
}
private val prohibitParkingFunction: Method? by lazy {
try {
Class.forName("io.ktor.utils.io.jvm.javaio.PollersKt")
.getMethod("prohibitParking")
} catch (cause: Throwable) {
null
}
}
private fun markParkingProhibited() {
try {
prohibitParkingFunction?.invoke(null)
} catch (_: Throwable) {
}
}
}
}
private fun getChannelClass(): KClass<out ServerSocketChannel> = when {
KQueue.isAvailable() -> KQueueServerSocketChannel::class
Epoll.isAvailable() -> EpollServerSocketChannel::class
else -> NioServerSocketChannel::class
}