From 368c34ace942e498be0b3dd371cdf82b1e04ff26 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 11 Mar 2009 09:27:14 +0000 Subject: [PATCH] Renamed DefaultReceiveBufferSizePredictor to AdaptiveReceiveBufferSizePredictor Added FixedReceiveBufferSizePredictor --- ...> AdaptiveReceiveBufferSizePredictor.java} | 19 +++--- .../FixedReceiveBufferSizePredictor.java | 59 +++++++++++++++++++ .../nio/DefaultNioSocketChannelConfig.java | 4 +- .../socket/nio/NioSocketChannelConfig.java | 6 +- .../xnio/DefaultXnioChannelConfig.java | 4 +- .../netty/channel/xnio/XnioChannelConfig.java | 6 +- 6 files changed, 79 insertions(+), 19 deletions(-) rename src/main/java/org/jboss/netty/channel/{DefaultReceiveBufferSizePredictor.java => AdaptiveReceiveBufferSizePredictor.java} (89%) create mode 100644 src/main/java/org/jboss/netty/channel/FixedReceiveBufferSizePredictor.java diff --git a/src/main/java/org/jboss/netty/channel/DefaultReceiveBufferSizePredictor.java b/src/main/java/org/jboss/netty/channel/AdaptiveReceiveBufferSizePredictor.java similarity index 89% rename from src/main/java/org/jboss/netty/channel/DefaultReceiveBufferSizePredictor.java rename to src/main/java/org/jboss/netty/channel/AdaptiveReceiveBufferSizePredictor.java index d06c4c94a7..3f2f8722a4 100644 --- a/src/main/java/org/jboss/netty/channel/DefaultReceiveBufferSizePredictor.java +++ b/src/main/java/org/jboss/netty/channel/AdaptiveReceiveBufferSizePredictor.java @@ -26,13 +26,14 @@ import java.util.ArrayList; import java.util.List; /** - * The default {@link ReceiveBufferSizePredictor} implementation. + * The {@link ReceiveBufferSizePredictor} that automatically increases and + * decreases the predicted buffer size on feed back. *

- * It gradually increases the expected number of readable bytes if the - * previous read filled the allocated buffer. It gradually decreases the - * expected number of readable bytes if the read operation was not able to - * fill a certain amount of the allocated buffer two times consecutively. - * Otherwise, it keeps returning the previous prediction. + * It gradually increases the expected number of readable bytes if the previous + * read filled the allocated buffer. It gradually decreases the expected number + * of readable bytes if the read operation was not able to fill a certain amount + * of the allocated buffer two times consecutively. Otherwise, it keeps + * returning the previous prediction. * * @author The Netty Project (netty-dev@lists.jboss.org) * @author Trustin Lee (tlee@redhat.com) @@ -40,7 +41,7 @@ import java.util.List; * @version $Rev$, $Date$ * */ -public class DefaultReceiveBufferSizePredictor implements +public class AdaptiveReceiveBufferSizePredictor implements ReceiveBufferSizePredictor { private static final int INDEX_INCREMENT = 4; @@ -115,7 +116,7 @@ public class DefaultReceiveBufferSizePredictor implements * parameters, the expected buffer size starts from {@code 1024}, does not * go down below {@code 64}, and does not go up above {@code 65536}. */ - public DefaultReceiveBufferSizePredictor() { + public AdaptiveReceiveBufferSizePredictor() { this(DEFAULT_MINIMUM, DEFAULT_INITIAL, DEFAULT_MAXIMUM); } @@ -126,7 +127,7 @@ public class DefaultReceiveBufferSizePredictor implements * @param initial the initial buffer size when no feed back was received * @param maximum the inclusive upper bound of the expected buffer size */ - public DefaultReceiveBufferSizePredictor(int minimum, int initial, int maximum) { + public AdaptiveReceiveBufferSizePredictor(int minimum, int initial, int maximum) { if (minimum <= 0) { throw new IllegalArgumentException("minimum: " + minimum); } diff --git a/src/main/java/org/jboss/netty/channel/FixedReceiveBufferSizePredictor.java b/src/main/java/org/jboss/netty/channel/FixedReceiveBufferSizePredictor.java new file mode 100644 index 0000000000..58c0a32faf --- /dev/null +++ b/src/main/java/org/jboss/netty/channel/FixedReceiveBufferSizePredictor.java @@ -0,0 +1,59 @@ +/* + * JBoss, Home of Professional Open Source + * + * Copyright 2008, Red Hat Middleware LLC, and individual contributors + * by the @author tags. See the COPYRIGHT.txt in the distribution for a + * full listing of individual contributors. + * + * This is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this software; if not, write to the Free + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. + */ +package org.jboss.netty.channel; + + +/** + * The {@link ReceiveBufferSizePredictor} that always yields the same buffer + * size prediction. + * + * @author The Netty Project (netty-dev@lists.jboss.org) + * @author Trustin Lee (tlee@redhat.com) + * + * @version $Rev$, $Date$ + */ +public class FixedReceiveBufferSizePredictor implements + ReceiveBufferSizePredictor { + + private final int bufferSize; + + /** + * Creates a new predictor that always returns the same prediction of + * the specified buffer size. + */ + public FixedReceiveBufferSizePredictor(int bufferSize) { + if (bufferSize <= 0) { + throw new IllegalArgumentException( + "bufferSize must greater than 0: " + bufferSize); + } + this.bufferSize = bufferSize; + } + + public int nextReceiveBufferSize() { + return bufferSize; + } + + public void previousReceiveBufferSize(int previousReceiveBufferSize) { + // Ignore + } +} diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java b/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java index 85e2c9c8b6..efbdf2caeb 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/DefaultNioSocketChannelConfig.java @@ -25,7 +25,7 @@ package org.jboss.netty.channel.socket.nio; import java.net.Socket; import java.util.Map; -import org.jboss.netty.channel.DefaultReceiveBufferSizePredictor; +import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor; import org.jboss.netty.channel.ReceiveBufferSizePredictor; import org.jboss.netty.channel.socket.DefaultSocketChannelConfig; import org.jboss.netty.logging.InternalLogger; @@ -50,7 +50,7 @@ class DefaultNioSocketChannelConfig extends DefaultSocketChannelConfig private volatile int writeBufferHighWaterMark = 64 * 1024; private volatile int writeBufferLowWaterMark = 32 * 1024; private volatile ReceiveBufferSizePredictor predictor = - new DefaultReceiveBufferSizePredictor(); + new AdaptiveReceiveBufferSizePredictor(); private volatile int writeSpinCount = 16; DefaultNioSocketChannelConfig(Socket socket) { diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java index 7180cb8e9a..3d42b564ae 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioSocketChannelConfig.java @@ -26,7 +26,7 @@ import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; import org.jboss.netty.channel.ChannelConfig; -import org.jboss.netty.channel.DefaultReceiveBufferSizePredictor; +import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor; import org.jboss.netty.channel.ReceiveBufferSizePredictor; import org.jboss.netty.channel.socket.SocketChannel; import org.jboss.netty.channel.socket.SocketChannelConfig; @@ -89,14 +89,14 @@ public interface NioSocketChannelConfig extends SocketChannelConfig { /** * Returns the {@link ReceiveBufferSizePredictor} which predicts the * number of readable bytes in the socket receive buffer. The default - * predictor is {@link DefaultReceiveBufferSizePredictor}. + * predictor is {@link AdaptiveReceiveBufferSizePredictor}. */ ReceiveBufferSizePredictor getReceiveBufferSizePredictor(); /** * Sets the {@link ReceiveBufferSizePredictor} which predicts the * number of readable bytes in the socket receive buffer. The default - * predictor is {@link DefaultReceiveBufferSizePredictor}. + * predictor is {@link AdaptiveReceiveBufferSizePredictor}. */ void setReceiveBufferSizePredictor(ReceiveBufferSizePredictor predictor); diff --git a/src/main/java/org/jboss/netty/channel/xnio/DefaultXnioChannelConfig.java b/src/main/java/org/jboss/netty/channel/xnio/DefaultXnioChannelConfig.java index 0aaa21dad3..94ab9e0f91 100644 --- a/src/main/java/org/jboss/netty/channel/xnio/DefaultXnioChannelConfig.java +++ b/src/main/java/org/jboss/netty/channel/xnio/DefaultXnioChannelConfig.java @@ -28,7 +28,7 @@ import java.util.Map.Entry; import org.jboss.netty.buffer.ChannelBufferFactory; import org.jboss.netty.buffer.HeapChannelBufferFactory; import org.jboss.netty.channel.ChannelPipelineFactory; -import org.jboss.netty.channel.DefaultReceiveBufferSizePredictor; +import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor; import org.jboss.netty.channel.ReceiveBufferSizePredictor; import org.jboss.netty.logging.InternalLogger; import org.jboss.netty.logging.InternalLoggerFactory; @@ -53,7 +53,7 @@ final class DefaultXnioChannelConfig implements XnioChannelConfig { private volatile int writeBufferHighWaterMark = 64 * 1024; private volatile int writeBufferLowWaterMark = 32 * 1024; private volatile ReceiveBufferSizePredictor predictor = - new DefaultReceiveBufferSizePredictor(); + new AdaptiveReceiveBufferSizePredictor(); private volatile int writeSpinCount = 16; DefaultXnioChannelConfig() { diff --git a/src/main/java/org/jboss/netty/channel/xnio/XnioChannelConfig.java b/src/main/java/org/jboss/netty/channel/xnio/XnioChannelConfig.java index b2d9551ac7..7724d8d419 100644 --- a/src/main/java/org/jboss/netty/channel/xnio/XnioChannelConfig.java +++ b/src/main/java/org/jboss/netty/channel/xnio/XnioChannelConfig.java @@ -26,7 +26,7 @@ import java.nio.ByteBuffer; import java.nio.channels.WritableByteChannel; import org.jboss.netty.channel.ChannelConfig; -import org.jboss.netty.channel.DefaultReceiveBufferSizePredictor; +import org.jboss.netty.channel.AdaptiveReceiveBufferSizePredictor; import org.jboss.netty.channel.ReceiveBufferSizePredictor; import org.jboss.netty.channel.socket.SocketChannelConfig; @@ -86,14 +86,14 @@ public interface XnioChannelConfig extends ChannelConfig { /** * Returns the {@link ReceiveBufferSizePredictor} which predicts the * number of readable bytes in the socket receive buffer. The default - * predictor is {@link DefaultReceiveBufferSizePredictor}. + * predictor is {@link AdaptiveReceiveBufferSizePredictor}. */ ReceiveBufferSizePredictor getReceiveBufferSizePredictor(); /** * Sets the {@link ReceiveBufferSizePredictor} which predicts the * number of readable bytes in the socket receive buffer. The default - * predictor is {@link DefaultReceiveBufferSizePredictor}. + * predictor is {@link AdaptiveReceiveBufferSizePredictor}. */ void setReceiveBufferSizePredictor(ReceiveBufferSizePredictor predictor); } \ No newline at end of file