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

feat(handler): implement duplicate read closer for request body #116

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ch3nnn
Copy link

@ch3nnn ch3nnn commented Jul 22, 2024

This change introduces a utility function dupReadCloser that allows duplicating the incoming request body reader. It is now used in the request handling loop to ensure that the request body can be consumed by multiple handlers or middleware without losing the original data.

This change introduces a utility function `dupReadCloser` that allows
duplicating the incoming request body reader. It is now used in the
request handling loop to ensure that the request body can be consumed
by multiple handlers or middleware without losing the original data.
@coveralls
Copy link

coveralls commented Jul 22, 2024

Pull Request Test Coverage Report for Build 10055591097

Details

  • 14 of 17 (82.35%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.2%) to 91.146%

Changes Missing Coverage Covered Lines Changed/Added Lines %
handler.go 14 17 82.35%
Totals Coverage Status
Change from base Build 10029513275: -0.2%
Covered Lines: 803
Relevant Lines: 881

💛 - Coveralls

@earthboundkid
Copy link
Owner

earthboundkid commented Jul 22, 2024

This is a useful feature, but it should not be on by default because it uses too much memory. Instead I think it should look like:

handler := requests.KeepResponseBody(
 	requests.ToJSON(&common),
 	requests.ToJSON(&book),
 	requests.ToString(&str),
 )

What do you think of that?

@ch3nnn
Copy link
Author

ch3nnn commented Jul 23, 2024

This is a useful feature, but it should not be on by default because it uses too much memory. Instead I think it should look like:

handler := requests.KeepResponseBody(
 	requests.ToJSON(&common),
 	requests.ToJSON(&book),
 	requests.ToString(&str),
 )

What do you think of that?

i think KeepRespBodyHandlers more suitable.
help to review new commit. thank!

}

var dup io.ReadCloser
r.Body, dup = dupReadCloser(r.Body)
Copy link
Owner

Choose a reason for hiding this comment

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

I'm not sure, but I think if one of the handlers only consumed part of the response, the subsequent handlers would only get a partial buffer.

Instead, what if on a new line between line 35 and line 36, it read the whole body into a buffer and reset the buffer on each loop?

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.

3 participants