Use a percentual calculation to see if a new buffer should get allocated. See #480
This commit is contained in:
parent
d772e3cc74
commit
f129ee1e28
@ -34,8 +34,7 @@ import org.jboss.netty.channel.ReceiveBufferSizePredictor;
|
||||
|
||||
public class NioWorker extends AbstractNioWorker {
|
||||
|
||||
// TODO: Make this configurable ?
|
||||
private final SocketReceiveBufferAllocator recvBufferPool = new SocketReceiveBufferAllocator(10);
|
||||
private final SocketReceiveBufferAllocator recvBufferPool = new SocketReceiveBufferAllocator();
|
||||
|
||||
public NioWorker(Executor executor) {
|
||||
super(executor);
|
||||
|
@ -24,11 +24,18 @@ final class SocketReceiveBufferAllocator {
|
||||
private ByteBuffer buf;
|
||||
private int exceedCount;
|
||||
private final int maxExceedCount;
|
||||
private final int percentual;
|
||||
|
||||
SocketReceiveBufferAllocator(int maxExceedCount) {
|
||||
this.maxExceedCount = maxExceedCount;
|
||||
SocketReceiveBufferAllocator() {
|
||||
this(16, 80);
|
||||
}
|
||||
|
||||
SocketReceiveBufferAllocator(int maxExceedCount, int percentual) {
|
||||
this.maxExceedCount = maxExceedCount;
|
||||
this.percentual = percentual;
|
||||
}
|
||||
|
||||
|
||||
ByteBuffer get(int size) {
|
||||
if (buf == null) {
|
||||
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
|
||||
@ -37,14 +44,17 @@ final class SocketReceiveBufferAllocator {
|
||||
ByteBufferUtil.destroy(buf);
|
||||
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
|
||||
} else if (buf.capacity() > size) {
|
||||
if (++exceedCount == maxExceedCount) {
|
||||
if (((buf.capacity() / 100) * percentual) > size) {
|
||||
if (++exceedCount == maxExceedCount) {
|
||||
exceedCount = 0;
|
||||
ByteBufferUtil.destroy(buf);
|
||||
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
|
||||
}
|
||||
} else {
|
||||
exceedCount = 0;
|
||||
ByteBufferUtil.destroy(buf);
|
||||
buf = ByteBuffer.allocateDirect(normalizeCapacity(size));
|
||||
}
|
||||
} else {
|
||||
buf.position(0).limit(size);
|
||||
}
|
||||
buf.clear();
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user