Skip to content

Commit

Permalink
Remove code duplication in ChunkedWriteHandler
Browse files Browse the repository at this point in the history
Motivation:

We had some code duplication in ChunkedWriteHandler.

Modifications:

Factor out duplicated code into private methods and reuse it.

Result:

Less code duplication.
  • Loading branch information
normanmaurer committed Mar 19, 2018
1 parent 2c90b62 commit 352e36a
Showing 1 changed file with 15 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,31 +102,29 @@ public void resumeTransfer() {
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() {
try {
doFlush(ctx);
} catch (Exception e) {
if (logger.isWarnEnabled()) {
logger.warn("Unexpected exception while sending chunks.", e);
}
}
resumeTransfer0(ctx);
}
});
}
}

private void resumeTransfer0(ChannelHandlerContext ctx) {
try {
doFlush(ctx);
} catch (Exception e) {
if (logger.isWarnEnabled()) {
logger.warn("Unexpected exception while sending chunks.", e);
}
}
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
queue.add(new PendingWrite(msg, promise));
Expand Down Expand Up @@ -192,7 +190,7 @@ private void discard(Throwable cause) {
}
}

private void doFlush(final ChannelHandlerContext ctx) throws Exception {
private void doFlush(final ChannelHandlerContext ctx) {
final Channel channel = ctx.channel();
if (!channel.isActive()) {
discard(null);
Expand Down Expand Up @@ -317,7 +315,7 @@ public void operationComplete(ChannelFuture future) throws Exception {
}
}

static void closeInput(ChunkedInput<?> chunks) {
private static void closeInput(ChunkedInput<?> chunks) {
try {
chunks.close();
} catch (Throwable t) {
Expand Down Expand Up @@ -346,12 +344,7 @@ void success(long total) {
// 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();
}

Expand Down

0 comments on commit 352e36a

Please sign in to comment.