[TOC]

0. Core job: Interceptor

After the dispatcher distribution, request task finally came to the steps to perform the work, in the source code we can see its core working method ingetResponseWithInterceptorChain ()In this method, we create a chain of five default interceptors for our request task,The chain of responsibility mode is used here, so that each interceptor object on the chain can do its own operation on the task to be executed, and can also do its own processing on the result of the task completion, the inner part of the chain is processed by the first interceptor, and then handed over to the next interceptor while waiting for the result to returnAs we can see from the source, the first interceptor to be processed isRetryAndFollowUpInterceptor

1. Retry and redirect the interceptor: RetryAndFollowUpInterceptor

Responsibilities: Responsible for retrying requests and redirecting requests if necessary

Intercepting method: For throwsRouteException andIOException Process, judge whether it needs to retry, and deliver it to the next chain normallyBridgeInterceptor

2. BridgeInterceptor

Responsibilities: The bridge between the application and the server, which processes the requests we make before sending them to the server for processing when the results are received (e.g., gzip decompression)

Interception method: here a series of operations are handled; Complete the request header, set the encoding, gZIP compression, etc. Normal delivery to the next chainCacheInterceptor

3. CacheInterceptor: CacheInterceptor

Responsibilities: Determine whether caching is used before a request is sent and whether caching is required after a response is received

Interception method: cache processing to the next chainConnectInterceptor

4. ConnectInterceptor

Responsibilities: Open a connection to the target server and execute the next interceptor

Interception method: obtain a connection with the target server, on this connection to send and receive HTTP data, delivery of the next chainCallServerInterceptor

5. Request server interceptor: CallServerInterceptor

Responsibilities: Send and receive data on the connection

Interception method: Encapsulate and parse HTTP packets and send back the results. At this point, the request and response are complete and only the response is processed

6. Summary

The five interceptors, with their own division of labor, completed their own parts through the chain of responsibility mode, and delivered to the next ring. After completing the request, the result is processed and delivered to the previous ring, decoupling the sender and receiver, while sorting out the process logic is clear, NICE!