broker/AutoFlushHandler: code formatter used
This commit is contained in:
@@ -13,33 +13,31 @@
|
|||||||
*
|
*
|
||||||
* You may elect to redistribute this code under either of these licenses.
|
* You may elect to redistribute this code under either of these licenses.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package io.moquette.server.netty;
|
package io.moquette.server.netty;
|
||||||
|
|
||||||
import io.netty.channel.ChannelDuplexHandler;
|
import io.netty.channel.ChannelDuplexHandler;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import io.netty.util.concurrent.EventExecutor;
|
import io.netty.util.concurrent.EventExecutor;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.concurrent.ScheduledFuture;
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Auto-flush data on channel after a read timeout.
|
* Auto-flush data on channel after a read timeout. It's inspired by IdleStateHandler but it's
|
||||||
* It's inspired by IdleStateHandler but it's specialized version, just flushing data after
|
* specialized version, just flushing data after no read is done on the channel after a period. It's
|
||||||
* no read is done on the channel after a period. It's used to avoid aggressively flushing from
|
* used to avoid aggressively flushing from the ProtocolProcessor.
|
||||||
* the ProtocolProcessor.
|
|
||||||
*/
|
*/
|
||||||
public class AutoFlushHandler extends ChannelDuplexHandler {
|
public class AutoFlushHandler extends ChannelDuplexHandler {
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(AutoFlushHandler.class);
|
private static final Logger LOG = LoggerFactory.getLogger(AutoFlushHandler.class);
|
||||||
private static final long MIN_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(1);
|
private static final long MIN_TIMEOUT_NANOS = TimeUnit.MILLISECONDS.toNanos(1);
|
||||||
|
|
||||||
private final long writerIdleTimeNanos;
|
private final long writerIdleTimeNanos;
|
||||||
volatile ScheduledFuture<?> writerIdleTimeout;
|
volatile ScheduledFuture<?> writerIdleTimeout;
|
||||||
volatile long lastWriteTime;
|
volatile long lastWriteTime;
|
||||||
// private boolean firstWriterIdleEvent = true;
|
// private boolean firstWriterIdleEvent = true;
|
||||||
|
|
||||||
private volatile int state; // 0 - none, 1 - initialized, 2 - destroyed
|
private volatile int state; // 0 - none, 1 - initialized, 2 - destroyed
|
||||||
|
|
||||||
@@ -57,7 +55,7 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
|||||||
// not be invoked. We have to initialize here instead.
|
// not be invoked. We have to initialize here instead.
|
||||||
initialize(ctx);
|
initialize(ctx);
|
||||||
} else {
|
} else {
|
||||||
// channelActive() event has not been fired yet. this.channelActive() will be invoked
|
// channelActive() event has not been fired yet. this.channelActive() will be invoked
|
||||||
// and initialization will occur there.
|
// and initialization will occur there.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,7 +77,7 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
|||||||
@Override
|
@Override
|
||||||
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
public void channelActive(ChannelHandlerContext ctx) throws Exception {
|
||||||
// This method will be invoked only if this handler was added
|
// This method will be invoked only if this handler was added
|
||||||
// before channelActive() event is fired. If a user adds this handler
|
// before channelActive() event is fired. If a user adds this handler
|
||||||
// after the channelActive() event, initialize() will be called by beforeAdd().
|
// after the channelActive() event, initialize() will be called by beforeAdd().
|
||||||
initialize(ctx);
|
initialize(ctx);
|
||||||
super.channelActive(ctx);
|
super.channelActive(ctx);
|
||||||
@@ -91,30 +89,30 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
|||||||
super.channelInactive(ctx);
|
super.channelInactive(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Override
|
// @Override
|
||||||
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
// public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
|
||||||
// if (writerrIdleTimeNanos > 0) {
|
// if (writerrIdleTimeNanos > 0) {
|
||||||
// reading = true;
|
// reading = true;
|
||||||
// firstReaderIdleEvent = true;
|
// firstReaderIdleEvent = true;
|
||||||
// }
|
// }
|
||||||
// ctx.fireChannelRead(msg);
|
// ctx.fireChannelRead(msg);
|
||||||
// }
|
// }
|
||||||
//
|
//
|
||||||
// @Override
|
// @Override
|
||||||
// public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
// public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
|
||||||
// if (writerrIdleTimeNanos > 0) {
|
// if (writerrIdleTimeNanos > 0) {
|
||||||
// lastReadTime = System.nanoTime();
|
// lastReadTime = System.nanoTime();
|
||||||
// reading = false;
|
// reading = false;
|
||||||
// }
|
// }
|
||||||
// ctx.fireChannelReadComplete();
|
// ctx.fireChannelReadComplete();
|
||||||
// }
|
// }
|
||||||
|
|
||||||
private void initialize(ChannelHandlerContext ctx) {
|
private void initialize(ChannelHandlerContext ctx) {
|
||||||
// Avoid the case where destroy() is called before scheduling timeouts.
|
// Avoid the case where destroy() is called before scheduling timeouts.
|
||||||
// See: https://github.com/netty/netty/issues/143
|
// See: https://github.com/netty/netty/issues/143
|
||||||
if (LOG.isDebugEnabled()) {
|
if (LOG.isDebugEnabled()) {
|
||||||
LOG.debug("Initializing autoflush handler. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
LOG.debug("Initializing autoflush handler. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
||||||
}
|
}
|
||||||
switch (state) {
|
switch (state) {
|
||||||
case 1:
|
case 1:
|
||||||
case 2:
|
case 2:
|
||||||
@@ -126,9 +124,7 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
|||||||
EventExecutor loop = ctx.executor();
|
EventExecutor loop = ctx.executor();
|
||||||
|
|
||||||
lastWriteTime = System.nanoTime();
|
lastWriteTime = System.nanoTime();
|
||||||
writerIdleTimeout = loop.schedule(
|
writerIdleTimeout = loop.schedule(new WriterIdleTimeoutTask(ctx), writerIdleTimeNanos, TimeUnit.NANOSECONDS);
|
||||||
new WriterIdleTimeoutTask(ctx),
|
|
||||||
writerIdleTimeNanos, TimeUnit.NANOSECONDS);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void destroy() {
|
private void destroy() {
|
||||||
@@ -143,14 +139,16 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
|||||||
/**
|
/**
|
||||||
* Is called when the write timeout expire.
|
* Is called when the write timeout expire.
|
||||||
*
|
*
|
||||||
* @param ctx the channel context.
|
* @param ctx
|
||||||
* @throws Exception in case of any IO error.
|
* the channel context.
|
||||||
|
* @throws Exception
|
||||||
|
* in case of any IO error.
|
||||||
*/
|
*/
|
||||||
protected void channelIdle(ChannelHandlerContext ctx/*, IdleStateEvent evt*/) throws Exception {
|
protected void channelIdle(ChannelHandlerContext ctx/* , IdleStateEvent evt */) throws Exception {
|
||||||
// ctx.fireUserEventTriggered(evt);
|
// ctx.fireUserEventTriggered(evt);
|
||||||
if (LOG.isDebugEnabled()) {
|
if (LOG.isDebugEnabled()) {
|
||||||
LOG.debug("Flushing idle Netty channel. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
LOG.debug("Flushing idle Netty channel. MqttClientId = {}.", NettyUtils.clientID(ctx.channel()));
|
||||||
}
|
}
|
||||||
ctx.channel().flush();
|
ctx.channel().flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,22 +166,19 @@ public class AutoFlushHandler extends ChannelDuplexHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// long lastWriteTime = IdleStateHandler.this.lastWriteTime;
|
// long lastWriteTime = IdleStateHandler.this.lastWriteTime;
|
||||||
// long lastWriteTime = lastWriteTime;
|
// long lastWriteTime = lastWriteTime;
|
||||||
long nextDelay = writerIdleTimeNanos - (System.nanoTime() - lastWriteTime);
|
long nextDelay = writerIdleTimeNanos - (System.nanoTime() - lastWriteTime);
|
||||||
if (nextDelay <= 0) {
|
if (nextDelay <= 0) {
|
||||||
// Writer is idle - set a new timeout and notify the callback.
|
// Writer is idle - set a new timeout and notify the callback.
|
||||||
writerIdleTimeout = ctx.executor().schedule(
|
writerIdleTimeout = ctx.executor().schedule(this, writerIdleTimeNanos, TimeUnit.NANOSECONDS);
|
||||||
this, writerIdleTimeNanos, TimeUnit.NANOSECONDS);
|
|
||||||
try {
|
try {
|
||||||
/*IdleStateEvent event;
|
/*
|
||||||
if (firstWriterIdleEvent) {
|
* IdleStateEvent event; if (firstWriterIdleEvent) { firstWriterIdleEvent =
|
||||||
firstWriterIdleEvent = false;
|
* false; event = IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT; } else { event =
|
||||||
event = IdleStateEvent.FIRST_WRITER_IDLE_STATE_EVENT;
|
* IdleStateEvent.WRITER_IDLE_STATE_EVENT; }
|
||||||
} else {
|
*/
|
||||||
event = IdleStateEvent.WRITER_IDLE_STATE_EVENT;
|
channelIdle(ctx/* , event */);
|
||||||
}*/
|
|
||||||
channelIdle(ctx/*, event*/);
|
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
ctx.fireExceptionCaught(t);
|
ctx.fireExceptionCaught(t);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user