Okhttp深入淺出

Okhttp分析

//同步
OkhttpClient.Builder().newCall(request).execute()
//異步
OkhttpClient.Builder().newCall(request).enqueue{
	onSuccess(...){}
	onFailed(...){}
}

newCall的時候創建了RealCall,同時傳入了request的請求參數。

  • 看異步方法:

       client.dispatcher().enqueue(new AsyncCall(responseCallback));
      1.到Dispatch中,判斷當前正在請求數,同一host的最大請求數有沒有超5。
      2.調用asyncCall的executeOn(executorService) ,把線程池帶了過來
      3.executorService.execute(this);
      4.回調AsyncCall的execute()方法
      Response response = getResponseWithInterceptorChain();
      攔截器開始工作,返回的是response
    

getResponseWithInterceptorChain()

Response getResponseWithInterceptorChain() throws IOException {
// Build a full stack of interceptors.
List<Interceptor> interceptors = new ArrayList<>();
1. 加入自定義的攔截器,主要用於爲請求添加header之類的
interceptors.addAll(client.interceptors());
2. 重定向攔截器
interceptors.add(retryAndFollowUpInterceptor);
3. 添加請求信息攔截器,比如cookies
interceptors.add(new BridgeInterceptor(client.cookieJar()));
4. 緩存攔截器 
interceptors.add(new CacheInterceptor(client.internalCache()));
5. 連接攔截器
interceptors.add(new ConnectInterceptor(client));
if (!forWebSocket) {
6. 非webSocket,添加網絡攔截器,自定義的,主要是響應返回之後的,自定義攔截
  interceptors.addAll(client.networkInterceptors());
}
7. 請求服務器攔截器
interceptors.add(new CallServerInterceptor(forWebSocket));
//攔截器的封裝
Interceptor.Chain chain = new RealInterceptorChain(interceptors, null, null, null, 0,
    originalRequest, this, eventListener, client.connectTimeoutMillis(),
    client.readTimeoutMillis(), client.writeTimeoutMillis());
//開始執行
Response response = chain.proceed(originalRequest);
if (retryAndFollowUpInterceptor.isCanceled()) {
  closeQuietly(response);
  throw new IOException("Canceled");
}
return response;}

RetryAndFollowUpInterceptor,重定向攔截器

@Override public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Call call = realChain.call();
//連接池的操作
StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
    createAddress(request.url()), call, eventListener, callStackTrace);
this.streamAllocation = streamAllocation;

int followUpCount = 0;
Response priorResponse = null;
//while的循環,循環搞事情,看來不簡單
while (true) {
  if (canceled) {
    streamAllocation.release();
    throw new IOException("Canceled");
  }

  Response response;
  boolean releaseConnection = true;
  try {
	//調用下一個攔截器
    response = realChain.proceed(request, streamAllocation, null, null);
    releaseConnection = false;
  } 
 ...
  // Attach the prior response if it exists. Such responses never have a body.
  if (priorResponse != null) {
    response = response.newBuilder()
        .priorResponse(priorResponse.newBuilder()
                .body(null)
                .build())
        .build();
  }

  Request followUp;
  try {
	// 300-303 重定向操作,這個followUp已經在其中封裝好了下次請求的請求頭
	// 408 請求超時,也需要重新進行請求
    followUp = followUpRequest(response, streamAllocation.route());
  } catch (IOException e) {
    streamAllocation.release();
    throw e;
  }
 // 爲空就不要請求了,不爲空:重試或者重定向 即 再次請求 
  if (followUp == null) {
    streamAllocation.release();
    return response;
  }

  closeQuietly(response.body());
//請求次數是否達到了20次
  if (++followUpCount > MAX_FOLLOW_UPS) {
    streamAllocation.release();
    throw new ProtocolException("Too many follow-up requests: " + followUpCount);}
// 重新請求的request賦值
  request = followUp;
//當前返回的response賦值
  priorResponse = response;}}

BridgeInterceptor

@Override public Response intercept(Chain chain) throws IOException {
Request userRequest = chain.request();
Request.Builder requestBuilder = userRequest.newBuilder();
RequestBody body = userRequest.body();
//處理了以下首部:
// Content-Type
// Content-Length
// Transfer-Encoding
// Host
// Connection
//Accept-Encoding
if (body != null) {
  MediaType contentType = body.contentType();
  if (contentType != null) {
    requestBuilder.header("Content-Type", contentType.toString());
  }
  long contentLength = body.contentLength();
  if (contentLength != -1) {
    requestBuilder.header("Content-Length", Long.toString(contentLength));
    requestBuilder.removeHeader("Transfer-Encoding");
  } else {
    requestBuilder.header("Transfer-Encoding", "chunked");
    requestBuilder.removeHeader("Content-Length");
  }
}

if (userRequest.header("Host") == null) {
  requestBuilder.header("Host", hostHeader(userRequest.url(), false));
}

if (userRequest.header("Connection") == null) {
  requestBuilder.header("Connection", "Keep-Alive");
}

// If we add an "Accept-Encoding: gzip" header field we're responsible for also decompressing
// the transfer stream.
boolean transparentGzip = false;
if (userRequest.header("Accept-Encoding") == null && userRequest.header("Range") == null) {
  transparentGzip = true;
  requestBuilder.header("Accept-Encoding", "gzip");
}
//接口回調,根據當前url,返回需要的cookies
List<Cookie> cookies = cookieJar.loadForRequest(userRequest.url());
if (!cookies.isEmpty()) {
  requestBuilder.header("Cookie", cookieHeader(cookies));
}

if (userRequest.header("User-Agent") == null) {
  requestBuilder.header("User-Agent", Version.userAgent());
}
//調用下一個攔截器
Response networkResponse = chain.proceed(requestBuilder.build());
//檢查響應是否包含了cookies,有的話接口回調,存儲起來
HttpHeaders.receiveHeaders(cookieJar, userRequest.url(), networkResponse.headers());

Response.Builder responseBuilder = networkResponse.newBuilder()
    .request(userRequest);

if (transparentGzip
    && "gzip".equalsIgnoreCase(networkResponse.header("Content-Encoding"))
    && HttpHeaders.hasBody(networkResponse)) {
  GzipSource responseBody = new GzipSource(networkResponse.body().source());
  Headers strippedHeaders = networkResponse.headers().newBuilder()
      .removeAll("Content-Encoding")
      .removeAll("Content-Length")
      .build();
  responseBuilder.headers(strippedHeaders);
  String contentType = networkResponse.header("Content-Type");
  responseBuilder.body(new RealResponseBody(contentType, -1L, Okio.buffer(responseBody)));
}
return responseBuilder.build();}

CacheInterceptor

@Override public Response intercept(Chain chain) throws IOException {
//根據request獲取緩存,默認是爲空的
Response cacheCandidate = cache != null
    ? cache.get(chain.request())
    : null;

long now = System.currentTimeMillis();

CacheStrategy strategy = new CacheStrategy.Factory(now, chain.request(), cacheCandidate).get();
//默認netWorkRequest不爲空,如果有緩存,並且緩存可以使用,那麼netWorkRequest爲空
Request networkRequest = strategy.networkRequest;
//默認爲空				  如果有緩存,並且緩存可以使用,那麼cacheResponse不爲空
Response cacheResponse = strategy.cacheResponse;

if (cache != null) {
  cache.trackResponse(strategy);
}

if (cacheCandidate != null && cacheResponse == null) {
  closeQuietly(cacheCandidate.body()); // The cache candidate wasn't applicable. Close it.
}

// If we're forbidden from using the network and the cache is insufficient, fail.
if (networkRequest == null && cacheResponse == null) {
  return new Response.Builder()
      .request(chain.request())
      .protocol(Protocol.HTTP_1_1)
      .code(504)
      .message("Unsatisfiable Request (only-if-cached)")
      .body(Util.EMPTY_RESPONSE)
      .sentRequestAtMillis(-1L)
      .receivedResponseAtMillis(System.currentTimeMillis())
      .build();
}

// If we don't need the network, we're done.
if (networkRequest == null) { //緩存可用,直接返回。
  return cacheResponse.newBuilder()
      .cacheResponse(stripBody(cacheResponse))
      .build();
}

Response networkResponse = null;
try {
//下一個攔截器
  networkResponse = chain.proceed(networkRequest);
} finally {
  // If we're crashing on I/O or otherwise, don't leak the cache body.
  if (networkResponse == null && cacheCandidate != null) {
    closeQuietly(cacheCandidate.body());
  }
}

// If we have a cache response too, then we're doing a conditional get.
if (cacheResponse != null) {
  if (networkResponse.code() == HTTP_NOT_MODIFIED) {
    Response response = cacheResponse.newBuilder()
        .headers(combine(cacheResponse.headers(), networkResponse.headers()))
        .sentRequestAtMillis(networkResponse.sentRequestAtMillis())
        .receivedResponseAtMillis(networkResponse.receivedResponseAtMillis())
        .cacheResponse(stripBody(cacheResponse))
        .networkResponse(stripBody(networkResponse))
        .build();
    networkResponse.body().close();

    // Update the cache after combining headers but before stripping the
    // Content-Encoding header (as performed by initContentStream()).
    cache.trackConditionalCacheHit();
    cache.update(cacheResponse, response);
    return response;
  } else {
    closeQuietly(cacheResponse.body());
  }
}

Response response = networkResponse.newBuilder()
    .cacheResponse(stripBody(cacheResponse))
    .networkResponse(stripBody(networkResponse))
    .build();

if (cache != null) {
  if (HttpHeaders.hasBody(response) && CacheStrategy.isCacheable(response, networkRequest)) {
    // Offer this request to the cache.
	//加入到cache中
    CacheRequest cacheRequest = cache.put(response);
    return cacheWritingResponse(cacheRequest, response);
  }

  if (HttpMethod.invalidatesCache(networkRequest.method())) {
    try {
      cache.remove(networkRequest);
    } catch (IOException ignored) {
      // The cache cannot be written.
    }
  }
}
return response;}

ConnectInterceptor

@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
Request request = realChain.request();
StreamAllocation streamAllocation = realChain.streamAllocation();

// We need the network to satisfy this request. Possibly for validating a conditional GET.
boolean doExtensiveHealthChecks = !request.method().equals("GET");
HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
RealConnection connection = streamAllocation.connection();
return realChain.proceed(request, streamAllocation, httpCodec, connection);}

從上面這個攔截器,見名知意,就要開始搞大事了。

在此之前,我們需要注意的是OkhttpClient在初始化的時候的兩個點

  1. 在Builder()構造函數中:

     //啥?連接池,是的,你沒看錯。
     connectionPool = new ConnectionPool();
     //果斷殺入這個ConnectionPool,不過屬性和方法,我就能知道你在幹啥
     //線程池,雖然開的無限大,但是其實只創建了一個線程來搞事情
       private static final Executor executor = new ThreadPoolExecutor(0 /* corePoolSize */,
           Integer.MAX_VALUE /* maximumPoolSize */, 60L /* keepAliveTime */, TimeUnit.SECONDS,
           new SynchronousQueue<Runnable>(), Util.threadFactory("OkHttp ConnectionPool", true));
     
       /** The maximum number of idle connections for each address. */
       private final int maxIdleConnections; // 5
       private final long keepAliveDurationNs; // 5分鐘
     //這個線程池就只會執行這唯一一個runable,仔細看它的內容你會知道,在做一些超時清理問題
       private final Runnable cleanupRunnable = new Runnable() {
         @Override public void run() {
           while (true) {
             long waitNanos = cleanup(System.nanoTime());
             if (waitNanos == -1) return;
             if (waitNanos > 0) {
               long waitMillis = waitNanos / 1000000L;
               waitNanos -= (waitMillis * 1000000L);
               synchronized (ConnectionPool.this) {
                 try {
                   ConnectionPool.this.wait(waitMillis, (int) waitNanos);
                 } catch (InterruptedException ignored) {}}}}}};
     
     //雙端隊列用來存儲RealConnection
       private final Deque<RealConnection> connections = new ArrayDeque<>();
     ##################################
     //根據host、路由來尋找已經存在的RealConnection. 注意這個方法的調用
       @Nullable RealConnection get(Address address, StreamAllocation streamAllocation, Route route) {
         assert (Thread.holdsLock(this));
         for (RealConnection connection : connections) {
           if (connection.isEligible(address, route)) {
     		//設置streamAllocation中的RealConnection 爲這找到的這個
             streamAllocation.acquire(connection, true);
             return connection;
           }
         }
         return null;
       }		
    
  2. OkhttpClient中還有另外一個需要注意的點:

     //所以一會兒,Internal.instance.get(...)你就需要到ConnectionPool中去查看
     //靜態的初始化
     static {
     	//抽象類
         Internal.instance = new Internal() {
     	...
       @Override public RealConnection get(ConnectionPool pool, Address address,
           StreamAllocation streamAllocation, Route route) {
     //調用ConnectPool的get方法
         return pool.get(address, streamAllocation, route);
       }
       @Override public void put(ConnectionPool pool, RealConnection connection) {
     //調用ConnectPool的put方法
         pool.put(connection);
       }
     	...
    

繼續回到ConnectInterceptor中

//不是get方法
boolean doExtensiveHealthChecks = !request.method().equals("GET");
//調用streamAllocation的newStream()
//返回的是一個HttpCodec,這個HttpCodec分爲Http1Codec和Http2Codec,分別對應的是http1.XX和http2.xx
HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
RealConnection connection = streamAllocation.connection();

StreamAllocation.newStream()

//~~尋找健康的Connection
  RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
      writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks);
  HttpCodec resultCodec = resultConnection.newCodec(client, chain, this);

//尋找健康的RealConnection
 private RealConnection findHealthyConnection(int connectTimeout, int readTimeout,
  int writeTimeout, int pingIntervalMillis, boolean connectionRetryEnabled,
  boolean doExtensiveHealthChecks) throws IOException {
  //while循環,你應該知道事情沒那麼簡單
  while (true) {
  //candidate 候選人的意思
  RealConnection candidate = findConnection(connectTimeout, readTimeout, writeTimeout,
      pingIntervalMillis, connectionRetryEnabled);
  // If this is a brand new connection, we can skip the extensive health checks.
  synchronized (connectionPool) {
    if (candidate.successCount == 0 && !candidate.isMultiplexed()) {
      return candidate;
    }
  }
 //判斷這個socket的輸入流,輸出流,socket是否close
  // isn't, take it out of the pool and start again.
  if (!candidate.isHealthy(doExtensiveHealthChecks)) {
	//已經掛了,做一些清理工作
    noNewStreams();
	//繼續,這次就找不到緩存的了,只能新new一個RealConnection出來。
    continue;}
  return candidate;}}

findConnection()

private RealConnection findConnection(int connectTimeout, int readTimeout, int writeTimeout,
  int pingIntervalMillis, boolean connectionRetryEnabled) throws IOException {
boolean foundPooledConnection = false;
RealConnection result = null;
Route selectedRoute = null;
Connection releasedConnection;
Socket toClose;
synchronized (connectionPool) {
  ################
我們知道StreamAllocation是在RetryAndFllowUpInterceptor中創建的對象,
如果第一次請求超時,返回了408,那麼將會重試,再次請求,下面這個connection就不會爲空。
  releasedConnection = this.connection;
  toClose = releaseIfNoNewStreams();
  if (this.connection != null) {
    // We had an already-allocated connection and it's good.
    result = this.connection;
    releasedConnection = null;
  }
  if (!reportedAcquired) {
    // If the connection was never reported acquired, don't report it as released!
    releasedConnection = null;
  }
  if (result == null) {
	//去ConnectPool裏面尋找一個匹配的RealConnection
	//通過上面的分析,我們知道下面這個get,實則是去ConnectionPool裏面找,找到之後
	//通過傳入的這個this(streamAllocation),把這個RealConnection傳到當前類中
    Internal.instance.get(connectionPool, address, this, null);
	//上一步如果找到,那麼connection在這已經不爲空了
    if (connection != null) {
      foundPooledConnection = true;
      result = connection;
    } else {
      selectedRoute = route;
    }
  }
}
closeQuietly(toClose);

if (releasedConnection != null) {
  eventListener.connectionReleased(call, releasedConnection);
}
if (foundPooledConnection) {
  eventListener.connectionAcquired(call, result);
}
//如果已經找到了,那麼就可以返回了
if (result != null) {
  // If we found an already-allocated or pooled connection, we're done.
  route = connection.route();
  return result;
}

// If we need a route selection, make one. This is a blocking operation.
boolean newRouteSelection = false;
if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) {
  newRouteSelection = true;
  routeSelection = routeSelector.next();
}

synchronized (connectionPool) {
  if (canceled) throw new IOException("Canceled");

  if (newRouteSelection) {
    // Now that we have a set of IP addresses, make another attempt at getting a connection from
    // the pool. This could match due to connection coalescing.
    List<Route> routes = routeSelection.getAll();
    for (int i = 0, size = routes.size(); i < size; i++) {
      Route route = routes.get(i);
      Internal.instance.get(connectionPool, address, this, route);
      if (connection != null) {
        foundPooledConnection = true;
        result = connection;
        this.route = route;
        break;
      }
    }
  }

  if (!foundPooledConnection) {
    if (selectedRoute == null) {
      selectedRoute = routeSelection.next();
    }

    // Create a connection and assign it to this allocation immediately. This makes it possible
    // for an asynchronous cancel() to interrupt the handshake we're about to do.
    route = selectedRoute;
    refusedStreamCount = 0;
	//找不到,直接new一個RealConnection
    result = new RealConnection(connectionPool, selectedRoute);
	//設置當前的connection爲這new出來的這個RealConnection.
    acquire(result, false);
  }
}

// If we found a pooled connection on the 2nd time around, we're done.
if (foundPooledConnection) {
  eventListener.connectionAcquired(call, result);
  return result;
}
//下面的註釋就知道進行了TCP,TLS握手
// Do TCP + TLS handshakes. This is a blocking operation.
//這行代碼進行了Socket的鏈接,也就是三次握手,另外,也有兩行很重要的代碼
// ##################################################
//可以看到是通過Okio來進行的操作。 這些操作都是在RealConnection中進行的
// source = Okio.buffer(Okio.source(rawSocket)); 數入流
// sink = Okio.buffer(Okio.sink(rawSocket));     輸出流
// ###################################################
result.connect(connectTimeout, readTimeout, writeTimeout, pingIntervalMillis,
    connectionRetryEnabled, call, eventListener);
routeDatabase().connected(result.route());

Socket socket = null;
synchronized (connectionPool) {
  reportedAcquired = true;
//將這個創建 出來的RealConnection加入到線程池中
  // Pool the connection.

  Internal.instance.put(connectionPool, result);
// 上面這行代碼的具體實現是ConnectionPool的put方法 #######
//  void put(RealConnection connection) {
//    assert (Thread.holdsLock(this));
//    if (!cleanupRunning) { //ConnectionPool是的cleanupRunning默認是false
//      cleanupRunning = true; //只會執行一次
//      executor.execute(cleanupRunnable); // 用於超時的鏈接清理
//    }
//    connections.add(connection);
//  }										   #######
  // If another multiplexed connection to the same address was created concurrently, then
  // release this connection and acquire that one.
  if (result.isMultiplexed()) {
    socket = Internal.instance.deduplicate(connectionPool, address, this);
    result = connection;
  }
}
closeQuietly(socket);
eventListener.connectionAcquired(call, result);
//返回,結束
return result;}

最後一個攔截器:CallServerInterceptor

@Override public Response intercept(Chain chain) throws IOException {
RealInterceptorChain realChain = (RealInterceptorChain) chain;
//HttpCodec包裹了在RealConnection中創建好socket之後拿到的source和sink,即輸入輸出流
HttpCodec httpCodec = realChain.httpStream();
StreamAllocation streamAllocation = realChain.streamAllocation();
RealConnection connection = (RealConnection) realChain.connection();
Request request = realChain.request();

long sentRequestMillis = System.currentTimeMillis();

realChain.eventListener().requestHeadersStart(realChain.call());
httpCodec.writeRequestHeaders(request);
realChain.eventListener().requestHeadersEnd(realChain.call(), request);

Response.Builder responseBuilder = null;
if (HttpMethod.permitsRequestBody(request.method()) && request.body() != null) {
  // If there's a "Expect: 100-continue" header on the request, wait for a "HTTP/1.1 100
  // Continue" response before transmitting the request body. If we don't get that, return
  // what we did get (such as a 4xx response) without ever transmitting the request body.
  if ("100-continue".equalsIgnoreCase(request.header("Expect"))) {
    httpCodec.flushRequest();
    realChain.eventListener().responseHeadersStart(realChain.call());
    responseBuilder = httpCodec.readResponseHeaders(true);
  }

  if (responseBuilder == null) {
    // Write the request body if the "Expect: 100-continue" expectation was met.
    realChain.eventListener().requestBodyStart(realChain.call());
    long contentLength = request.body().contentLength();
	//創建請求body
    CountingSink requestBodyOut =
        new CountingSink(httpCodec.createRequestBody(request, contentLength));
    BufferedSink bufferedRequestBody = Okio.buffer(requestBodyOut);

    request.body().writeTo(bufferedRequestBody);
    bufferedRequestBody.close();
    realChain.eventListener()
        .requestBodyEnd(realChain.call(), requestBodyOut.successfulCount);
  } else if (!connection.isMultiplexed()) {
    // If the "Expect: 100-continue" expectation wasn't met, prevent the HTTP/1 connection
    // from being reused. Otherwise we're still obligated to transmit the request body to
    // leave the connection in a consistent state.
    streamAllocation.noNewStreams();
  }
}
//調用 sink.flush();
httpCodec.finishRequest();

if (responseBuilder == null) {
  realChain.eventListener().responseHeadersStart(realChain.call());
//只是拿了響應頭,後續判斷響應是否正確
  responseBuilder = httpCodec.readResponseHeaders(false);
}

Response response = responseBuilder
    .request(request)
    .handshake(streamAllocation.connection().handshake())
    .sentRequestAtMillis(sentRequestMillis)
    .receivedResponseAtMillis(System.currentTimeMillis())
    .build();
//判斷響應頭是否正確
int code = response.code();
if (code == 100) {
  // server sent a 100-continue even though we did not request one.
  // try again to read the actual response
  responseBuilder = httpCodec.readResponseHeaders(false);

  response = responseBuilder
          .request(request)
          .handshake(streamAllocation.connection().handshake())
          .sentRequestAtMillis(sentRequestMillis)
          .receivedResponseAtMillis(System.currentTimeMillis())
          .build();

  code = response.code();
}

realChain.eventListener()
        .responseHeadersEnd(realChain.call(), response);

if (forWebSocket && code == 101) {
  // Connection is upgrading, but we need to ensure interceptors see a non-null response body.
  response = response.newBuilder()
      .body(Util.EMPTY_RESPONSE)
      .build();
} else {
//響應頭無異常,調用httpCodec.openResponseBody(response)真正的讀取響應體
  response = response.newBuilder()
      .body(httpCodec.openResponseBody(response))
      .build();
}

if ("close".equalsIgnoreCase(response.request().header("Connection"))
    || "close".equalsIgnoreCase(response.header("Connection"))) {
  streamAllocation.noNewStreams();
}

if ((code == 204 || code == 205) && response.body().contentLength() > 0) {
  throw new ProtocolException(
      "HTTP " + code + " had non-zero Content-Length: " + response.body().contentLength());
}
//請求結束,返回響應.
return response; }

至此Okhttp的分析就到此了。遇到的關鍵點:

  1. RealCall --> call
  2. AsyncCall --> Runable
  3. Dispatcher --> 線程池調度器,維護着集合: readyAsyncCalls、 runningAsyncCalls、runningSyncCalls,正在運行的是否超過64? 同一個url請求是否超過5個連接?
  4. 攔截器: RetryAndFllowUpInterceptor、BridgeInterceptor、CacheInterceptor、ConnectInterceptor、CallServerInterceptor.
  5. StreamAllocation --> 流管理,連接服務器,調度TCP連接的關鍵
  6. ConnectionPool --> socket連接池,負責管理空閒的socket連接,超時?socket斷開?進行清理工作,其內部包含了一個Deque ,隊列用來存儲已經連接的Socket,方便進行復用。
  7. RealConnection --> 一個真正的socket的連接,獲取sorce和sink,即輸入輸出流
  8. HttpCodec 接口,實現爲Http1Codec和Http2Codec分別爲http1.XX和http2.xx的協議。HttpCodeC主要引用這Sorce和Sink,進行輸入和輸出流的操作,對服務器進行請求和響應的接收。

其實,分析這些主流的框架,會有一種直觀的感受就是“層次分明”。各個部分是什麼功能,細粒度的分解,組合,封裝。好像很散,但是有很緊密。計算機裏面設計的很重要的思想就是分層,代碼層面,通過這些開源框架的學習,也能深有體會。記得小學學畫畫的時候,老師就一直強調:層次感很重要。明、暗、灰,各個部分的結合才能體現層次感。不能太黑,沒有層次,不能太亮沒有細節。

還有很多細節需要後續深入的去學習。但是瞭解一個框架的邏輯,也能從整體上把握住。也值得我們去學習。不要因爲細節而丟失整體,不要因爲整體而把握不住細節。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章