reactor / reactor-netty

TCP/HTTP/UDP/QUIC client/server with Reactor over Netty

Home Page:https://projectreactor.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add ability to set idle-timeout for websockets

tomfi opened this issue · comments

I could not find a way to control the idle-timeout for websockets via properties / existing handlers.

The expected behaviour will be that any read, including ping, will reset the idle timeout "counter" and if the idle timeout configured was reached, close the websocket session.

By coping this class i could achive the goal, but the class is not public - https://github.com/reactor/reactor-netty/blob/main/reactor-netty-http/src/main/java/reactor/netty/http/server/IdleTimeoutHandler.java

It will be great if you can make it public or give a way to configure a http server to use it.

Example if how I am using it with spring boot webflux (note that IdleTimeoutHandler was copied so i can use it in my code):

@Configuration
class NettyReactiveWebServerFactoryCustomizer(
  private val webSocketProperties: WebSocketProperties,
) : WebServerFactoryCustomizer<NettyReactiveWebServerFactory> {
  override fun customize(factory: NettyReactiveWebServerFactory) {
    factory.addServerCustomizers(
      { httpServer ->
        httpServer.doOnConnection { connection ->
          connection.addHandlerLast(
            IdleTimeoutHandler(Duration.ofSeconds(webSocketProperties.idleTimeout).toMillis()),
          )
        }
      },
    )
  }
}

Motivation

When dealing with IOT devices, a lot of the times there are network issues and disconnects that will be "invisible" to the netty server which results in IOT devices appearing online when they are not reachable. We need the idle timeout to disconnect them fast in order to show the appropriate status to and notify admins/users that something is wrong with their devices.

I did a POC without setting the idle-timeout, and the netty stuck did not disconnect the session when a network issue like I described happend.

Desired solution

Allow setting idle-timeout for websocket or make the class i linked public so it can be used without duplicating it.

Considered alternatives

Copy the class like i did, but i prefer to avoid duplicating existing framework code

@tomfi In Reactor Netty we use IdleTimeoutHandler for the functionality that waits for the next HTTP request.

If you need just a handler that listens for read timeouts, why don't you use io.netty.handler.timeout.ReadTimeoutHandler?

@violetagg i will check if it works for my use-case, thanks :)

@violetagg it works perfectly, weird I could not find it when i searched around.

Thanks again.