Skip to content

Commit

Permalink
Remove buffer max
Browse files Browse the repository at this point in the history
  • Loading branch information
sgammon committed Nov 13, 2019
1 parent 8a82029 commit 99470af
Showing 1 changed file with 21 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
@SuppressWarnings("unused")
public class SoyResponseBuffer implements Closeable, AutoCloseable, AdvisingAppendable {
private static final Logger LOG = LoggerFactory.getLogger(SoyResponseBuffer.class);
private static final int DEFAULT_BUFFER_SIZE = 4096;
private static final int MAX_BUFFER_CHUNKS = 2048;
private static final int DEFAULT_INITIAL_BUFFER = 1024;
static final int MAX_CHUNK_SIZE = DEFAULT_INITIAL_BUFFER * 2;
Expand All @@ -58,15 +57,14 @@ public class SoyResponseBuffer implements Closeable, AutoCloseable, AdvisingAppe
private final Charset charset;
private final float softLimit;
private final int initialBufferSize;
private final int maxBufferSize;

// -- Constructors -- //

/**
* Construct an `SoyResponseBuffer` backed by a buffer of default size, with the default charset.
*/
SoyResponseBuffer() {
this(DEFAULT_CHARSET, DEFAULT_INITIAL_BUFFER, DEFAULT_BUFFER_SIZE, DEFAULT_SOFT_LIMIT);
this(DEFAULT_CHARSET, DEFAULT_INITIAL_BUFFER, DEFAULT_SOFT_LIMIT);
}

/**
Expand All @@ -75,7 +73,7 @@ public class SoyResponseBuffer implements Closeable, AutoCloseable, AdvisingAppe
* @param charset Character set to use.
*/
public SoyResponseBuffer(Charset charset) {
this(charset, DEFAULT_INITIAL_BUFFER, DEFAULT_BUFFER_SIZE);
this(charset, DEFAULT_INITIAL_BUFFER);
}

/**
Expand All @@ -84,28 +82,24 @@ public SoyResponseBuffer(Charset charset) {
* @param charset Charset to use.
* @param initialBufferSize Initial buffer size to use.
*/
private SoyResponseBuffer(Charset charset, int initialBufferSize, int maxBufferSize) {
this(charset, initialBufferSize, maxBufferSize, DEFAULT_SOFT_LIMIT);
private SoyResponseBuffer(Charset charset, int initialBufferSize) {
this(charset, initialBufferSize, DEFAULT_SOFT_LIMIT);
}

/**
* Construct an `SoyResponseBuffer` backed by a buffer of a custom size and charset.
*
* @param charset Charset to use.
* @param initialBufferSize Buffer size to use.
* @param maxBufferSize Maximum buffer size to use.
* @param softLimitRatio Ratio of buffer fill at which to begin reporting `true` for {@link #softLimitReached()}.
*/
private SoyResponseBuffer(Charset charset, int initialBufferSize, int maxBufferSize, float softLimitRatio) {
if (maxBufferSize < 1) {
throw new IllegalArgumentException("Cannot create `SoyResponseBuffer` with max buffer size less than 1.");
} else if (softLimitRatio > 1.0 || softLimitRatio < 0.0) {
private SoyResponseBuffer(Charset charset, int initialBufferSize, float softLimitRatio) {
if (softLimitRatio > 1.0 || softLimitRatio < 0.0) {
throw new IllegalArgumentException(
"Cannot create `SoyResponseBuffer` with soft limit ratio of: '" + softLimitRatio + "'.");
}
this.charset = charset;
this.softLimit = softLimitRatio;
this.maxBufferSize = maxBufferSize;
this.initialBufferSize = initialBufferSize;
this.buffer = ALLOCATOR.compositeDirectBuffer(MAX_BUFFER_CHUNKS);
this.chunk = allocateChunk();
Expand All @@ -118,9 +112,9 @@ private SoyResponseBuffer(Charset charset, int initialBufferSize, int maxBufferS
*/
private ByteBuf allocateChunk() {
if (LOG.isTraceEnabled()) {
LOG.trace("Allocating chunk of sizing = i: " + initialBufferSize + ", m: " + maxBufferSize);
LOG.trace("Allocating chunk of sizing = " + initialBufferSize);
}
return ALLOCATOR.directBuffer(initialBufferSize, maxBufferSize);
return ALLOCATOR.directBuffer(initialBufferSize);
}

// -- Internals -- //
Expand Down Expand Up @@ -158,26 +152,25 @@ ByteBuf exportChunk(int maxBytes) {
return buffer.readSlice(Math.min(maxBytes, availableBytes)).asReadOnly().retain();
}

private void ensureChunkSize(int size) {
if (chunk.capacity() > (chunk.writerIndex() + size)) {
// we need to allocate a new chunk real fast
buffer.addComponent(true, chunk);
chunk = allocateChunk();
} else {
chunk.ensureWritable(size);
}
}

// -- Incoming Data -- //

@Override
public AdvisingAppendable append(CharSequence charSequence) {
int size = charSequence.length();
ensureChunkSize(size);
int available = (chunk.capacity() - chunk.writerIndex() - 1);
int balance = available - size;
if (balance < 0) {
buffer.addComponent(true, chunk);
chunk = allocateChunk();
}

// we can write it, there is either enough space or we made enough space
chunk.ensureWritable(size);
chunk.writeCharSequence(charSequence, charset);

if (LOG.isTraceEnabled()) {
LOG.trace("Appended char seq of size = " + charSequence.length() +
" (c: " + chunk.writerIndex() + ", b: " + buffer.writerIndex() + ")");
" (c: " + chunk.writerIndex() + ", b: " + buffer.writerIndex() + ")");
}
return this;
}
Expand All @@ -189,13 +182,8 @@ public AdvisingAppendable append(CharSequence csq, int start, int end) {

@Override
public AdvisingAppendable append(char c) {
ensureChunkSize(1);
char[] item = {c};
chunk.writeCharSequence(CharBuffer.wrap(item), charset);
if (LOG.isTraceEnabled()) {
LOG.trace("Appended char seq of size = 1 (c: " +
chunk.writerIndex() + ", b: " + buffer.writerIndex() + ")");
}
append(CharBuffer.wrap(item));
return this;
}

Expand All @@ -204,7 +192,7 @@ public AdvisingAppendable append(char c) {
@Override
public boolean softLimitReached() {
// have we met, or exceeded, soft-limit ratio of the buffer capacity?
return (buffer.writerIndex() >= Math.round(Math.min(maxBufferSize, MAX_CHUNK_SIZE) * softLimit));
return (buffer.writerIndex() >= Math.round(MAX_CHUNK_SIZE) * softLimit);
}

// -- Interface: Closeable -- //
Expand Down

0 comments on commit 99470af

Please sign in to comment.