Do not call System.nanoTime() in ReadTimeoutHandler.channelRead()

Motivation:

We mitigate callouts to System.nanoTime() in SingleThreadEventExecutor
as it is 'relatively expensive'. On a modern system, tak translates to
about 20ns per call. With channelReadComplete() we can side-step this in
channelRead().

Modifications:

Introduce a boolean flag, which indicates that a read batch is currently
on-going, which acts as a flush guard for lastReadTime. Update
lastReadTime in channelReadComplete() just before setting the flag to
false. We set the flag to true in channelRead().

The periodic task examines the flag, and if it observes it to be true,
it will reschedule the task for the full duration. If it observes as
false, it will read lastReadTime and adjust the delay accordingly.

Result:

ReadTimeoutHandler calls System.nanoTime() only once per read batch.
This commit is contained in:
Robert Varga 2015-05-20 15:02:10 +02:00 committed by Norman Maurer
parent 271af7c624
commit f3dcad3230

View File

@ -66,11 +66,13 @@ public class ReadTimeoutHandler extends ChannelInboundHandlerAdapter {
private final long timeoutNanos;
private long lastReadTime;
private volatile ScheduledFuture<?> timeout;
private volatile long lastReadTime;
private volatile int state; // 0 - none, 1 - Initialized, 2 - Destroyed;
private volatile boolean reading;
private boolean closed;
/**
@ -146,10 +148,17 @@ public class ReadTimeoutHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
lastReadTime = System.nanoTime();
reading = true;
ctx.fireChannelRead(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
lastReadTime = System.nanoTime();
reading = false;
ctx.fireChannelReadComplete();
}
private void initialize(ChannelHandlerContext ctx) {
// Avoid the case where destroy() is called before scheduling timeouts.
// See: https://github.com/netty/netty/issues/143
@ -204,7 +213,11 @@ public class ReadTimeoutHandler extends ChannelInboundHandlerAdapter {
}
long currentTime = System.nanoTime();
long nextDelay = timeoutNanos - (currentTime - lastReadTime);
long nextDelay = timeoutNanos;
if (!reading) {
nextDelay -= currentTime - lastReadTime;
}
if (nextDelay <= 0) {
// Read timed out - set a new timeout and notify the callback.
timeout = ctx.executor().schedule(this, timeoutNanos, TimeUnit.NANOSECONDS);