Android Development Technology weekly 176 learning record

Source: Internet
Author: User
Tags rfc http 2 nginx server

Android Development Technology weekly 176 Learning record tutorial when OkHttp meets Http 2.0

http://fucknmb.com/2018/04/16/%E5%BD%93OkHttp%E9%81%87%E4%B8%8AHttp-2-0/

Recording

Problem: The Daisy on the app has been turned away.
Cause: okhttp3.4.1 used a closed connection under http2.0, resulting in an infinite loop. In the case of HTTP code!=200, there is no end to the dead loop.
Workaround: In the case of HTTP code!=200 stream for shutdown, the connection unexpected interrupt triggers this dead loop, and the simple and effective way to avoid the file is HTTP code no matter how much, directly shut down.

HTTP/2 protocol upgrade for Android client Network performance tuning

Https://mp.weixin.qq.com/s/N16jX2zRT3mlaJF4ixDhMg

Recording

The difference between HTTP/2 and http/1.x

    • Binary Sub-frame
      A binary sub-frame layer is added between the application layer and the transport layer in order to break through the performance limits of the HTTP1.1, improve transmission performance, and achieve low latency and high throughput without altering HTTP semantics, HTTP methods, status codes, URIs, and header fields.
      On the binary sub-frame layer, HTTP2.0 divides all the transmitted information into smaller information and frames, and encodes them in binary format.
    • Compression Head
      The header of http/1.x is easily inflated by cookies and user agents, and is repeated every time.
      HTTP2.0 uses Hpack algorithm compression to reduce the size of headers that need to be transferred, each party caches a header fields table, which avoids the repetition of headers and reduces the number of packets that need to be transmitted, thus reducing latency.
    • Multiplexing
      The client and the server can decompose the HTTP messages into non-dependent frames, then send them in random order, and then regroup them at the other end.
      On a connection, you can send countless requests at the same time, and the response can be returned at the same time.

Android clients optimized based on the HTTP/2 protocol

  • Compatible Httpdns Solutions
    Client unified interface and picture of the HTTP/2 network request adapted to the self-research Httpdns service, to avoid the operator Localdns resolution hijacking, improve the user resolution success rate, but also speed up the effective time of the server resolution switch, reduce the impact of the failure range.
    The Android native network interface HttpURLConnection does not support the HTTP/2 protocol, and the client network module can inherit the square's open source network components okhttp,okhttp form the native support HTTP/2 protocol, However, there are several issues that need to be addressed when Httpdns services are compatible:
    (1) IP after HTTP/2 protocol conversion in the URL where host is replaced with Httpdns: Authority Field correction
    The HTTP/2 protocol RFC document uses the following: Authority field instead of the host field in the http/1.x header, Okhttp does not make the conversion between two fields before the V3.6.0 version, which causes the Nginx server not to get the correct authority field and cannot implement the request forwarding.
    Network module access Httpdns replace the host field in the request URL with the IP under Httpdns, and then add the Host field in the header to ensure that the background Nginx server can correctly forward the request.
    (2) URL in which host is replaced with HTTPDNS IP after SSL/TLS handshake initial SNI field correction
    SNI (server Name Indication) is a SSL/TLS extension that addresses a server that uses multiple domain names and certificates. It works by sending the domain name (Hostname) to access the site before connecting to the server to establish the SSL/TLS connection so that the server returns a suitable certificate based on the domain name.
    Since SNI's delivery process is based on the Transport Layer (TCP) layer, if the header field based on the HTTP protocol layer does not take effect, the SNI transfer process of okhttp is reformed to pass the SNI parameters from the application layer to the transport layer. This solves the problem that sni resolves from an existing URL to an unrecognized service side.
    (3) HTTP/2 long connection resume handshake process of network domain name check problem correction
    Domain name verification is an important part of verifying the validity of the certificate in the process of HTTPS handshake, in order to verify the legitimacy of the certificate authority under the service side, if simply ignoring the domain name verification, will give the middleman an opportunity.
    Okhttp handshake Process Host is resolved from the URL, if the URL in the host is replaced by the Hostdns IP address, the HTTPS handshake verification process will fail, here is also through the Okhttp transfer layer handshake process to transform, through the correct host field, This scenario is suitable for Httpdns IP direct connection.

  • client HTTP/2 long connection management optimization
    problem: The app has a long time in the background to switch to the foreground or the phone after a long sleep wake-up app has a load timeout phenomenon.
    analysis: the client needs to maintain a regular network long connection link, while the Android version of the iteration process as the system's power consumption and traffic management more stringent, in hibernation or idle process at the system level to shut down the network connection, Failed to notify the application tier that the connection has been disconnected, causing network requests to time out, timeout requests waiting too long, user experience is very poor.
    improved: In order to improve the request timeout after long connection loss, the client has added a ping frame heartbeat mechanism for okhttp, the process is as follows:
    (1) client based on http/ 2 Protocol for network communication, HTTP/2 from the bottom to achieve multiplexing, multiple requests to share a link to request, when the client is idle, the link remains alive.
    (2) to implement a fixed interval to send ping frames to the server, if the server can return the results within a specified time, then continue the next round of heartbeat.
    (3) after a total heartbeat n (n is the number of heartbeats sent by the server), if it is still idle, send the Goaway frame to the server to release the connection and then release the local connection.
    (4) If a heartbeat timeout is detected during the n heartbeat cycle, the local connection is released directly and the request is re-initiated the next time the request is requested.

  • SSL/TLS handshake process depth optimization
    Existing HTTPS,HTTP/2 protocols require a connection, and each new TLS connection requires a handshake to create a shared encryption key, which requires two additional round-trips (7 handshake) on top of the standard TCP handshake, which can cause delays in request efficiency. TLS has several features that can be used to eliminate additional back-and-forth, such as reusing session sessions, two standard session reuse mechanisms are session IDs (RFC 5246) and session tickets (RFC 5077), using one of the techniques, A client can reuse a previously created session, which is preceded by a successful handshake with the server, which reduces the time-to-back process.
    The example client supports the TLS session tickets reuse mechanism, session tickets is encrypted with a security key known only to the server, and is eventually saved to the mobile terminal. Mobile TLS handshake with session tickets information, as long as the server can successfully decrypt the fast handshake can be completed.
  • Quality control of user access and optimization of network disaster tolerance
    optimization of access quality monitoring and disaster resilience:
    (1) Considering the increase in the number of online users, may lead to excessive performance pressure on the service side, the client increased the online switch control HTTP/2 protocol on the state; In the event of an abnormality, the switch rollback can be adjusted in time.
    (2) To increase the HTTP/2 after opening the abnormal buried point report, if there are anomalies, can be detected through the monitoring platform timely problems, and query the problem of the user's exception details.
    (3) When the HTTP/2 request exception occurs, the client initiates a downgrade retry mechanism, which includes the policy of retry, protocol demotion, built-in extraction, only routing, etc. to enhance the client network disaster tolerance.
    (4) In the application to increase the network-based statistical data monitoring, development and operations personnel can fully monitor network performance.

Android Development Technology weekly 176 learning record

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.