-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
'Client.Endpoint()' returns 'context canceled' while reading the Body #773
Comments
So I've refactored the code to maybe be more clean, and instead of using a But now the error is random 👍 . I know that it's due to that I would like to know how to read from a Ideally the That's what I think it's happening, but I'm not 100% sure. |
Ok, so I had some reading on the topic https://medium.com/the-software-firehose/canceling-requests-in-go-using-context-b4b28a118da8 and https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/ and I think that my assumptions where correct, if the So then I tried to reproduce it on the
And some times (IDK why If I change the sleep time or something it switches from one to the other):
Removing the logic for cancelling (just for testing purposes) it returns another error:
Which makes sense with the
And if I repeat https://github.com/go-kit/kit/blob/master/transport/http/client_test.go#L135 again it works (the read bytes of the first one is all of them So now that I know that this is an error, I tried to find a solution for it. First of all I did try to find a way to know when the The idea would be something like this: type bodyWithCancel struct {
io.ReadCloser
cancel context.CancelFunc
}
func (bwc bodyWithCancel) Close() error {
bwc.ReadCloser.Close()
bwc.cancel()
return nil
} In terms of use this would mean that when the In terms of code it would mean that on the if c.bufferedStream {
resp.Body = bodyWithCancel{ReadCloser: resp.Body, cancel: cancel}
} else {
defer resp.Body.Close()
} I'm commenting all this, because IDK if it may have potential side effects that I'm not aware of, or if it's a good solution (or wanted solution). So I prefer to comment it before opening a PR. Maybe there is another solution for the problem but I did not see any other 😢 |
@peterbourgon WDYT about this? Does this make sense? |
It makes rough sense to me when typed out. Can you make a proposed PR? |
Yes ofc! It was to know if It was ok to continue with it (open PR) or I had something wrong. |
Hello! I have a question which IDK if it's a missus on my part and there is another way to do it.
I'm writing a client which consumes a MS that returns binary data. I'm using the
kithttp.BufferedStream(true)
whileNewClient
to make sure the body is not closed so I can read it when I want and not have it buffered.This works fine with my implementation of the
decode*
that I use on theNewClient
except if the body is longer than3966
which then stops reading from the body.This is my
decode*
:The intention is to only read from the
r.Body
when I need it, that's why I'm using aio.Pipe
, and also because I have tor.Body.Close()
when it ends (if not, just passing ther.Body
to theresponse.IOR
would work, but then ther.Body
wold not closed, no?).This code works for "small"
r.Body
but when the it's bigger than3966
theio.Copy
stops and the output form the print is3966 context canceled
. So basically thekit/transport/http/client.go
Line 95 in 4914a69
Maybe I'm overcomplicating it using the
io.Pipe
but I thought it was the best solution for what I had in mind.Maybe the
defer cancel()
should be called once ther.Body.Close()
is called as it's I have theBufferedStream
option no? IDK if that's even possible haha.Thanks!
The text was updated successfully, but these errors were encountered: