Decouple AbstractChannel and AbstractChannelHandlerContext

Motivation:

We do a "blind" cast to AbstractChannel in AbstractChannelHandlerContext which we should better no do. It would be better to decouble AbstractChannelHandlerContext from AbstractChannel.

Modifications:

Decouble AbstractChannelHandlerContext from AbstractChannel by move logic to DefaultChannelPipeline

Result:

Less coubling and less casting.
This commit is contained in:
Norman Maurer 2016-05-20 11:13:07 +02:00
parent 7547a448e0
commit 0838f223e1
3 changed files with 11 additions and 13 deletions

View File

@ -50,8 +50,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
NOT_YET_CONNECTED_EXCEPTION.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
}
private MessageSizeEstimator.Handle estimatorHandle;
private final Channel parent;
private final ChannelId id;
private final Unsafe unsafe;
@ -408,13 +406,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return pipeline.voidPromise();
}
final MessageSizeEstimator.Handle estimatorHandle() {
if (estimatorHandle == null) {
estimatorHandle = config().getMessageSizeEstimator().newHandle();
}
return estimatorHandle;
}
/**
* {@link Unsafe} implementation which sub-classes must extend and use.
*/
@ -800,7 +791,7 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
int size;
try {
msg = filterOutboundMessage(msg);
size = estimatorHandle().size(msg);
size = pipeline.estimatorHandle().size(msg);
if (size < 0) {
size = 0;
}

View File

@ -1011,7 +1011,7 @@ abstract class AbstractChannelHandlerContext extends DefaultAttributeMap
// Check for null as it may be set to null if the channel is closed already
if (buffer != null) {
task.size = ((AbstractChannel) ctx.channel()).estimatorHandle().size(msg) + WRITE_TASK_OVERHEAD;
task.size = ctx.pipeline.estimatorHandle().size(msg) + WRITE_TASK_OVERHEAD;
buffer.incrementPendingOutboundBytes(task.size);
} else {
task.size = 0;

View File

@ -57,16 +57,16 @@ public class DefaultChannelPipeline implements ChannelPipeline {
}
};
private final Channel channel;
final AbstractChannelHandlerContext head;
final AbstractChannelHandlerContext tail;
private final Channel channel;
private final ChannelFuture succeededFuture;
private final VoidChannelPromise voidPromise;
private final boolean touch = ResourceLeakDetector.isEnabled();
private Map<EventExecutorGroup, EventExecutor> childExecutors;
private MessageSizeEstimator.Handle estimatorHandle;
/**
* This is the head of a linked list that is processed by {@link #callHandlerAddedForAllHandlers()} and so process
@ -99,6 +99,13 @@ public class DefaultChannelPipeline implements ChannelPipeline {
tail.prev = head;
}
final MessageSizeEstimator.Handle estimatorHandle() {
if (estimatorHandle == null) {
estimatorHandle = channel.config().getMessageSizeEstimator().newHandle();
}
return estimatorHandle;
}
final Object touch(Object msg, AbstractChannelHandlerContext next) {
return touch ? ReferenceCountUtil.touch(msg, next) : msg;
}