Remove code duplication in ChunkedWriteHandler

Motivation:

We had some code duplication in ChunkedWriteHandler.

Modifications:

Factor out duplicated code into private methods and reuse it.

Result:

Less code duplication.
This commit is contained in:
Norman Maurer 2018-03-18 13:42:00 +01:00 committed by Norman Maurer
parent 2c90b6235d
commit 352e36a179

View File

@ -102,19 +102,20 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler {
return;
}
if (ctx.executor().inEventLoop()) {
try {
doFlush(ctx);
} catch (Exception e) {
if (logger.isWarnEnabled()) {
logger.warn("Unexpected exception while sending chunks.", e);
}
}
resumeTransfer0(ctx);
} else {
// let the transfer resume on the next event loop round
ctx.executor().execute(new Runnable() {
@Override
public void run() {
resumeTransfer0(ctx);
}
});
}
}
private void resumeTransfer0(ChannelHandlerContext ctx) {
try {
doFlush(ctx);
} catch (Exception e) {
@ -123,9 +124,6 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler {
}
}
}
});
}
}
@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
@ -192,7 +190,7 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler {
}
}
private void doFlush(final ChannelHandlerContext ctx) throws Exception {
private void doFlush(final ChannelHandlerContext ctx) {
final Channel channel = ctx.channel();
if (!channel.isActive()) {
discard(null);
@ -317,7 +315,7 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler {
}
}
static void closeInput(ChunkedInput<?> chunks) {
private static void closeInput(ChunkedInput<?> chunks) {
try {
chunks.close();
} catch (Throwable t) {
@ -346,12 +344,7 @@ public class ChunkedWriteHandler extends ChannelDuplexHandler {
// No need to notify the progress or fulfill the promise because it's done already.
return;
}
if (promise instanceof ChannelProgressivePromise) {
// Now we know what the total is.
((ChannelProgressivePromise) promise).tryProgress(total, total);
}
progress(total, total);
promise.trySuccess();
}