OkHttp principle and mechanism explanation

OkHttp principle and mechanism explanation

Link to this article: https://blog.csdn.net/feather_wch/article/details/131767285

1. What are the principles and mechanisms of OkHttp?

  1. Application of Design Patterns: Builder Pattern, Appearance Pattern, Chain of Responsibility Pattern
  2. overall process
  3. Distributor (scheduling mechanism)
  4. TCP link multiplexing (multiplexing mechanism)
  5. Interceptor process
  6. caching mechanism

2. The difference between Http 1.1 and Http 2.0

  1. 1.1 keep-alive, serial, ordered -> Order is based on text and must be ordered
  2. 2.0 header compression (smaller), ServerPush (server active Push), multiplexing, parallelism, derived from the use of binary data frames (storage of sequence flags)

3. Overall process of OkHttp request

flowchart TD
    OkHttpClient --newCall 参数Request--> RealCall --execute/enqueue--> Dispatcher --> Intercepter --> Response

4. Distributor (scheduling mechanism)

  1. Synchronization: directly execute the interceptor process
  2. Asynchronous: Submit to the thread pool execute
  3. Internal members of the scheduler:
    1. Maximum number of simultaneous asynchronous requests, 64
    2. The number of simultaneous asynchronous requests to the same host, 5
    3. Thread Pool
    4. asynchronous wait queue
    5. asynchronous execution queue
    6. synchronous execution queue
  4. Thread pool: core=0, max=65535, waitTime=60s, queue=no capacity, equivalent to CacheThreadPool
    1. Create a thread when there is a request, 60s expires
    2. Maximum number of threads is unlimited & 64 = 64
  5. After the task is executed, finish() does the cleaning work
    1. Synchronous / asynchronous must be out of the queue
    2. Asynchronous: calculate according to 64 and 5
    3. The promoteCalls() method reschedules the request

5. Scheduling mechanism, asynchronous request process

  1. If there are no more than 64 asynchronous requests at the same time and the number of requests from the same host does not exceed 5, the request will be initiated and it will be in the asynchronous execution queue
  2. If the condition is not met, it will be placed in the asynchronous waiting queue for waiting
  3. After the asynchronous request is completed, execute finish() to clean up the work, and check the two conditions of 64 and 5. If the conditions are met, promoteCalls() will be called to re-schedule the request

6. Interceptor + Chain of Responsibility

  1. *Custom user interceptor: printable log, addInterceptor
  2. Retry redirect interceptor: 1. Protocol 2. Timeout 3. IO exception 4. SSL exception (SecureSocketLayer security protocol)
  3. Bridging Interceptors: Gzip, Cookies
  4. Cache interceptor: caching mechanism
  5. Connection interceptor: open the connection with the target, RealConnection (encapsulates Socket and Socket connection pool)
  6. *Custom network interceptor: addNetworkInterceptor
  7. Request service interceptor: write data + flushRequest (really send the request)

TCP connection multiplexing mechanism

8. OkHttp connection pool and connection multiplexing mechanism

  1. The connection multiplexing mechanism is implemented based on the ConnectionPool class.
  2. The ConnectionPool class maintains a double-ended queue for storing idle RealConnection objects.
  3. The RealConnection object represents a TCP connection that can be shared by multiple requests. ³⁴
  4. When a user initiates a request, OKHTTP will first check whether there is an idle connection that meets the requirements in the connection pool. If there is, it will directly use the connection to send the request. If not, it will create a new connection and add it to the connection pool.
  5. Cleanup mechanism: OKHTTP uses a background thread to periodically clean up idle connections. The cleaning conditions are:
    1. If a connection is idle for more than 5 minutes
    2. Or the number of idle connections in the connection pool exceeds 5

caching mechanism

9. Cache mechanism

  1. Strong cache: do not send request, expires field
  2. Negotiation cache: 304 indicates that the cache is available
  3. Datat、Expires、Last-Modified、ETag

10. Detailed analysis of caching mechanism

  1. The caching mechanism of OkHttp is implemented according to the caching mechanism of HTTP. OkHttp's specific data caching logic is encapsulated in the Cache class, which is implemented using DiskLruCache . ¹³
  2. By default, OkHttp does not cache data. You can set the Cache object when constructing OkHttpClient , and specify the cache directory and cache size in its constructor. ¹³
  3. OkHttp provides a batch of optional caching strategies, which are uniformly configured through CacheControl , and we can see the caching strategy through the constructor. ²
  4. The caching mechanism of HTTP is divided into strong caching and negotiation caching . The implementation of these two caches is controlled through the fields of the header information of the HTTP protocol, and then judges whether to load data directly from the local cache according to the expiration time.

Guess you like

Origin blog.csdn.net/feather_wch/article/details/131767285