This is the fourth day of my participation in the August Text Challenge.More challenges in August
A quick review of synchronous/asynchronous
- A synchronous request is a blocking way of executing the request until the HTTP response is returned
- Asynchronous requests are similar to non-blocking requests in that the result of their execution is usually notified to the caller through an interface callback
Introduction to interceptors
- OkHttp provides a powerful mechanism for network listening, request and response rewriting, retrying failed requests, and more
However, it is important to note that the interceptor is synchronous/asynchronous; So the implication is clear, whether synchronous or asynchronous we will use the interceptor functionality for the actual network request response
- You can see the picture below, which is provided by the official website
There are two types of interceptors, Application interceptor and Network interceptor, and what we care about is this OkHttp Core in the middle, which is the internal interceptor that the OkHttp framework gives us. When you send a request, The request is executed through the interceptor’s chain
- Here’s a quick look at the five interceptors it offers in-house
-
Retry and failure is the first RetryAndFollowUpInterceptor redirect interceptors, mainly do the initialization work, create streamAllocation object
-
The BridgeInterceptor is a bridge and adaptation interceptor, and the CacheInterceptor is a CacheInterceptor. Their main responsibility is to supplement some of the required HTTP headers and caching functions that are missing from user creation requests
-
Then there are two more important interceptors, ConntectIntercept and CallServerIntercept, which are actually the key to network interaction; Where CallServerIntercept writes our HTTP request into the IO stream of the network, and reads the data returned by the server to our client from the IO stream of the network. ConnectIntercept establishes the usable connection. It is arguably the foundation of CallServerIntercept
Interceptor source code analysis
The interceptor chain
- GetResponseWithInterceptorChain first of all, we can see the * * * * () method, it returns the Response is our Response results
Response getResponseWithInterceptorChain(a) throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
interceptors.addAll(client.interceptors());
interceptors.add(retryAndFollowUpInterceptor);
interceptors.add(new BridgeInterceptor(client.cookieJar()));
interceptors.add(new CacheInterceptor(client.internalCache()));
interceptors.add(new ConnectInterceptor(client));
if(! forWebSocket) { interceptors.addAll(client.networkInterceptors()); } interceptors.add(new CallServerInterceptor(forWebSocket));
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null.null.null.0,
originalRequest, this, eventListener, client.connectTimeoutMillis(),
client.readTimeoutMillis(), client.writeTimeoutMillis());
return chain.proceed(originalRequest);
}
Copy the code
Interceptors are a collection of interceptors that are used to add interceptors. They are then passed to the constructor to add the five interceptors in turn to the collection
- Let’s take a look at its processD () method, and here’s its core implementation code
// Call the next interceptor in the chain.
RealInterceptorChain next = new RealInterceptorChain(interceptors, streamAllocation, httpCodec,
connection, index + 1, request, call, eventListener, connectTimeout, readTimeout,
writeTimeout);
Interceptor interceptor = interceptors.get(index);
Response response = interceptor.intercept(next);
// Confirm that the next interceptor made its required call to chain.proceed().
if(httpCodec ! =null && index + 1< interceptors.size() && next.calls ! =1) {
throw new IllegalStateException("network interceptor " + interceptor
+ " must call proceed() exactly once");
}
Copy the code
Create the interceptor chain RealInterceptorChain here, but this and previous getResponseWithInterceptorChain () have obvious difference, the incoming parameter is the index + 1, the significance is only starting from the next interceptor access, Instead of being able to access it from the current interceptor, you have a chain, which is also clever; The next step is to execute the index interceptor method and pass in the interceptor whose index is Next. This completes the chain of interceptors
In a nutshell, this method mainly does the following
- Create a series of interceptors and a collection of interceptors to put into the collection
- Create an interceptor chain RealInterceptorChain and execute its processD () method from the interceptor chain, which performs the interceptor constructor in processD () and gets the interception or by subscript.
- It then returns a Response each time it calls its interceptor method, and executes the next interceptor’s proceed method again, forming a chain call that makes up a complete chain of interceptor returns
Interceptor summary
In the above statement, an Okhttp network request can be understood as a process in which an interceptor executes its interceptor method
An interceptor method has the following steps to perform
- Process the request (including headers, etc.) before the request is made
- Invoke the next interceptor (the concept of an interceptor chain) to get response
- Response is processed and returned to the previous interceptor
Stern said
The above is the core logic and summary of the overall interceptor. Next, I will analyze the interceptor given by the system one by one, including its source code analysis and specific functions. Welcome to study with me and continue to voyage
To be continued