网站建设中最重要的环节是什么,东莞网站网络,做直播网站软件有哪些软件,电商网站设计网络服务连接闲置网络连接的闲置指的是当前网络连接处于空闲状态#xff0c;即没有正在进行的数据传输或通信活动。当我们的某个连接不再发送请求或者接收响应的时候#xff0c;这个连接就开始处于闲置状态。网络连接的闲置时间越长#xff0c;说明该连接越不活跃。此时#xff0c;…连接闲置网络连接的闲置指的是当前网络连接处于空闲状态即没有正在进行的数据传输或通信活动。当我们的某个连接不再发送请求或者接收响应的时候这个连接就开始处于闲置状态。网络连接的闲置时间越长说明该连接越不活跃。此时可以根据不同的场景采取不同行为比如关闭连接发送心跳长连接场景下要求定时发送心跳保持连接状态或者是探活Netty连接闲置handlerNetty默认提供了io.netty.handler.timeout.IdleStateHandler管理连接闲置事件它可以检测连接空闲时间当连接在指定时间内没有读或者写操作时就会触发一个IdleStateEvent事件IdleStateHandler主要提供有三个参数设置为0表示禁用readerIdleTimeSeconds读空闲时间即当服务器指定时间没有数据读取会触发一个读闲置事件。writerIdleTimeSeconds写空闲时间即当服务器指定时间没有数据发送或写入动作参数不同阶段不一样)会触发一个写闲置事件。allIdleTimeSeconds读/写空闲时间客户端连接在指定时间内没有读/写操作时就会触发一个IdleStateEvent的All事件。 public IdleStateHandler(int readerIdleTimeSeconds,int writerIdleTimeSeconds,int allIdleTimeSeconds) {this(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds,TimeUnit.SECONDS);}读闲置Netty的ReadTimeoutHandler是一个针对读闲置处理的handler用于处理读取超时事件。当客户端或服务器在指定时间内没有收到数据时这个类会触发一个读取超时事件。在Netty中使用IdleStateHandler来管理读取超时和写入超时。ReadTimeoutHandler继承了IdleStateHandler并在其基础上实现了定制化的读取超时功能。当读取超时发生时ReadTimeoutHandler会调用其channelRead方法其中实现了具体的读取超时逻辑。ReadTimeoutHandler在达到闲置时间的时候会抛出一个读超时异常并关闭连接 protected void readTimedOut(ChannelHandlerContext ctx) throws Exception {if (!closed) {ctx.fireExceptionCaught(ReadTimeoutException.INSTANCE);ctx.close();closed true;}}我一般把ReadTimeoutHandler用在服务端当某个客户端的连接闲置太长时间就关闭这个客户端的连接释放资源。一般情况下IdleStateHandler都是作为ChannelPipeline中的第一个Handler用于处理连接的心跳检测。当连接超时时会触发IdleStateEvent事件然后交给下一个handler处理该事件所以ReadTimeoutHandler也是这样示例如下 Overridepublic void initChannel(SocketChannel ch) throws Exception {ChannelPipeline pipeline ch.pipeline();pipeline.addLast(new ReadTimeoutHandler(READ_TIMEOUT_SECONDS));pipeline.addLast(new ServerHandler());}写闲置写闲置我一般用在客户端比如达到闲置时间关闭与服务端的连接或者发送心跳给服务端以保持长连接。写闲置则关闭示例如下public class ClientIdleHandler extends IdleStateHandler {private static final long IDLE_TIMEOUT 30000;public ClientIdleHandler() {super(true, 0, IDLE_TIMEOUT, 0, TimeUnit.MILLISECONDS);}Overrideprotected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {if (evt IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT) {// 连接闲置关闭连接ctx.close();}}
}或者也可以不关闭发送一个心跳包但是要注意这种情况客户端的闲置时间要少于服务端的。我们也可以增加一个定时任务来发送心跳包 // 重写handlerAdded方法添加一个定时任务Overridepublic void handlerAdded(ChannelHandlerContext ctx) throws Exception {ctx.executor().scheduleAtFixedRate(() - {// 判断连接是否处于活动状态if (ctx.channel().isActive()) { // 发送心跳包ctx.writeAndFlush(new Heartbeat()); }}, 0, idleTimeSeconds, TimeUnit.SECONDS);}读写闲置上面的读闲置时间和写闲置时间都达到了才触发两者取最大值 long nextDelay allIdleTimeNanos;if (!reading) {nextDelay - ticksInNanos() - Math.max(lastReadTime, lastWriteTime);}写意图 public IdleStateHandler(boolean observeOutput,long readerIdleTime, long writerIdleTime, long allIdleTime,TimeUnit unit) 在评估写闲置的时候还有一个参数observeOutput。非写闲置判定的时候是以数据发送出去为准还是有写的动作为准默认该值是false数据发送判定为写闲置而设置为true就是写动作发生时比如写入缓存但是还没发送就是非闲置。依据观测当前缓存的数据变化情况和进度来判断。如下和上次进行比对 if (buf ! null) {int messageHashCode System.identityHashCode(buf.current());long pendingWriteBytes buf.totalPendingWriteBytes();if (messageHashCode ! lastMessageHashCode || pendingWriteBytes ! lastPendingWriteBytes) {lastMessageHashCode messageHashCode;lastPendingWriteBytes pendingWriteBytes;if (!first) {return true;}}long flushProgress buf.currentProgress();if (flushProgress ! lastFlushProgress) {lastFlushProgress flushProgress;if (!first) {return true;}}}}误区Netty提供了读闲置处理的ReadTimeoutHandler是不是提供的也有写闲置的WriteTimeoutHandler。明确说明没有写闲置的WriteTimeoutHandler但确实存在WriteTimeoutHandler该hander表示的是写数据动作的超时handler却不是表示连接写闲置的handler当在指定时间内数据没写完会抛出一个写超时异常可以看下源码 public void run() {// Was not written yet so issue a write timeout// The promise itself will be failed with a ClosedChannelException once the close() was issued// See https://github.com/netty/netty/issues/2159if (!promise.isDone()) {try {writeTimedOut(ctx);} catch (Throwable t) {ctx.fireExceptionCaught(t);}}removeWriteTimeoutTask(this);}protected void writeTimedOut(ChannelHandlerContext ctx) throws Exception {if (!closed) {ctx.fireExceptionCaught(WriteTimeoutException.INSTANCE);ctx.close();closed true;}}