Skip to content

Commit 0838f22

Browse files
committed
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.
1 parent 7547a44 commit 0838f22

3 files changed

Lines changed: 11 additions & 13 deletions

File tree

transport/src/main/java/io/netty/channel/AbstractChannel.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public abstract class AbstractChannel extends DefaultAttributeMap implements Cha
5050
NOT_YET_CONNECTED_EXCEPTION.setStackTrace(EmptyArrays.EMPTY_STACK_TRACE);
5151
}
5252

53-
private MessageSizeEstimator.Handle estimatorHandle;
54-
5553
private final Channel parent;
5654
private final ChannelId id;
5755
private final Unsafe unsafe;
@@ -408,13 +406,6 @@ public final ChannelPromise voidPromise() {
408406
return pipeline.voidPromise();
409407
}
410408

411-
final MessageSizeEstimator.Handle estimatorHandle() {
412-
if (estimatorHandle == null) {
413-
estimatorHandle = config().getMessageSizeEstimator().newHandle();
414-
}
415-
return estimatorHandle;
416-
}
417-
418409
/**
419410
* {@link Unsafe} implementation which sub-classes must extend and use.
420411
*/
@@ -800,7 +791,7 @@ public final void write(Object msg, ChannelPromise promise) {
800791
int size;
801792
try {
802793
msg = filterOutboundMessage(msg);
803-
size = estimatorHandle().size(msg);
794+
size = pipeline.estimatorHandle().size(msg);
804795
if (size < 0) {
805796
size = 0;
806797
}

transport/src/main/java/io/netty/channel/AbstractChannelHandlerContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1011,7 +1011,7 @@ protected static void init(AbstractWriteTask task, AbstractChannelHandlerContext
10111011

10121012
// Check for null as it may be set to null if the channel is closed already
10131013
if (buffer != null) {
1014-
task.size = ((AbstractChannel) ctx.channel()).estimatorHandle().size(msg) + WRITE_TASK_OVERHEAD;
1014+
task.size = ctx.pipeline.estimatorHandle().size(msg) + WRITE_TASK_OVERHEAD;
10151015
buffer.incrementPendingOutboundBytes(task.size);
10161016
} else {
10171017
task.size = 0;

transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,16 @@ protected Map<Class<?>, String> initialValue() throws Exception {
5757
}
5858
};
5959

60-
private final Channel channel;
61-
6260
final AbstractChannelHandlerContext head;
6361
final AbstractChannelHandlerContext tail;
6462

63+
private final Channel channel;
6564
private final ChannelFuture succeededFuture;
6665
private final VoidChannelPromise voidPromise;
6766
private final boolean touch = ResourceLeakDetector.isEnabled();
6867

6968
private Map<EventExecutorGroup, EventExecutor> childExecutors;
69+
private MessageSizeEstimator.Handle estimatorHandle;
7070

7171
/**
7272
* This is the head of a linked list that is processed by {@link #callHandlerAddedForAllHandlers()} and so process
@@ -99,6 +99,13 @@ protected DefaultChannelPipeline(AbstractChannel channel) {
9999
tail.prev = head;
100100
}
101101

102+
final MessageSizeEstimator.Handle estimatorHandle() {
103+
if (estimatorHandle == null) {
104+
estimatorHandle = channel.config().getMessageSizeEstimator().newHandle();
105+
}
106+
return estimatorHandle;
107+
}
108+
102109
final Object touch(Object msg, AbstractChannelHandlerContext next) {
103110
return touch ? ReferenceCountUtil.touch(msg, next) : msg;
104111
}

0 commit comments

Comments
 (0)