Improve the HttpPostRequestDecoder and so make it configurable when it will discard read bytes to prevent OOM
This commit is contained in:
parent
e53738f38c
commit
939fd8d17e
@ -46,6 +46,8 @@ import static io.netty.buffer.Unpooled.*;
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class HttpPostRequestDecoder {
|
public class HttpPostRequestDecoder {
|
||||||
|
private static final int DEFAULT_DISCARD_THRESHOLD = 10 * 1024 * 1024;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Factory used to create InterfaceHttpData
|
* Factory used to create InterfaceHttpData
|
||||||
*/
|
*/
|
||||||
@ -130,6 +132,8 @@ public class HttpPostRequestDecoder {
|
|||||||
|
|
||||||
private boolean destroyed;
|
private boolean destroyed;
|
||||||
|
|
||||||
|
private int discardThreshold = DEFAULT_DISCARD_THRESHOLD;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param request
|
* @param request
|
||||||
@ -293,6 +297,25 @@ public class HttpPostRequestDecoder {
|
|||||||
return isMultipart;
|
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>
|
* 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
|
// 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
|
// more care by teh caller to release the content in a correct manner later
|
||||||
// So maybe something to optimize on a later stage
|
// So maybe something to optimize on a later stage
|
||||||
ByteBuf chunked = content.content().copy();
|
ByteBuf buf = content.content();
|
||||||
if (undecodedChunk == null) {
|
if (undecodedChunk == null) {
|
||||||
undecodedChunk = chunked;
|
undecodedChunk = buf.copy();
|
||||||
} else {
|
} else {
|
||||||
// undecodedChunk = ByteBufs.wrappedBuffer(undecodedChunk,
|
undecodedChunk.writeBytes(buf);
|
||||||
// chunk.getContent());
|
|
||||||
// less memory usage
|
|
||||||
undecodedChunk = wrappedBuffer(undecodedChunk, chunked);
|
|
||||||
}
|
}
|
||||||
if (content instanceof LastHttpContent) {
|
if (content instanceof LastHttpContent) {
|
||||||
isLastChunk = true;
|
isLastChunk = true;
|
||||||
}
|
}
|
||||||
parseBody();
|
parseBody();
|
||||||
|
if (undecodedChunk != null && undecodedChunk.writerIndex() > discardThreshold) {
|
||||||
|
undecodedChunk.discardReadBytes();
|
||||||
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user