0

As I understand keep-alive timeout is a server side property. I would like to configure keep-alive timeout in ktor. Here is the server:

embeddedServer(Netty, port = 8080, configure = {..}, ) { ... }

I see boolean tcpKeepAlive property inside Netty specific configuration. And it definition is:

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.

So for me it is not clear what does it mean configure your host accordingly.

I found this answer https://stackoverflow.com/a/29749938/3601615 which could tell how to close idle connections. But I have been supposing that it would be enough to use some single property to set keep-alive timeout, and the mentioned solution looks complicated.

I will appreciate any advices how to configure keep-alive timeout in ktor.

Ales
  • 170
  • 1
  • 4
  • 14
  • The `tcpKeepAlive` property set to `true` enables the `StandardSocketOptions. SO_KEEPALIVE` option that is described in https://docs.oracle.com/javase/7/docs/api/java/net/StandardSocketOptions.html?is-external=true#SO_KEEPALIVE – Aleksei Tirman Jun 15 '22 at 13:30
  • @AlekseiTirman, maybe you have an idea how is it possible to add custom `childHandler` in `ktor-server-netty:1.6.7`, I see that in version 2.x the special property `channelPipelineConfig` appeared – Ales Jun 15 '22 at 15:28
  • 1
    You can do it by configuring a `ServerBootstrap`. Here is an example https://gist.github.com/Stexxe/afcea94a66a0d2d1affdfc3d563438dc. – Aleksei Tirman Jun 15 '22 at 17:38
  • @AlekseiTirman thank you, but if I am correct, the default `NettyChannelInitializer` will override MyHandler https://github.com/ktorio/ktor/blob/9ca85c50e297a2c0a124f11f1c1a453909417ecf/ktor-server/ktor-server-netty/jvm/src/io/ktor/server/netty/NettyApplicationEngine.kt#L144 Maybe we should tune configuration a bit – Ales Jun 15 '22 at 20:19
  • 1
    Yes, you're right. The handler will be overwritten. Unfortunately, I don't know how to solve your problem with Ktor 1.6.*. In Ktor 2.* you can use `channelPipelineConfig` as you wrote before. – Aleksei Tirman Jun 16 '22 at 06:51
  • @AlekseiTirman am I right that `requestReadTimeoutSeconds` can be used as an alternative to keep-alive timeout? With one caveat that it may close not only idle connections but some long processing requests ? – Ales Jun 16 '22 at 08:41
  • Seems like you're right. – Aleksei Tirman Jun 16 '22 at 09:45

0 Answers0