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 f2577f7361
commit 35f5f509a1
3 changed files with 11 additions and 13 deletions

View File

@ -51,8 +51,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 long hashCode = ThreadLocalRandom.current().nextLong();
private final Unsafe unsafe;
@ -364,13 +362,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
return voidPromise;
}
final MessageSizeEstimator.Handle estimatorHandle() {
if (estimatorHandle == null) {
estimatorHandle = config().getMessageSizeEstimator().newHandle();
}
return estimatorHandle;
}
/**
* {@link Unsafe} implementation which sub-classes must extend and use.
*/
@ -747,7 +738,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

@ -984,7 +984,7 @@ abstract class AbstractChannelHandlerContext extends DefaultAttributeMap impleme
// 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

@ -56,12 +56,12 @@ public class DefaultChannelPipeline implements ChannelPipeline {
}
};
private final Channel channel;
final AbstractChannelHandlerContext head;
final AbstractChannelHandlerContext tail;
private final Channel channel;
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
@ -92,6 +92,13 @@ public class DefaultChannelPipeline implements ChannelPipeline {
tail.prev = head;
}
final MessageSizeEstimator.Handle estimatorHandle() {
if (estimatorHandle == null) {
estimatorHandle = channel.config().getMessageSizeEstimator().newHandle();
}
return estimatorHandle;
}
private AbstractChannelHandlerContext newContext(EventExecutorGroup group, String name, ChannelHandler handler) {
return new DefaultChannelHandlerContext(this, childExecutor(group), name, handler);
}