diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java
index 36a9195af9..a91c54efa9 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java
@@ -104,21 +104,11 @@ public final class HttpClientCodec
decoder().discardInboundReadBytes(ctx);
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- decoder().freeInboundBuffer(ctx);
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return encoder().newOutboundBuffer(ctx);
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- encoder().freeOutboundBuffer(ctx);
- }
-
private final class Encoder extends HttpRequestEncoder {
@Override
diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java
index a724f0a4d9..2018362ba4 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpServerCodec.java
@@ -67,18 +67,8 @@ public final class HttpServerCodec
decoder().discardInboundReadBytes(ctx);
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- decoder().freeInboundBuffer(ctx);
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return encoder().newOutboundBuffer(ctx);
}
-
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- encoder().freeOutboundBuffer(ctx);
- }
}
diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameCodec.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameCodec.java
index d60698cd62..67b73b1fad 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameCodec.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyFrameCodec.java
@@ -70,18 +70,8 @@ public final class SpdyFrameCodec
decoder().discardInboundReadBytes(ctx);
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- decoder().freeInboundBuffer(ctx);
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return encoder().newOutboundBuffer(ctx);
}
-
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- encoder().freeOutboundBuffer(ctx);
- }
}
diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpCodec.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpCodec.java
index 311e7edf8c..cfb73bf528 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpCodec.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyHttpCodec.java
@@ -50,18 +50,8 @@ public final class SpdyHttpCodec
return decoder().newInboundBuffer(ctx);
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- decoder().freeInboundBuffer(ctx);
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return encoder().newOutboundBuffer(ctx);
}
-
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- encoder().freeOutboundBuffer(ctx);
- }
}
diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java
index cc011d7fd1..9baca054eb 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdyOrHttpChooser.java
@@ -72,11 +72,6 @@ public abstract class SpdyOrHttpChooser extends ChannelInboundByteHandlerAdapter
// No need to discard anything because this handler will be replaced with something else very quickly.
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ctx.inboundByteBuffer().release();
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
if (initPipeline(ctx)) {
diff --git a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java
index 0e283af67f..0062e2d3ab 100644
--- a/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java
+++ b/codec-http/src/main/java/io/netty/handler/codec/spdy/SpdySessionHandler.java
@@ -89,21 +89,11 @@ public class SpdySessionHandler
return Unpooled.messageBuffer();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ctx.inboundMessageBuffer().release();
- }
-
@Override
public MessageBuf
*/
Buf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception;
-
- /**
- * Invoked when this handler is not allowed to send any outbound message anymore and thus it's safe to
- * deallocate its outbound buffer.
- *
- * Please note that this method can be called from any thread repeatatively, and thus you should not perform
- * stateful operation here.
- *
- */
- void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception;
}
diff --git a/transport/src/main/java/io/netty/channel/ChannelOutboundMessageHandlerAdapter.java b/transport/src/main/java/io/netty/channel/ChannelOutboundMessageHandlerAdapter.java
index 02bf38aeed..4889e035a5 100644
--- a/transport/src/main/java/io/netty/channel/ChannelOutboundMessageHandlerAdapter.java
+++ b/transport/src/main/java/io/netty/channel/ChannelOutboundMessageHandlerAdapter.java
@@ -89,11 +89,6 @@ public abstract class ChannelOutboundMessageHandlerAdapter
return Unpooled.messageBuffer();
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ctx.outboundMessageBuffer().release();
- }
-
@Override
public final void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
ChannelHandlerUtil.handleFlush(ctx, promise, isCloseOnFailedFlush(), this);
diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java
index 665e1ea109..439d2d254c 100755
--- a/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java
+++ b/transport/src/main/java/io/netty/channel/DefaultChannelHandlerContext.java
@@ -433,28 +433,27 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
private void freeHandlerBuffersAfterRemoval() {
int flags = this.flags;
if ((flags & FLAG_REMOVED) != 0 && (flags & FLAG_FREED) == 0) { // Removed, but not freed yet
- final ChannelHandler handler = handler();
try {
- if (handler instanceof ChannelInboundHandler) {
- try {
- ((ChannelInboundHandler) handler).freeInboundBuffer(this);
- } catch (Exception e) {
- notifyHandlerException(e);
- }
- }
- if (handler instanceof ChannelOutboundHandler) {
- try {
- ((ChannelOutboundHandler) handler).freeOutboundBuffer(this);
- } catch (Exception e) {
- notifyHandlerException(e);
- }
- }
+ freeBuffer(inByteBuf);
+ freeBuffer(inMsgBuf);
+ freeBuffer(outByteBuf);
+ freeBuffer(outMsgBuf);
} finally {
free();
}
}
}
+ private void freeBuffer(Buf buf) {
+ if (buf != null) {
+ try {
+ buf.release();
+ } catch (Exception e) {
+ notifyHandlerException(e);
+ }
+ }
+ }
+
private void free() {
flags |= FLAG_FREED;
freeInbound();
@@ -1477,13 +1476,9 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeFreeInboundBuffer0() {
- ChannelHandler handler = handler();
try {
- if (handler instanceof ChannelInboundHandler) {
- ((ChannelInboundHandler) handler).freeInboundBuffer(this);
- }
- } catch (Throwable t) {
- notifyHandlerException(t);
+ freeBuffer(inByteBuf);
+ freeBuffer(inMsgBuf);
} finally {
freeInbound();
}
@@ -1526,13 +1521,9 @@ final class DefaultChannelHandlerContext extends DefaultAttributeMap implements
}
private void invokeFreeOutboundBuffer0() {
- ChannelHandler handler = handler();
try {
- if (handler instanceof ChannelOutboundHandler) {
- ((ChannelOutboundHandler) handler).freeOutboundBuffer(this);
- }
- } catch (Throwable t) {
- notifyHandlerException(t);
+ freeBuffer(outByteBuf);
+ freeBuffer(outMsgBuf);
} finally {
freeOutbound();
}
diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java
index 4605e39631..ab214a256e 100755
--- a/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java
+++ b/transport/src/main/java/io/netty/channel/DefaultChannelPipeline.java
@@ -1011,12 +1011,6 @@ final class DefaultChannelPipeline implements ChannelPipeline {
throw new Error();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- byteSink.release();
- msgSink.release();
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
int byteSinkSize = byteSink.readableBytes();
@@ -1133,12 +1127,6 @@ final class DefaultChannelPipeline implements ChannelPipeline {
throw new Error();
}
- @Override
- public final void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- msgSink.release();
- byteSink.release();
- }
-
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.fireExceptionCaught(cause);
diff --git a/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java b/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java
index 4bb690789d..f950809652 100755
--- a/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java
+++ b/transport/src/main/java/io/netty/channel/embedded/AbstractEmbeddedChannel.java
@@ -52,8 +52,8 @@ public abstract class AbstractEmbeddedChannel extends AbstractChannel {
private final ChannelConfig config = new DefaultChannelConfig(this);
private final SocketAddress localAddress = new EmbeddedSocketAddress();
private final SocketAddress remoteAddress = new EmbeddedSocketAddress();
- private final MessageBuf lastInboundMessageBuffer = Unpooled.messageBuffer();
- private final ByteBuf lastInboundByteBuffer = Unpooled.buffer();
+ private final MessageBuf lastInboundMessageBuffer = Unpooled.messageBuffer().retain(2);
+ private final ByteBuf lastInboundByteBuffer = Unpooled.buffer().retain(2);
protected final Object lastOutboundBuffer;
private Throwable lastException;
private int state; // 0 = OPEN, 1 = ACTIVE, 2 = CLOSED
@@ -331,11 +331,6 @@ public abstract class AbstractEmbeddedChannel extends AbstractChannel {
return lastInboundMessageBuffer;
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- // Do NOT free the buffer.
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
// Do nothing.
@@ -360,11 +355,6 @@ public abstract class AbstractEmbeddedChannel extends AbstractChannel {
// nothing
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- // Do NOT free the buffer.
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
// No nothing
diff --git a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java
index 2610602b74..d2def281fc 100644
--- a/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java
+++ b/transport/src/test/java/io/netty/channel/DefaultChannelPipelineTest.java
@@ -801,11 +801,6 @@ public class DefaultChannelPipelineTest {
return Unpooled.messageBuffer();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ctx.inboundMessageBuffer().release();
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
updated = true;
@@ -820,11 +815,6 @@ public class DefaultChannelPipelineTest {
return Unpooled.messageBuffer();
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ctx.outboundMessageBuffer().release();
- }
-
@Override
public void flush(ChannelHandlerContext ctx, ChannelPromise promise) throws Exception {
promise.setSuccess();
@@ -848,11 +838,6 @@ public class DefaultChannelPipelineTest {
((ChannelInboundByteHandler) stateHandler()).discardInboundReadBytes(ctx);
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ((ChannelInboundHandler) stateHandler()).freeInboundBuffer(ctx);
- }
-
@Override
public ByteBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ((ChannelOutboundByteHandler) operationHandler()).newOutboundBuffer(ctx);
@@ -862,11 +847,6 @@ public class DefaultChannelPipelineTest {
public void discardOutboundReadBytes(ChannelHandlerContext ctx) throws Exception {
((ChannelOutboundByteHandler) operationHandler()).discardOutboundReadBytes(ctx);
}
-
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ((ChannelOutboundHandler) operationHandler()).freeOutboundBuffer(ctx);
- }
}
private static final class MessageHandlerImpl extends CombinedChannelDuplexHandler
@@ -881,23 +861,11 @@ public class DefaultChannelPipelineTest {
return ((ChannelInboundMessageHandler) stateHandler()).newInboundBuffer(ctx);
}
- @SuppressWarnings("unchecked")
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ((ChannelInboundHandler) stateHandler()).freeInboundBuffer(ctx);
- }
-
@SuppressWarnings("unchecked")
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ((ChannelOutboundMessageHandler) operationHandler()).newOutboundBuffer(ctx);
}
-
- @SuppressWarnings("unchecked")
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ((ChannelOutboundHandler) operationHandler()).freeOutboundBuffer(ctx);
- }
}
/** Test handler to validate life-cycle aware behavior. */
diff --git a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java
index f2630eb9bd..db1677f09c 100644
--- a/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java
+++ b/transport/src/test/java/io/netty/channel/local/LocalTransportThreadModelTest.java
@@ -344,21 +344,11 @@ public class LocalTransportThreadModelTest {
return Unpooled.messageBuffer();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- // Nothing to free
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return Unpooled.messageBuffer();
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) {
- // Nothing to free
- }
-
@Override
public void inboundBufferUpdated(
ChannelHandlerContext ctx) throws Exception {
@@ -406,11 +396,6 @@ public class LocalTransportThreadModelTest {
return Unpooled.messageBuffer();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- // Nothing to free
- }
-
@Override
public ByteBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return ChannelHandlerUtil.allocate(ctx);
@@ -421,11 +406,6 @@ public class LocalTransportThreadModelTest {
// NOOP
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) {
- ctx.outboundByteBuffer().release();
- }
-
@Override
public void inboundBufferUpdated(
ChannelHandlerContext ctx) throws Exception {
@@ -520,22 +500,12 @@ public class LocalTransportThreadModelTest {
ctx.inboundByteBuffer().discardSomeReadBytes();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- ctx.inboundByteBuffer().release();
- }
-
@Override
public MessageBuf newOutboundBuffer(
ChannelHandlerContext ctx) throws Exception {
return Unpooled.messageBuffer();
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) {
- // Nothing to free
- }
-
@Override
public void inboundBufferUpdated(
ChannelHandlerContext ctx) throws Exception {
@@ -617,21 +587,11 @@ public class LocalTransportThreadModelTest {
return Unpooled.messageBuffer();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- // Nothing to free
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return Unpooled.messageBuffer();
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) {
- // Nothing to free
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
Thread t = this.t;
@@ -707,21 +667,11 @@ public class LocalTransportThreadModelTest {
return Unpooled.messageBuffer();
}
- @Override
- public void freeInboundBuffer(ChannelHandlerContext ctx) throws Exception {
- // Nothing to free
- }
-
@Override
public MessageBuf newOutboundBuffer(ChannelHandlerContext ctx) throws Exception {
return Unpooled.messageBuffer();
}
- @Override
- public void freeOutboundBuffer(ChannelHandlerContext ctx) {
- // Nothing to free
- }
-
@Override
public void inboundBufferUpdated(ChannelHandlerContext ctx) throws Exception {
Thread t = this.t;