Skip to content

Commit

Permalink
Remove unnecessary traversal of pipeline which occurs when a Channel …
Browse files Browse the repository at this point in the history
…is closed

- Remove unnecessary ascending traversal of pipeline in DefaultChannelHandlerContext.freeInbound()
- Move DefaultChannelHandlerContext.teardownAll() to DefaultChannelPipeline
  • Loading branch information
trustin committed Jul 31, 2013
1 parent 3b1881b commit 81de227
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,39 +82,8 @@ boolean isRunning() {
return callDepth != 0;
}

void freeInbound() {
EventExecutor executor = executor();
if (executor.inEventLoop()) {
freeInbound0();
} else {
executor.execute(new Runnable() {
@Override
public void run() {
freeInbound0();
}
});
}
}

private void freeInbound0() {
if (next != null) {
DefaultChannelHandlerContext nextCtx = findContextInbound();
nextCtx.freeInbound();
} else {
// Freed all inbound buffers. Remove all handlers from the pipeline one by one from tail (exclusive)
// to head (inclusive) to trigger handlerRemoved(). If the removed handler has an outbound buffer, free it,
// too. Note that the tail handler is excluded because it's neither an outbound buffer and it doesn't
// do anything in handlerRemoved().
pipeline.tail.prev.teardown();
}
}

void teardownAll() {
pipeline.tail.prev.teardown();
}

/** Invocation initiated by {@link #freeInbound0()} after freeing all inbound buffers. */
private void teardown() {
/** Invocation initiated by {@link DefaultChannelPipeline#teardownAll()}}. */
void teardown() {
EventExecutor executor = executor();
if (executor.inEventLoop()) {
teardown0();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,13 +737,22 @@ public ChannelPipeline fireChannelRegistered() {
public ChannelPipeline fireChannelUnregistered() {
head.fireChannelUnregistered();

// Free all buffers if channel is closed and unregistered.
// Remove all handlers sequentially if channel is closed and unregistered.
if (!channel.isOpen()) {
head.freeInbound();
teardownAll();
}
return this;
}

/**
* Removes all handlers from the pipeline one by one from tail (exclusive) to head (inclusive) to trigger
* handlerRemoved(). Note that the tail handler is excluded because it's neither an outbound handler nor it
* does anything in handlerRemoved().
*/
private void teardownAll() {
tail.prev.teardown();
}

@Override
public ChannelPipeline fireChannelActive() {
head.fireChannelActive();
Expand Down

0 comments on commit 81de227

Please sign in to comment.