Improve the HttpPostRequestDecoder and so make it configurable when it will discard read bytes to prevent OOM

This commit is contained in:
Norman Maurer 2013-07-17 15:00:43 +02:00
parent e53738f38c
commit 939fd8d17e

View File

@ -46,6 +46,8 @@ import static io.netty.buffer.Unpooled.*;
*
*/
public class HttpPostRequestDecoder {
private static final int DEFAULT_DISCARD_THRESHOLD = 10 * 1024 * 1024;
/**
* Factory used to create InterfaceHttpData
*/
@ -130,6 +132,8 @@ public class HttpPostRequestDecoder {
private boolean destroyed;
private int discardThreshold = DEFAULT_DISCARD_THRESHOLD;
/**
*
* @param request
@ -293,6 +297,25 @@ public class HttpPostRequestDecoder {
return isMultipart;
}
/**
* Set the amount of bytes after which read bytes in the buffer should be discarded.
* Setting this lower gives lower memory usage but with the overhead of more memory copies.
* Use {@code 0} to disable it.
*/
public void setDiscardThreshold(int discardThreshold) {
if (discardThreshold < 0) {
throw new IllegalArgumentException("discardThreshold must be >= 0");
}
this.discardThreshold = discardThreshold;
}
/**
* Return the threshold in bytes after which read data in the buffer should be discarded.
*/
public int getDiscardThreshold() {
return discardThreshold;
}
/**
* This getMethod returns a List of all HttpDatas from body.<br>
*
@ -372,19 +395,19 @@ public class HttpPostRequestDecoder {
// Maybe we should better not copy here for performance reasons but this will need
// more care by teh caller to release the content in a correct manner later
// So maybe something to optimize on a later stage
ByteBuf chunked = content.content().copy();
ByteBuf buf = content.content();
if (undecodedChunk == null) {
undecodedChunk = chunked;
undecodedChunk = buf.copy();
} else {
// undecodedChunk = ByteBufs.wrappedBuffer(undecodedChunk,
// chunk.getContent());
// less memory usage
undecodedChunk = wrappedBuffer(undecodedChunk, chunked);
undecodedChunk.writeBytes(buf);
}
if (content instanceof LastHttpContent) {
isLastChunk = true;
}
parseBody();
if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
undecodedChunk.discardReadBytes();
}
return this;
}