From cc0d7d8be50bd59c369529305228c328b561993f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Wed, 2 May 2012 12:35:36 +0300 Subject: [PATCH 1/8] typo fix! sorry --- .../io/netty/handler/codec/http/HttpPostRequestDecoder.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostRequestDecoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostRequestDecoder.java index 88d96ad42e..7f0c9689d7 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostRequestDecoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpPostRequestDecoder.java @@ -851,7 +851,7 @@ public class HttpPostRequestDecoder { } while (sao.pos < sao.limit) { - char c = (char) ()sao.bytes[sao.pos ++] & 0xFF); + char c = (char) (sao.bytes[sao.pos ++] & 0xFF); if (!Character.isISOControl(c) && !Character.isWhitespace(c)) { sao.setReadPosition(1); return; From 923498de92804a7e41caf13db97475f5e8cbc5a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:17:02 +0300 Subject: [PATCH 2/8] Add support for ObjectSizeEstimator --- .../traffic/ChannelTrafficShapingHandler.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java index 0c8ca4b2ba..ef5b51f7da 100644 --- a/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java @@ -23,6 +23,7 @@ import io.netty.channel.ChannelStateEvent; import io.netty.handler.execution.ExecutionHandler; import io.netty.handler.execution.MemoryAwareThreadPoolExecutor; import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; +import org.jboss.netty.util.ObjectSizeEstimator; /** * This implementation of the {@link AbstractTrafficShapingHandler} is for channel @@ -79,6 +80,69 @@ public class ChannelTrafficShapingHandler extends AbstractTrafficShapingHandler long readLimit) { super(executor, writeLimit, readLimit); } + + + /** + * @param executor + * @param checkInterval + */ + public ChannelTrafficShapingHandler(Executor executor, long checkInterval) { + super(executor, checkInterval); + } + + /** + * @param executor + */ + public ChannelTrafficShapingHandler(Executor executor) { + super(executor); + } + + /** + * @param objectSizeEstimator + * @param executor + * @param writeLimit + * @param readLimit + * @param checkInterval + */ + public ChannelTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor, + long writeLimit, long readLimit, long checkInterval) { + super(objectSizeEstimator, executor, writeLimit, readLimit, + checkInterval); + } + + /** + * @param objectSizeEstimator + * @param executor + * @param writeLimit + * @param readLimit + */ + public ChannelTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor, + long writeLimit, long readLimit) { + super(objectSizeEstimator, executor, writeLimit, readLimit); + } + + /** + * @param objectSizeEstimator + * @param executor + * @param checkInterval + */ + public ChannelTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor, + long checkInterval) { + super(objectSizeEstimator, executor, checkInterval); + } + + /** + * @param objectSizeEstimator + * @param executor + */ + public ChannelTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor) { + super(objectSizeEstimator, executor); + } + @Override public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) From 0996bac7fffc2a4d0e68d05149b8af6ae2ed42f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:20:46 +0300 Subject: [PATCH 3/8] Add support for ObjectSizeEstimator: in order to allow special optimization if possible from user code --- .../AbstractTrafficShapingHandler.java | 125 +++++++++++++++++- 1 file changed, 119 insertions(+), 6 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java index 45219228b3..12edb6d788 100644 --- a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java @@ -28,7 +28,9 @@ import io.netty.channel.MessageEvent; import io.netty.channel.SimpleChannelHandler; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; -import io.netty.util.ExternalResourceReleasable; +import org.jboss.netty.util.DefaultObjectSizeEstimator; +import org.jboss.netty.util.ExternalResourceReleasable; +import org.jboss.netty.util.ObjectSizeEstimator; import io.netty.util.internal.ExecutorUtil; /** @@ -72,6 +74,11 @@ public abstract class AbstractTrafficShapingHandler extends */ protected TrafficCounter trafficCounter; + /** + * ObjectSizeEstimator + */ + private ObjectSizeEstimator objectSizeEstimator; + /** * Executor to associated to any TrafficCounter */ @@ -99,8 +106,9 @@ public abstract class AbstractTrafficShapingHandler extends */ final AtomicBoolean release = new AtomicBoolean(false); - private void init( + private void init(ObjectSizeEstimator newObjectSizeEstimator, Executor newExecutor, long newWriteLimit, long newReadLimit, long newCheckInterval) { + objectSizeEstimator = newObjectSizeEstimator; executor = newExecutor; writeLimit = newWriteLimit; readLimit = newReadLimit; @@ -117,6 +125,8 @@ public abstract class AbstractTrafficShapingHandler extends } /** + * Constructor using default {@link ObjectSizeEstimator} + * * @param executor * created for instance like Executors.newCachedThreadPool * @param writeLimit @@ -129,10 +139,36 @@ public abstract class AbstractTrafficShapingHandler extends */ public AbstractTrafficShapingHandler(Executor executor, long writeLimit, long readLimit, long checkInterval) { - init(executor, writeLimit, readLimit, checkInterval); + init(new DefaultObjectSizeEstimator(), executor, writeLimit, readLimit, + checkInterval); } /** + * Constructor using the specified ObjectSizeEstimator + * + * @param objectSizeEstimator + * the {@link ObjectSizeEstimator} that will be used to compute + * the size of the message + * @param executor + * created for instance like Executors.newCachedThreadPool + * @param writeLimit + * 0 or a limit in bytes/s + * @param readLimit + * 0 or a limit in bytes/s + * @param checkInterval + * The delay between two computations of performances for + * channels or 0 if no stats are to be computed + */ + public AbstractTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor, + long writeLimit, long readLimit, long checkInterval) { + init(objectSizeEstimator, executor, writeLimit, readLimit, + checkInterval); + } + + /** + * Constructor using default {@link ObjectSizeEstimator} and using default Check Interval + * * @param executor * created for instance like Executors.newCachedThreadPool * @param writeLimit @@ -142,7 +178,84 @@ public abstract class AbstractTrafficShapingHandler extends */ public AbstractTrafficShapingHandler(Executor executor, long writeLimit, long readLimit) { - init(executor, writeLimit, readLimit, DEFAULT_CHECK_INTERVAL); + init(new DefaultObjectSizeEstimator(), executor, writeLimit, readLimit, + DEFAULT_CHECK_INTERVAL); + } + + /** + * Constructor using the specified ObjectSizeEstimator and using default Check Interval + * + * @param objectSizeEstimator + * the {@link ObjectSizeEstimator} that will be used to compute + * the size of the message + * @param executor + * created for instance like Executors.newCachedThreadPool + * @param writeLimit + * 0 or a limit in bytes/s + * @param readLimit + * 0 or a limit in bytes/s + */ + public AbstractTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor, + long writeLimit, long readLimit) { + init(objectSizeEstimator, executor, writeLimit, readLimit, + DEFAULT_CHECK_INTERVAL); + } + + /** + * Constructor using default {@link ObjectSizeEstimator} and using NO LIMIT and default Check Interval + * + * @param executor + * created for instance like Executors.newCachedThreadPool + */ + public AbstractTrafficShapingHandler(Executor executor) { + init(new DefaultObjectSizeEstimator(), executor, 0, 0, + DEFAULT_CHECK_INTERVAL); + } + + /** + * Constructor using the specified ObjectSizeEstimator and using NO LIMIT and default Check Interval + * + * @param objectSizeEstimator + * the {@link ObjectSizeEstimator} that will be used to compute + * the size of the message + * @param executor + * created for instance like Executors.newCachedThreadPool + */ + public AbstractTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor) { + init(objectSizeEstimator, executor, 0, 0, DEFAULT_CHECK_INTERVAL); + } + + /** + * Constructor using default {@link ObjectSizeEstimator} and using NO LIMIT + * + * @param executor + * created for instance like Executors.newCachedThreadPool + * @param checkInterval + * The delay between two computations of performances for + * channels or 0 if no stats are to be computed + */ + public AbstractTrafficShapingHandler(Executor executor, long checkInterval) { + init(new DefaultObjectSizeEstimator(), executor, 0, 0, checkInterval); + } + + /** + * Constructor using the specified ObjectSizeEstimator and using NO LIMIT + * + * @param objectSizeEstimator + * the {@link ObjectSizeEstimator} that will be used to compute + * the size of the message + * @param executor + * created for instance like Executors.newCachedThreadPool + * @param checkInterval + * The delay between two computations of performances for + * channels or 0 if no stats are to be computed + */ + public AbstractTrafficShapingHandler( + ObjectSizeEstimator objectSizeEstimator, Executor executor, + long checkInterval) { + init(objectSizeEstimator, executor, 0, 0, checkInterval); } /** @@ -255,7 +368,7 @@ public abstract class AbstractTrafficShapingHandler extends throws Exception { try { long curtime = System.currentTimeMillis(); - long size = ((ChannelBuffer) arg1.getMessage()).readableBytes(); + long size = objectSizeEstimator.estimateSize(arg1.getMessage()); if (trafficCounter != null) { trafficCounter.bytesRecvFlowControl(arg0, size); if (readLimit == 0) { @@ -315,7 +428,7 @@ public abstract class AbstractTrafficShapingHandler extends throws Exception { try { long curtime = System.currentTimeMillis(); - long size = ((ChannelBuffer) arg1.getMessage()).readableBytes(); + long size = objectSizeEstimator.estimateSize(arg1.getMessage()); if (trafficCounter != null) { trafficCounter.bytesWriteFlowControl(size); if (writeLimit == 0) { From a1a60ec5b6d585e1503061f8435be27fd269d5db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:23:11 +0300 Subject: [PATCH 4/8] Add support for ObjectSizeEstimator --- .../traffic/GlobalTrafficShapingHandler.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java index 73fc97df6d..dc9a6d2091 100644 --- a/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandler.Sharable; import io.netty.handler.execution.ExecutionHandler; import io.netty.handler.execution.MemoryAwareThreadPoolExecutor; import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; +import org.jboss.netty.util.ObjectSizeEstimator; /** * This implementation of the {@link AbstractTrafficShapingHandler} is for global @@ -91,4 +92,68 @@ public class GlobalTrafficShapingHandler extends AbstractTrafficShapingHandler { super(executor, writeLimit, readLimit); createGlobalTrafficCounter(); } + /** + * @param executor + * @param checkInterval + */ + public GlobalTrafficShapingHandler(Executor executor, long checkInterval) { + super(executor, checkInterval); + createGlobalTrafficCounter(); + } + + /** + * @param executor + */ + public GlobalTrafficShapingHandler(Executor executor) { + super(executor); + createGlobalTrafficCounter(); + } + + /** + * @param objectSizeEstimator + * @param executor + * @param writeLimit + * @param readLimit + * @param checkInterval + */ + public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, + Executor executor, long writeLimit, long readLimit, + long checkInterval) { + super(objectSizeEstimator, executor, writeLimit, readLimit, + checkInterval); + createGlobalTrafficCounter(); + } + + /** + * @param objectSizeEstimator + * @param executor + * @param writeLimit + * @param readLimit + */ + public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, + Executor executor, long writeLimit, long readLimit) { + super(objectSizeEstimator, executor, writeLimit, readLimit); + createGlobalTrafficCounter(); + } + + /** + * @param objectSizeEstimator + * @param executor + * @param checkInterval + */ + public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, + Executor executor, long checkInterval) { + super(objectSizeEstimator, executor, checkInterval); + createGlobalTrafficCounter(); + } + + /** + * @param objectSizeEstimator + * @param executor + */ + public GlobalTrafficShapingHandler(ObjectSizeEstimator objectSizeEstimator, + Executor executor) { + super(objectSizeEstimator, executor); + createGlobalTrafficCounter(); + } } From 7d4a276ab096d6bd892844a66f02c06218fd480d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:26:21 +0300 Subject: [PATCH 5/8] Fix private class to static private class (dynamicity is not necessary there) --- .../main/java/io/netty/handler/traffic/TrafficCounter.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java b/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java index 4904d36840..6976ac08c5 100644 --- a/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java +++ b/handler/src/main/java/io/netty/handler/traffic/TrafficCounter.java @@ -117,7 +117,7 @@ public class TrafficCounter { /** * Class to implement monitoring at fix delay */ - private class TrafficMonitoring implements Runnable { + private static class TrafficMonitoring implements Runnable { /** * The associated TrafficShapingHandler */ @@ -145,8 +145,8 @@ public class TrafficCounter { @Override public void run() { try { - Thread.currentThread().setName(name); - for (; monitorActive.get();) { + Thread.currentThread().setName(counter.name); + for (; counter.monitorActive.get();) { long check = counter.checkInterval.get(); if (check > 0) { Thread.sleep(check); From 51debe12873fd2f08c8e7443d882219e2ef162b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:32:36 +0300 Subject: [PATCH 6/8] import fix --- .../handler/traffic/AbstractTrafficShapingHandler.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java index 12edb6d788..7da4b29a96 100644 --- a/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/AbstractTrafficShapingHandler.java @@ -28,9 +28,9 @@ import io.netty.channel.MessageEvent; import io.netty.channel.SimpleChannelHandler; import io.netty.logging.InternalLogger; import io.netty.logging.InternalLoggerFactory; -import org.jboss.netty.util.DefaultObjectSizeEstimator; -import org.jboss.netty.util.ExternalResourceReleasable; -import org.jboss.netty.util.ObjectSizeEstimator; +import io.netty.util.DefaultObjectSizeEstimator; +import io.netty.util.ExternalResourceReleasable; +import io.netty.util.ObjectSizeEstimator; import io.netty.util.internal.ExecutorUtil; /** From 9c2262716fdcb6fdfee1893c496d76159441621d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:32:59 +0300 Subject: [PATCH 7/8] Import fix --- .../io/netty/handler/traffic/ChannelTrafficShapingHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java index ef5b51f7da..4fc4151124 100644 --- a/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/ChannelTrafficShapingHandler.java @@ -23,7 +23,7 @@ import io.netty.channel.ChannelStateEvent; import io.netty.handler.execution.ExecutionHandler; import io.netty.handler.execution.MemoryAwareThreadPoolExecutor; import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; -import org.jboss.netty.util.ObjectSizeEstimator; +import io.netty.util.ObjectSizeEstimator; /** * This implementation of the {@link AbstractTrafficShapingHandler} is for channel From 1afb209010d7db97f2bc76fdad44ff8ca569c755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Br=C3=A9gier?= Date: Fri, 18 May 2012 15:33:23 +0300 Subject: [PATCH 8/8] import fix --- .../io/netty/handler/traffic/GlobalTrafficShapingHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java b/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java index dc9a6d2091..06055e2cb9 100644 --- a/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java +++ b/handler/src/main/java/io/netty/handler/traffic/GlobalTrafficShapingHandler.java @@ -21,7 +21,7 @@ import io.netty.channel.ChannelHandler.Sharable; import io.netty.handler.execution.ExecutionHandler; import io.netty.handler.execution.MemoryAwareThreadPoolExecutor; import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; -import org.jboss.netty.util.ObjectSizeEstimator; +import io.netty.util.ObjectSizeEstimator; /** * This implementation of the {@link AbstractTrafficShapingHandler} is for global