HTTP/2 multiplexing

1003 views
Skip to first unread message

pala.d...@gmail.com

unread,
Jul 13, 2017, 10:56:27 AM7/13/17
to golang-nuts
Hello, I'm trying to understand how to write an HTTP client using the standard HTTP package and which takes advantage of HTTP/2 multiplexing (i.e. send many requests "in parallel" using the same TCP connection).

One thing that it's not clear to me is that it seems like TCP connection reuse requires closing the response body of the previous request, but with HTTP/2 multiplexing I should be able to send a new request before even receiving a response to the prevoius ones...

Best regards
Daniele

James Abley

unread,
Jul 30, 2017, 6:08:08 PM7/30/17
to golang-nuts
HTTP/2 support was added in Go 1.6. Can you provide a code example which demonstrates the behaviour that has puzzled you?

pala.d...@gmail.com

unread,
Aug 2, 2017, 3:56:27 AM8/2/17
to golang-nuts
Hi, thanks a lot for your reply. An example to illustrate my question: I want to send a sequence of POSTs to an HTTP server:

        resp, e := c.Post(...) // c is an http.Client
        if e != nil {
                log.Fatal(e)
        }
        defer resp.Body.Close()

I can put this code inside a for loop. However, the calls will be sequential. So, I can put this code inside a function and call it many times with the 'go' keyword (always passing the same http.Client as input).
Now, what I see with Wireshark is that the "sequential" version actually re-uses the same TCP connection, while the "parallel" version uses different TCP connections, so I was wondering how can I send many POST request in parallel on the same TCP connection?

BR
Daniele

Tamás Gulácsi

unread,
Aug 2, 2017, 1:38:00 PM8/2/17
to golang-nuts
Parallel, on the same connection? How?
Which byte shall be sent next?
TCP is essentially sequential, sorry.

pala.d...@gmail.com

unread,
Aug 2, 2017, 1:57:48 PM8/2/17
to golang-nuts
Parallel in the sense of HTTP/2 multiplexing, i.e. sending the next request without having to wait for the reply of the previous one.

James Abley

unread,
Aug 2, 2017, 5:34:42 PM8/2/17
to pala.d...@gmail.com, golang-nuts
A goroutine making each request, passing in the http.Client should work.


On Wed, 2 Aug 2017 at 18:58 <pala.d...@gmail.com> wrote:
Parallel in the sense of HTTP/2 multiplexing, i.e. sending the next request without having to wait for the reply of the previous one.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/5T5aiDRl_cw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

pala.d...@gmail.com

unread,
Aug 3, 2017, 3:46:01 AM8/3/17
to golang-nuts, pala.d...@gmail.com
Yes I tried this solution, but looking at the generated traffic with Wireshark I see that different TCP connections are used for the requests, while I'd like to reuse the same TCP connection for all the requests...

Henrik Johansson

unread,
Aug 3, 2017, 3:54:14 AM8/3/17
to pala.d...@gmail.com, golang-nuts
What is your target service you are calling? Is it available online?


tors 3 aug. 2017 kl 09:46 skrev <pala.d...@gmail.com>:

pala.d...@gmail.com

unread,
Aug 3, 2017, 5:07:50 AM8/3/17
to golang-nuts, pala.d...@gmail.com
Example code with DuckDuckGo as default target:

https://play.golang.org/p/_iI5-MDJ5t

Christian Joergensen

unread,
Aug 3, 2017, 5:21:18 AM8/3/17
to golang-nuts, pala.d...@gmail.com
On Thursday, August 3, 2017 at 11:07:50 AM UTC+2, pala.d...@gmail.com wrote:
Example code with DuckDuckGo as default target:

https://play.golang.org/p/_iI5-MDJ5t

This looks like a thundering herd race on connection setup. So there is no connection to reuse as none of them has been setup yet.

Try completing a single request and then run the rest of the requests in parallel afterwards.

Cheers,

Christian

pala.d...@gmail.com

unread,
Aug 3, 2017, 5:34:57 AM8/3/17
to golang-nuts, pala.d...@gmail.com
Works, thanks a lot!!!
Also found this related discussion: https://github.com/golang/go/issues/13397

BR
Daniele

Henrik Johansson

unread,
Aug 3, 2017, 8:21:57 AM8/3/17
to pala.d...@gmail.com, golang-nuts
Out of curiosity do you do something like this in wireshark?

ip.dst_host=="duckduckgo.com" && tcp.flags.syn == 0

My wireshark fu is really weak...

pala.d...@gmail.com

unread,
Aug 3, 2017, 9:20:20 AM8/3/17
to golang-nuts, pala.d...@gmail.com
Mine is even weaker...in fact I was just filtering based on my IP address and SSL protocol, like this:

        ip.addr == xx.xx.xx.xx && ssl

of course I had to ensure that there were no other ssl conversation going on on my machine...also, in this way the TCP packets are hidden, you just see SSL stuff. But then you can easily see the TCP ports used by opening the "Conversations" dialog (menu Statistics->Conversations).

Daniele
Reply all
Reply to author
Forward
0 new messages