Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Also send ping when no message has been read for the keep alive time #357

Merged
merged 3 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class MqttConnectHandler extends MqttTimeoutInboundHandler {
private final @NotNull MqttDisconnectOnConnAckHandler disconnectOnConnAckHandler;

private boolean connectCalled = false;
private long connectFlushTime;

@Inject
MqttConnectHandler(
Expand Down Expand Up @@ -122,6 +123,7 @@ public void handlerAdded(final @NotNull ChannelHandlerContext ctx) {
* @param ctx the channel handler context.
*/
private void writeConnect(final @NotNull ChannelHandlerContext ctx) {
connectFlushTime = System.nanoTime();
ctx.writeAndFlush((connect.getRawEnhancedAuthMechanism() == null) ?
connect.createStateful(clientConfig.getRawClientIdentifier(), null) : connect).addListener(this);
}
Expand Down Expand Up @@ -173,7 +175,8 @@ private void readConnAck(final @NotNull MqttConnAck connAck, final @NotNull Chan

final int keepAlive = connectionConfig.getKeepAlive();
if (keepAlive > 0) {
channel.pipeline().addAfter(MqttDecoder.NAME, MqttPingHandler.NAME, new MqttPingHandler(keepAlive));
final MqttPingHandler pingHandler = new MqttPingHandler(keepAlive, connectFlushTime, System.nanoTime());
channel.pipeline().addAfter(MqttDecoder.NAME, MqttPingHandler.NAME, pingHandler);
}

clientConfig.getRawState().set(MqttClientState.CONNECTED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,22 @@ public class MqttPingHandler extends MqttConnectionAwareHandler

private final long keepAliveNanos;
private long lastFlushTimeNanos;
private long lastReadTimeNanos;
private boolean pingReqWritten;
private boolean pingReqFlushed;
private boolean messageRead;
private @Nullable ScheduledFuture<?> timeoutFuture;

public MqttPingHandler(final int keepAlive) {
public MqttPingHandler(final int keepAlive, final long lastFlushTimeNanos, final long lastReadTimeNanos) {
keepAliveNanos = TimeUnit.SECONDS.toNanos(keepAlive) - TimeUnit.MILLISECONDS.toNanos(100);
this.lastFlushTimeNanos = lastFlushTimeNanos;
this.lastReadTimeNanos = lastReadTimeNanos;
}

@Override
public void handlerAdded(final @NotNull ChannelHandlerContext ctx) {
super.handlerAdded(ctx);
lastFlushTimeNanos = System.nanoTime();
schedule(ctx, keepAliveNanos);
schedule(ctx, nextDelay(System.nanoTime()));
}

@Override
Expand All @@ -75,6 +77,7 @@ public void flush(final @NotNull ChannelHandlerContext ctx) {

@Override
public void channelRead(final @NotNull ChannelHandlerContext ctx, final @NotNull Object msg) {
lastReadTimeNanos = System.nanoTime();
if (msg instanceof MqttPingResp) {
messageRead = true;
} else {
Expand All @@ -87,6 +90,10 @@ private void schedule(final @NotNull ChannelHandlerContext ctx, final long delay
timeoutFuture = ctx.executor().schedule(this, delayNanos, TimeUnit.NANOSECONDS);
}

private long nextDelay(final long timeNanos) {
return keepAliveNanos - (timeNanos - Math.min(lastReadTimeNanos, lastFlushTimeNanos));
}

@Override
public void run() {
if (ctx == null) {
Expand All @@ -104,13 +111,15 @@ public void run() {
}
pingReqFlushed = false;
messageRead = false;
final long nextDelayNanos = keepAliveNanos - (System.nanoTime() - lastFlushTimeNanos);
final long timeNanos = System.nanoTime();
final long nextDelayNanos = nextDelay(timeNanos);
if (nextDelayNanos > 1_000) {
pingReqWritten = false;
schedule(ctx, nextDelayNanos);
} else {
pingReqWritten = true;
schedule(ctx, keepAliveNanos);
lastFlushTimeNanos = timeNanos;
ctx.writeAndFlush(MqttPingReq.INSTANCE).addListener(this);
}
}
Expand Down