FAQ
Hello every body,
Currently, I am confronting to strange problem with reuse tcp connection.
The problem is describe in more detail here
https://github.com/go-kit/kit/issues/249

In the nutshell:
when I use httpclient with server that send me response, the httpclient
closes the tcp connection
except if I set the classic defer io.Copy(ioutil.Discard, r.Body) (as
describe here
https://groups.google.com/forum/#!search/golang$20json$20decode$20$20io.Copy/golang-nuts/4Rr8BYVKrAI/ZrJJFTNleekJ)
Normally I just read the response with
json.NewDecoder(response.Body).Decode(&something)

The trace from wireshark:

GET /api/v1/state HTTP/1.1
Host: 10.130.145.94:8401
User-Agent: Go-http-client/1.1
Accept-Encoding: gzip
Connection: keep-alive

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Wed, 04 May 2016 09:05:12 GMT

1b
{"state":"OK","message":""}




But this io.Copy doesn't read nothing because:

defer func() {
     b := new(bytes.Buffer)
     io.Copy(b, response.Body)

     fmt.Println("--------------->", b.Len())
     fmt.Println(hex.Dump(b.Bytes()))
     fmt.Println("###############>")
}()


log

---------------> 0

###############>


My current comprehension, that is json decoder doesn't read the last 0 that
indicates the end of chunked response.
or do I miss something?
Do I have to put defer io.Copy(ioutil.Discard, r.Body)?

Thx in adv
Jérôme

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Peter Waller at May 4, 2016 at 11:27 am
    I think you've hit an unfortunate gotcha with encoding/json. Decode and
    Encode aren't exact mirrors of each other.

    Encode writes a trailing newline, but decode doesn't read this newline:

    https://github.com/golang/go/blob/f78a4c84ac8ed44aaf331989aa32e40081fd8f13/src/encoding/json/stream.go#L193


    I became aware of this via a nasty docker bug:

    https://github.com/docker/docker/issues/14203


    I can't find any other discussion about the problem on this list, hopefully
    others can chime in with references to why this decision was made.

    As far as I can see, your solution to use ioutil.Discard is correct and
    reasonable.

    A strict reading of the documentation supports the current behaviour:

    https://golang.org/pkg/encoding/json/#Encoder.Encode
    Encode writes the JSON encoding of v to the stream, followed by a newline
    character.
    https://golang.org/pkg/encoding/json/#Decoder.Decode
    Decode reads the next JSON-encoded value from its input and stores it in
    the value pointed to by v.

    Note that decode makes no promise to consume the extra newline.

    On 4 May 2016 at 10:41, Jérôme LAFORGE wrote:

    Normally I just read the response with
    json.NewDecoder(response.Body).Decode(&something)
    My current comprehension, that is json decoder doesn't read the last 0
    that indicates the end of chunked response.
    or do I miss something?
    >

    Do I have to put defer io.Copy(ioutil.Discard, r.Body)?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.
  • Jérôme LAFORGE at May 17, 2016 at 6:38 am
    FYI, I have opened this issue https://github.com/golang/go/issues/15703
    against Go net/http.

    Le mercredi 4 mai 2016 13:27:43 UTC+2, Peter Waller a écrit :
    I think you've hit an unfortunate gotcha with encoding/json. Decode and
    Encode aren't exact mirrors of each other.

    Encode writes a trailing newline, but decode doesn't read this newline:


    https://github.com/golang/go/blob/f78a4c84ac8ed44aaf331989aa32e40081fd8f13/src/encoding/json/stream.go#L193


    I became aware of this via a nasty docker bug:

    https://github.com/docker/docker/issues/14203


    I can't find any other discussion about the problem on this list,
    hopefully others can chime in with references to why this decision was made.

    As far as I can see, your solution to use ioutil.Discard is correct and
    reasonable.

    A strict reading of the documentation supports the current behaviour:

    https://golang.org/pkg/encoding/json/#Encoder.Encode
    Encode writes the JSON encoding of v to the stream, followed by a newline
    character.
    https://golang.org/pkg/encoding/json/#Decoder.Decode
    Decode reads the next JSON-encoded value from its input and stores it in
    the value pointed to by v.

    Note that decode makes no promise to consume the extra newline.


    On 4 May 2016 at 10:41, Jérôme LAFORGE <[email protected] <javascript:>
    wrote:

    Normally I just read the response with
    json.NewDecoder(response.Body).Decode(&something)
    My current comprehension, that is json decoder doesn't read the last 0
    that indicates the end of chunked response.
    or do I miss something?

    Do I have to put defer io.Copy(ioutil.Discard, r.Body)?
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 4, '16 at 9:41a
activeMay 17, '16 at 6:38a
posts3
users2
websitegolang.org

2 users in discussion

Jérôme LAFORGE: 2 posts Peter Waller: 1 post

People

Translate

site design / logo © 2023 Grokbase