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