Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #5048 - Review temporary buffer usage #5091

Closed

Conversation

lachlan-roberts
Copy link
Contributor

Implementations of HttpContent now use the servers ByteBufferPool to allocate temporary buffers for resources.
Any allocated buffers are released back to the ByteBufferPool when the HttpContentrelease() method is called.

Avoid using direct buffers before calling BufferUtil.writeTo().

- avoid readOnly buffers if possible for access to the array
- other cleanups to related code

Signed-off-by: Lachlan Roberts <[email protected]>
@joakime joakime linked an issue Jul 30, 2020 that may be closed by this pull request
Copy link
Contributor

@gregw gregw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly what I was thinking. Let's hangout.

return equalsIgnoreCase(_encoding, ccf._encoding) && equalsIgnoreCase(_extension, ccf._extension);
}

private static boolean equalsIgnoreCase(String a, String b)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there a StringUtil.equalsIgnoreCase method? If not, this method should go there.

@@ -156,7 +151,26 @@ public ByteBuffer getIndirectBuffer()
return null;
try
{
return BufferUtil.toBuffer(_resource, false);
ByteBuffer byteBuffer = ByteBufferPoolUtil.resourceToBuffer(_resource, false, _bufferPool);
_allocatedBuffers.add(byteBuffer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the lifecycle of a ResourceHttpContent? Can it be put into a cache? If so, could getIndirectBuffer be called again and again, and the list of allocated buffers get longer and longer with release never called or called in the distant future? Is release always called?

I'd be perhaps more included to have a ByteBuffer getIndirectBuffer(ByteBufferPool pool) method that the caller can pass in their pool and then be responsible for doing the release themselves.

{
try (InputStream is = resource.getInputStream())
{
BufferUtil.readFrom(is, ilen, buffer);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is the temporary buffers like the one used inside readFrom that this issue was initially raised about.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also I think we can use our context to use bigger temp buffers inside readFrom

@@ -674,10 +674,10 @@ protected boolean sendData(HttpServletRequest request,
content.getResource().writeTo(out, 0, content_length);
}
// else if we can't do a bypass write because of wrapping
else if (written || !(out instanceof HttpOutput))
else if (written)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was the instanceof test removed? The response object may be wrapped!

Signed-off-by: Lachlan Roberts <[email protected]>
@lachlan-roberts lachlan-roberts marked this pull request as draft August 6, 2020 10:54
@@ -237,7 +255,7 @@ private HttpContent load(String pathInContext, Resource resource, int maxBufferS
{
String compressedPathInContext = pathInContext + format._extension;
CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
if (compressedContent == null || compressedContent.isValid())
if (compressedContent == null || !compressedContent.isValid())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why was this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We only want to get the resource from the factory again if it is null or not valid.
The code doesn't make much sense the way it was, I think it was a mistake and someone forgot the !.

@lachlan-roberts lachlan-roberts deleted the jetty-9.4.x-5048-TempBufferReview branch May 14, 2021 00:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Review temporary buffer usage
3 participants