Skip to content

'Client.Endpoint()' returns 'context canceled' while reading the Body #773

@xescugc

Description

@xescugc

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) while NewClient 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 the NewClient except if the body is longer than 3966 which then stops reading from the body.

This is my decode*:

func decode(_ context.Context, r *http.Response) (interface{}, error) {
        pr, pw := io.Pipe()

        go func() {
                defer r.Body.Close()
                defer pw.Close()
                i, err := io.Copy(pw, r.Body)
                fmt.Println(i, err)
        }()
        // IOR is a io.Reader
        return response{IOR: pr}, nil
}

The intention is to only read from the r.Body when I need it, that's why I'm using a io.Pipe, and also because I have to r.Body.Close() when it ends (if not, just passing the r.Body to the response.IOR would work, but then the r.Body wold not closed, no?).

This code works for "small" r.Body but when the it's bigger than 3966 the io.Copy stops and the output form the print is 3966 context canceled. So basically the

defer cancel()
is called and the body lost.

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 the r.Body.Close() is called as it's I have the BufferedStream option no? IDK if that's even possible haha.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions