consumeAvailable should use number of reads instead of bytes

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2023-02-01 14:59:29 +11:00
parent 02823ecfad
commit d66e36b746
2 changed files with 15 additions and 15 deletions

View File

@ -79,7 +79,7 @@ public class HttpConfiguration implements Dumpable
private boolean _relativeRedirectAllowed;
private HostPort _serverAuthority;
private SocketAddress _localAddress;
private long _maxUnconsumedRequestContentBytes = 1024 * 1024;
private int _maxUnconsumedRequestContentReads = 16;
/**
* <p>An interface that allows a request object to be customized
@ -718,25 +718,25 @@ public class HttpConfiguration implements Dumpable
}
/**
* Sets the maximum amount of bytes that will be read from the HttpStream if the content is not fully consumed by the
* application. If this is unable to consume to EOF then the connection will be made non-persistent.
* Sets the maximum amount of {@link HttpStream#read()}s that can be done by the {@link HttpStream} if the content is not
* fully consumed by the application. If this is unable to consume to EOF then the connection will be made non-persistent.
*
* @param maxUnconsumedRequestContentBytes the maximum amount of unconsumed bytes that will be read from the HttpStream.
* @param maxUnconsumedRequestContentReads the maximum amount of reads for unconsumed content or -1 for unlimited.
*/
public void setMaxUnconsumedRequestContentBytes(long maxUnconsumedRequestContentBytes)
public void setMaxUnconsumedRequestContentReads(int maxUnconsumedRequestContentReads)
{
_maxUnconsumedRequestContentBytes = maxUnconsumedRequestContentBytes;
_maxUnconsumedRequestContentReads = maxUnconsumedRequestContentReads;
}
/**
* Gets the maximum amount of bytes that will be read from the HttpStream if the content is not fully consumed by the
* application. If this is unable to consume to EOF then the connection will be made non-persistent.
* Gets the maximum amount of {@link HttpStream#read()}s that can be done by the {@link HttpStream} if the content is not
* fully consumed by the application. If this is unable to consume to EOF then the connection will be made non-persistent.
*
* @return the maximum amount of unconsumed bytes that will be read from the HttpStream.
* @return the maximum amount of reads for unconsumed content or -1 for unlimited.
*/
public long getMaxUnconsumedRequestContentBytes()
public int getMaxUnconsumedRequestContentReads()
{
return _maxUnconsumedRequestContentBytes;
return _maxUnconsumedRequestContentReads;
}
@Override

View File

@ -108,19 +108,19 @@ public interface HttpStream extends Callback
static Throwable consumeAvailable(HttpStream stream, HttpConfiguration httpConfig)
{
long consumedRequestContentBytes = 0;
long maxUnconsumedRequestContentBytes = httpConfig.getMaxUnconsumedRequestContentBytes();
while (maxUnconsumedRequestContentBytes < 0 || consumedRequestContentBytes < maxUnconsumedRequestContentBytes)
int numReads = 0;
int maxReads = httpConfig.getMaxUnconsumedRequestContentReads();
while (maxReads < 0 || numReads < maxReads)
{
// We can always just read again here as EOF and Error content will be persistently returned.
Chunk content = stream.read();
numReads++;
// if we cannot read to EOF then fail the stream rather than wait for unconsumed content
if (content == null)
return new IOException("Content not consumed");
// Always release any returned content. This is a noop for EOF and Error content.
consumedRequestContentBytes += content.remaining();
content.release();
// if the input failed, then fail the stream for same reason