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

Add Request.WithLimit #271

Open
gavv opened this issue Feb 2, 2023 · 10 comments
Open

Add Request.WithLimit #271

gavv opened this issue Feb 2, 2023 · 10 comments
Assignees
Labels
feature New feature or request help wanted Contributions are welcome waiting reply Waiting for reply from reporter or contributor

Comments

@gavv
Copy link
Owner

gavv commented Feb 2, 2023

Add new method Request.WithLimit that set's a limit for maximum number of bytes that can be retrieved from response body.

It can be helpful to interrupt early on abnormally large response (e.g. mistakenly trying to read infinite chunked response into memory), thus avoiding insane memory consumption by tests, swapping, etc.

Request.WithLimit should store the provided limit into a private field of Request. Later, in Request.retryRequest we should check if limit is set, and if so, pass this limit to bodyWrapper when we're wrapping response body.

Then, we should teach bodyWrapper to handle the limit. When the limit is set and bodyWrapper detected that it read more bytes than desired, it should abort read and return error that explains that limit is exceed and what is the limit.

Ideally error message should contain limit both in bytes and in human readable form, like "5 MB".

New method should have documentation comment with code example and two tests:

  • unit test for request (request_test.go)
  • end-to-end (blackbox) test (we can create new file e2e_limit_test.go)
@gavv gavv added feature New feature or request help wanted Contributions are welcome labels Feb 2, 2023
@Rohith-Raju
Copy link
Contributor

Rohith-Raju commented Feb 27, 2023

Hey @gavv, I was looking into this issue. We can use this
MaxBytesReader.
Here's an example of how it can be implemented

https://go.dev/play/p/BJKXGLnwiL5

Then, we should teach bodyWrapper to handle the limit. When the limit is set and bodyWrapper detected that it read more bytes than desired, it should abort read and return error that explains that limit is exceed and what is the limit.

Even if the response exceeds the max size, we can return data(resp) until the max and return error/warning after reading the response. Are we separating functionality where we just report an error when it exceeds or return the data and error?

I'd like to give this a go

@gavv
Copy link
Owner Author

gavv commented Feb 27, 2023

Hi,

Even if the response exceeds the max size, we can return data(resp) until the max and return error/warning after reading the response.

This sounds good, I was thinking about the same behavior.

We can use this MaxBytesReader.

Using MaxBytesReader is nice idea.

On the other hand, there are two thoughts why I think it's better to implement the check manually in bodyWrapper:

  • we already have one hidden wrapper (not reflected in Body type), using two nested hidden wrapper will make it even more confusing; I think it would be nice to keep all "wrapping" stuff in one place

  • to produce a nice error when limit exceeded, we anyway should catch error from MaxBytesReader and convert it to our own; so what we really get from MaxBytesReader is only counting bytes, but it's actually easy to implement

BTW, there is another issue related to bodyWrapper: #245

@gavv
Copy link
Owner Author

gavv commented Feb 27, 2023

I'd like to give this a go

Would you like to be assigned?

@Rohith-Raju
Copy link
Contributor

Yes @gavv

@gavv
Copy link
Owner Author

gavv commented Mar 22, 2023

Postponing this issue until #342 is merged. See #321 (comment)

@aliziyacevik
Copy link
Contributor

Hey @gavv, I can take this one if it's available.

@gavv
Copy link
Owner Author

gavv commented Apr 6, 2023

@aliziyacevik #342 was just merged today, so yes, you're welcome.

In the new implementation, I think it should be enough to modify httpReadNext and httpReadFull methods (but please re-check).

@gavv
Copy link
Owner Author

gavv commented Oct 1, 2023

@aliziyacevik Hi, do you still have plans on the issue?

@gavv gavv added the waiting reply Waiting for reply from reporter or contributor label Oct 1, 2023
@palakg11
Copy link

Hi @gavv, if no one is working on this issue, Can I take this up?

@kendriyavid
Copy link

Hi @gavv can I work on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request help wanted Contributions are welcome waiting reply Waiting for reply from reporter or contributor
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants