HTTP/2 Service-side push

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Translation: shihuaping0918@163.com

"Translator Note: This article needs to have certain HTTP protocol knowledge and browser parsing page process knowledge"

Original: Https://blog.golang.org/h2push

Objective

HTTP/2 is designed to solve many of http/1.x's flaws. Contemporary Web pages use a lot of resources: HTML, stylesheets, scripts, pictures, and so on. Each of these resources must be explicitly requested in the http/1.x. This can be a slow process. The browser starts with getting HTML, and then incrementally gets more resources when it parses and evaluates the page. Because the server must wait for the browser to make every request, the network is often idle and underutilized.

To improve latency, HTTP/2 introduced the server push, which allows servers to push resources to the browser before the browser explicitly requests it. A server often knows that a page requires a lot of additional resources, and when it responds to the browser's first request, it can start pushing these resources. This allows the server to fully utilize a potentially idle network to improve page load times.


Serverpush.svg.png

At the protocol layer, HTTP/2 server push is driven by push_promise frames, and a push_promise describes a request that the server expects the browser to make a request immediately. Once the browser receives the push_promise, it immediately knows that the server will be transmitting this resource. If the browser later discovers that it needs this resource, it waits for the push to complete instead of sending a new request. This reduces the amount of time the browser spends waiting on the network.

Service-side push in the Net/http package

go1.8 introduces support for push responses from Http.server. This feature is available if the server that is running is a HTTP/2 service and the incoming connection is using HTTP/2. In any HTTP handler, you can judge http. Whether the Responsewriter supports server-side push, by checking whether it implements the new HTTP. Pusher interface.

For example, if the server knows that App.js is going to be requested to render the page, handler can initialize a push if Http.pusher is available.

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {        if pusher, ok := w.(http.Pusher); ok {            // Push is supported.            if err := pusher.Push("/app.js", nil); err != nil {                log.Printf("Failed to push: %v", err)            }        }        // ...    })

Because of the time relationship, this evening first translated to this ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.