Detailed explanation of the principle of okhttp in Android

table of Contents

1. The general process of okhttp work
2. The connection pool in okhttp
3. Reference connection

 

The bottom layer of OkHttp3 is Socket, not URLConnection. It obtains the socket library used by the current Runtime through the reflection of Platform's Class.forName()

The process of socket initiating a network request is generally:
(1). Create a socket object;
(2). Connect to the target network;

(3). Perform input and output stream operations.

(1) The realization of (2) is encapsulated in the connection interface, and the specific implementation class is RealConnection.
(3) It is implemented through the stream interface. According to different network protocols, there are two implementation classes, Http1xStream and Http2xStream.
Because the time to create a network connection is relatively long (if it is HTTP, a three-way handshake is required), and requests are often frequent Fragmentation, so in order to improve the efficiency of network connections, OKHttp3 realizes network connection reuse
 

1. The general process of okhttp work

1.1, the overall process

(1) When we create a Call through OkhttpClient and initiate a synchronous or asynchronous request;
(2) Okhttp will use Dispatcher to manage all of our RealCall (the specific implementation class of Call) in a unified way, and through execute() and The enqueue() method processes synchronous or asynchronous requests;
(3), execute() and enqueue() these two methods will eventually call the getResponseWithInterceptorChain() method in RealCall to obtain the return result from the interceptor chain;
(4) , In the interceptor chain, the request is processed in turn through RetryAndFollowUpInterceptor (redirection interceptor), BridgeInterceptor (bridge interceptor), CacheInterceptor (cache interceptor), ConnectInterceptor (connection interceptor), CallServerInterceptor (network interceptor), and service After the connection is established, the returned data is obtained, and then processed by the above interceptor in turn, and finally the result is returned to the caller.
Two pictures are provided for easy understanding and memorization:


okhttp overall process 1 okhttp overall process 2

This picture only shows the request process, there is no data return process, it will be processed later

1.2.4, ConnectInterceptor: responsible for establishing a connection with the server

Use StreamAllocation.newStream to establish a connection with the server, and return the input and output stream (HttpCodec), in fact, find an available Connection through the findConnection in StreamAllocation, and then call the connect method of Connection, and use the socket to establish a connection with the server.

1.2.5, CallServerInterceptor: responsible for reading the response data from the server

The main job is to write the requested Request to the server, and then read the Response from the server.
(1), write request header
(2), write request body
(3), read response header
(4), read response body

Connection pool principle

Since HTTP is based on TCP, a three-way handshake is required for TCP connection. In order to speed up network access, we can set Connection to keepalive in the header of Reuqst to reuse connections.

Okhttp supports 5 concurrent KeepAlives, the default link life is 5 minutes (after the link is idle, the time to keep alive), the connection pool is implemented by ConctionPool to recycle and manage the connection.

Guess you like

Origin blog.csdn.net/guodashen007/article/details/104953861