-
Notifications
You must be signed in to change notification settings - Fork 119
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
[otelfiber] fix: when body is stream middleware read everything into memory first, no streaming to client #932
base: main
Are you sure you want to change the base?
Conversation
…, no streaming to client
@Alantoo could you pls provide a unittest for this case? |
@Alantoo friendly ping |
…quest/response body provided as stream
WalkthroughThe recent modifications introduce enhancements in handling request and response body sizes, particularly focusing on stream handling. This includes the introduction of a new struct for reading body stream sizes and improvements in testing capabilities for streamed response bodies by implementing a fake file interface and its concrete counterpart. Changes
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (2)
- otelfiber/fiber.go (6 hunks)
- otelfiber/otelfiber_test/fiber_test.go (2 hunks)
Additional comments: 7
otelfiber/fiber.go (5)
- 5-6: The addition of imports for
github.com/valyala/fasthttp
andio
is appropriate given the context of the changes. These imports are necessary for the new functionality introduced to handle streamed data more efficiently.- 36-55: The introduction of the
bodyStreamSizeReader
struct and itsRead
method is a key part of the solution to handle streamed data without excessive memory consumption. This implementation correctly adheres to theio.Reader
interface, ensuring compatibility and proper functionality within the Go ecosystem. The method's logic to record the histogram upon reaching EOF is a clever way to measure the streamed data size without reading it entirely into memory. However, it's important to ensure that thehistogram.Record
method is thread-safe ifRead
can be called concurrently.- 164-181: The logic to handle request bodies that are streams is correctly implemented by checking
request.IsBodyStream()
. Creating a copy of the request and setting the body stream to an instance ofbodyStreamSizeReader
is a smart approach to integrate the new streaming handling mechanism. However, it's crucial to ensure that theCopyTo
method and theSetBodyStream
method do not introduce any side effects or memory leaks, especially in long-running applications. Additionally, setting the body length to-1
inSetBodyStream
correctly indicates an unknown length, aligning with the streaming context.- 183-199: Similar to the request handling, the logic for response bodies follows the same pattern and correctly checks if the body is a stream. The use of
bodyStreamSizeReader
for the response stream and the careful handling of copying the response to avoid issues with closed streams are well thought out. Again, it's important to verify the absence of side effects or memory leaks with theCopyTo
andSetBodyStream
methods in this context as well.- 209-215: The conditional recording of request and response sizes based on whether they are streams or not is a good practice. It ensures that the middleware does not attempt to calculate the size of streamed data, aligning with the PR's objectives. This approach effectively addresses the issue of excessive memory consumption when handling large streamed data. It's essential to ensure that the metrics recorded here accurately reflect the actual sizes for non-streamed data and that the absence of size recording for streamed data is documented and understood by users of the middleware.
otelfiber/otelfiber_test/fiber_test.go (2)
- 450-483: The introduction of the
fakeFile
interface and its implementationfakeFileImpl
is a strategic addition for testing the middleware's handling of streamed response bodies. TheRead
method's logic, simulating a finite stream of data, is well-designed for testing purposes. It's important to ensure that the test adequately covers various scenarios, including partial reads and reaching EOF, to fully validate the middleware's behavior with streamed data. Additionally, resettingf.done
andf.pos
upon reaching EOF might not be necessary unless the intention is to reuse thefakeFileImpl
instance in multiple reads, which is not typical for most streaming use cases.- 485-525: The
TestStreamedResponseBody
function is a crucial addition to the test suite, ensuring that the middleware's new behavior with streamed response bodies is correctly implemented. The use offakeFileImpl
to simulate a streamed response and the validation of the recorded metrics for the response size are well-executed. It's important to ensure comprehensive coverage by including tests for various stream sizes and potential edge cases, such as empty streams or streams that trigger multiple reads. Additionally, verifying that no unexpected metrics are recorded and that the middleware's behavior remains consistent across different streaming scenarios would strengthen the test suite.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review Status
Actionable comments generated: 0
Configuration used: CodeRabbit UI
Files selected for processing (2)
- otelfiber/fiber.go (6 hunks)
- otelfiber/otelfiber_test/fiber_test.go (2 hunks)
Files skipped from review as they are similar to previous changes (2)
- otelfiber/fiber.go
- otelfiber/otelfiber_test/fiber_test.go
Hey. Sorry for long delay. Unfortunately had no time to work on it. I've pushed test to make sure response size computed in lazy way correctly. Please let me know if its fine or I should fix anything else. |
@Alantoo pls fix this |
but we still support go 1.19.x |
Bug Description
When application stream some file middleware reads all the streamed data into the memory to calculate response size which can consume all the available memory.
How to Reproduce
Steps to reproduce the behavior:
Expected Behavior
The middleware should skip response/request body size calculation when body is stream
Summary by CodeRabbit