HttpUrlurl;Stringmethod;Headers.Builderheaders;RequestBodybody;/** A mutable map of tags, or an immutable empty map if we don't have any. */Map<Class<?>,Object>tags=Collections.emptyMap();
staticRealCallnewRealCall(OkHttpClientclient,RequestoriginalRequest,booleanforWebSocket){// Safely publish the Call instance to the EventListener.RealCallcall=newRealCall(client,originalRequest,forWebSocket);call.eventListener=client.eventListenerFactory().create(call);returncall;}
ResponsegetResponseWithInterceptorChain()throwsIOException{// Build a full stack of interceptors.List<Interceptor>interceptors=newArrayList<>();interceptors.addAll(client.interceptors());// OkHttpClient 提供的默认拦截器interceptors.add(retryAndFollowUpInterceptor);interceptors.add(newBridgeInterceptor(client.cookieJar()));interceptors.add(newCacheInterceptor(client.internalCache()));interceptors.add(newConnectInterceptor(client));if(!forWebSocket){interceptors.addAll(client.networkInterceptors());}interceptors.add(newCallServerInterceptor(forWebSocket));// 重点参数是 0 ,表示责任链的开始Interceptor.Chainchain=newRealInterceptorChain(interceptors,null,null,null,0,originalRequest,this,eventListener,client.connectTimeoutMillis(),client.readTimeoutMillis(),client.writeTimeoutMillis());returnchain.proceed(originalRequest);}
@OverridepublicResponseproceed(Requestrequest)throwsIOException{returnproceed(request,streamAllocation,httpCodec,connection);}publicResponseproceed(Requestrequest,StreamAllocationstreamAllocation,HttpCodechttpCodec,RealConnectionconnection)throwsIOException{if(index>=interceptors.size())thrownewAssertionError();calls++;// 删除一些判断和抛出异常的方法// Call the next interceptor in the chain.// 以拦截器队列中的下一个拦截器为起点,构建新的请求链RealInterceptorChainnext=newRealInterceptorChain(interceptors,streamAllocation,httpCodec,connection,index+1,request,call,eventListener,connectTimeout,readTimeout,writeTimeout);// 取出当前请求链中的第一个Interceptorinterceptor=interceptors.get(index);// 执行当前拦截器的功能,并且开始子链的请求流程Responseresponse=interceptor.intercept(next);// 删除一些判断和抛出异常的方法returnresponse;}
整个拦截器的集合 interceptors 是不会变的,而 index 就对应从拦截器集合中取出拦截器的索引,为 0 表示取出第一个来作为整条 链 的起点,从而构建一条请求链。
// 异步请求中的方法@Overrideprotectedvoidexecute(){booleansignalledCallback=false;try{// 具体执行的请求还是在这里Responseresponse=getResponseWithInterceptorChain();if(retryAndFollowUpInterceptor.isCanceled()){signalledCallback=true;responseCallback.onFailure(RealCall.this,newIOException("Canceled"));}else{signalledCallback=true;responseCallback.onResponse(RealCall.this,response);}}catch(IOExceptione){if(signalledCallback){// Do not signal the callback twice!Platform.get().log(INFO,"Callback failure for "+toLoggableString(),e);}else{// 请求失败的回调eventListener.callFailed(RealCall.this,e);responseCallback.onFailure(RealCall.this,e);}}finally{client.dispatcher().finished(this);}}
// 异步请求才会执行该方法privatevoidpromoteCalls(){// 优先把 readyAsyncCalls 队列中的请求执行完if(runningAsyncCalls.size()>=maxRequests)return;// Already running max capacity.if(readyAsyncCalls.isEmpty())return;// No ready calls to promote.for(Iterator<AsyncCall>i=readyAsyncCalls.iterator();i.hasNext();){AsyncCallcall=i.next();if(runningCallsForHost(call)<maxRequestsPerHost){i.remove();runningAsyncCalls.add(call);executorService().execute(call);}if(runningAsyncCalls.size()>=maxRequests)return;// Reached max capacity.}}