Reactor Netty WebClient 不活动后如何关闭 TCP 连接?
How to close TCP connection after some inactivity in Reactor Netty WebClient?
我想在闲置一段时间后关闭 TCP 连接。
不活动是指 TCP 连接在一定时间内未被使用。
我认为我会为此目的使用连接处理程序。
private val webClient = webClientBuilder
.build()
.mutate()
.filters(MutableList<ExchangeFilterFunction>::clear)
.clientConnector(
ReactorClientHttpConnector(
HttpClient.from(
TcpClient.create()
.option(CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT)
.doOnConnected { connection ->
connection
.addHandlerLast(
IdleStateHandler(
SOCKET_INACTIVITY_TIMEOUT,
SOCKET_INACTIVITY_TIMEOUT,
0
)
)
.addHandlerLast(CloseIdleConnectionHandler())
.addHandlerLast(ReadTimeoutHandler(READ_TIMEOUT))
.addHandlerLast(WriteTimeoutHandler(WRITE_TIMEOUT))
}
)
)
)
.build()
companion object {
const val SOCKET_INACTIVITY_TIMEOUT = 20
const val CONNECT_TIMEOUT = 5_000
const val READ_TIMEOUT = 5
const val WRITE_TIMEOUT = 5
}
private class CloseIdleConnectionHandler : ChannelDuplexHandler() {
override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any) {
if (evt is IdleStateEvent) {
ctx.disconnect()
}
}
}
问题是 CloseIdleConnectionHandler
在 SOCKET_INACTIVITY_TIMEOUT
之后没有被调用。
我是做错了什么还是误用了IdleStateHandler
?
您可以使用 ConnectionProvider.builder
配置这些设置
ConnectionProvider connectionProvider =
ConnectionProvider.builder("my-connection-pool")
.maxConnections(100)
.pendingAcquireTimeout(Duration.ofMillis(100))
// this is the setting you're after
.maxIdleTime(Duration.ofMillis(1000))
.maxLifeTime(Duration.ofMillis(5000))
.build()
然后您将在构造您的 tcp 客户端时传入您的连接提供程序
TcpClient.create(connectionProvider)
.options(..... // other custom configuration
我想在闲置一段时间后关闭 TCP 连接。 不活动是指 TCP 连接在一定时间内未被使用。
我认为我会为此目的使用连接处理程序。
private val webClient = webClientBuilder
.build()
.mutate()
.filters(MutableList<ExchangeFilterFunction>::clear)
.clientConnector(
ReactorClientHttpConnector(
HttpClient.from(
TcpClient.create()
.option(CONNECT_TIMEOUT_MILLIS, CONNECT_TIMEOUT)
.doOnConnected { connection ->
connection
.addHandlerLast(
IdleStateHandler(
SOCKET_INACTIVITY_TIMEOUT,
SOCKET_INACTIVITY_TIMEOUT,
0
)
)
.addHandlerLast(CloseIdleConnectionHandler())
.addHandlerLast(ReadTimeoutHandler(READ_TIMEOUT))
.addHandlerLast(WriteTimeoutHandler(WRITE_TIMEOUT))
}
)
)
)
.build()
companion object {
const val SOCKET_INACTIVITY_TIMEOUT = 20
const val CONNECT_TIMEOUT = 5_000
const val READ_TIMEOUT = 5
const val WRITE_TIMEOUT = 5
}
private class CloseIdleConnectionHandler : ChannelDuplexHandler() {
override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any) {
if (evt is IdleStateEvent) {
ctx.disconnect()
}
}
}
问题是 CloseIdleConnectionHandler
在 SOCKET_INACTIVITY_TIMEOUT
之后没有被调用。
我是做错了什么还是误用了IdleStateHandler
?
您可以使用 ConnectionProvider.builder
配置这些设置
ConnectionProvider connectionProvider =
ConnectionProvider.builder("my-connection-pool")
.maxConnections(100)
.pendingAcquireTimeout(Duration.ofMillis(100))
// this is the setting you're after
.maxIdleTime(Duration.ofMillis(1000))
.maxLifeTime(Duration.ofMillis(5000))
.build()
然后您将在构造您的 tcp 客户端时传入您的连接提供程序
TcpClient.create(connectionProvider)
.options(..... // other custom configuration