前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >okhttp之StreamAllocation

okhttp之StreamAllocation

作者头像
103style
发布2022-12-19 13:44:07
2610
发布2022-12-19 13:44:07
举报
文章被收录于专栏:Android开发经验分享

转载请以链接形式标明出处: 本文出自:103style的博客


base on 3.12.0


目录

  • 背景
  • 简介
  • StreamAllocation 的成员变量
  • StreamAllocation 的构造函数
  • StreamAllocation 的相关方法
  • 小结

背景

HTTP 的版本从最初的 1.0版本,到后续的 1.1版本,再到后续的 google 推出的SPDY,后来再推出 2.0版本,HTTP协议越来越完善。okhttp也是根据2.0和1.1/1.0作为区分,实现了两种连接机制.

http2.0解决了老版本(1.1和1.0)最重要两个问题:

http2.0 使用 多路复用 的技术,多个 stream 可以共用一个 socket 连接。每个 tcp连接都是通过一个 socket 来完成的,socket 对应一个 hostport,如果有多个stream(即多个 Request) 都是连接在一个 hostport上,那么它们就可以共同使用同一个 socket ,这样做的好处就是 可以减少TCP的一个三次握手的时间。 在OKHttp里面,负责连接的是 RealConnection


简介

官方注释是 StreamAllocation是用来协调ConnectionsStreamsCalls这三个实体的。

HTTP通信 执行 网络请求Call 需要在 连接Connection 上建立一个新的 Stream,我们将 StreamAllocation 称之 的桥梁,它负责为一次 请求 寻找 连接 并建立 ,从而完成远程通信。

然后我们先来看看 StreamAllocation 这个类是在什么地方初始化的,以及在哪些地方用到?

选中StreamAllocation 的构造方法,在 AndroidStudio 中按 Alt + F7,我们发现是在 RetryAndFollowUpInterceptor这个拦截器intercept(Chain chain)中创建的. 然后往下层拦截器传递,直到 ConnectInterceptor 以及 CallServerInterceptor才继续用到。

代码语言:javascript
复制
public final class RetryAndFollowUpInterceptor implements Interceptor {
    ...
  @Override public Response intercept(Chain chain) throws IOException {
    ...
    StreamAllocation streamAllocation = new StreamAllocation(client.connectionPool(),
        createAddress(request.url()), call, eventListener, callStackTrace);
    ...
}
public final class ConnectInterceptor implements Interceptor {
  ...
  @Override public Response intercept(Chain chain) throws IOException {
    ...
    HttpCodec httpCodec = streamAllocation.newStream(client, chain, doExtensiveHealthChecks);
    RealConnection connection = streamAllocation.connection();
    return realChain.proceed(request, streamAllocation, httpCodec, connection);
  }
}
public final class CallServerInterceptor implements Interceptor {
  ...
  @Override public Response intercept(Chain chain) throws IOException {
    ...
        streamAllocation.noNewStreams();
    ...
  }
 ...
}

StreamAllocation 的成员变量

  • 由构造方法中传入的变量:
    • public final Address address; 地址
    • private final ConnectionPool connectionPool; 连接池
    • public final Call call; 请求对象
    • public final EventListener eventListener; 事件回调
    • private final Object callStackTrace; 日志
    • private final RouteSelector routeSelector; 路由选择器
  • private RouteSelector.Selection routeSelection; 选中的路由集合
  • private Route route; 路由
  • private RealConnection connection; HTTP连接
  • private HttpCodec codec; HTTP请求的编码和响应的解码

StreamAllocation 的构造函数

RetryAndFollowUpInterceptor 中传入了 :

代码语言:javascript
复制
public StreamAllocation(ConnectionPool connectionPool, Address address, Call call,
                        EventListener eventListener, Object callStackTrace) {
    this.connectionPool = connectionPool;
    this.address = address;
    this.call = call;
    this.eventListener = eventListener;
    this.routeSelector = new RouteSelector(address, routeDatabase(), call, eventListener);
    this.callStackTrace = callStackTrace;
}

StreamAllocation 的相关方法

我们在简介中介绍 StreamAllocation 在哪里创建的时候,发现拦截器中主要调用的就是 newStream(...)noNewStreams() 这两个方法。那我们先来看看这两个方法吧。

newStream(...) 主要就是获取一个 可用的连接 和 对应连接协议的编解码实例,并赋值给变量 connectioncodec.

代码语言:javascript
复制
public HttpCodec newStream(...) {
  ...
  try {
    //在连接池中找到一个可用的连接  没有则创建一个
    RealConnection resultConnection = findHealthyConnection(connectTimeout, readTimeout,
        writeTimeout, pingIntervalMillis, connectionRetryEnabled, doExtensiveHealthChecks);
    //获取对应协议的 编解码类 Http1Codec or Http2Codec
    HttpCodec resultCodec = resultConnection.newCodec(client, chain, this);
    synchronized (connectionPool) {
      codec = resultCodec;
      return resultCodec;
    }
  } catch (IOException e) {
    throw new RouteException(e);
  }
}

noNewStreams() 主要就是设置当前的 connection 不能再创建新的流。

代码语言:javascript
复制
public void noNewStreams() {
  Socket socket;
  Connection releasedConnection;
  synchronized (connectionPool) {
    releasedConnection = connection;
    socket = deallocate(true, false, false);
    if (connection != null) releasedConnection = null;
  }
  closeQuietly(socket);
  if (releasedConnection != null) {
    eventListener.connectionReleased(call, releasedConnection);
  }
}

继续看下 newStream(...) 获取可用连接的流程。

findHealthyConnection(...) 通过findConnection(...)得到一个连接,如果是新创建的连接,则直接返回,否则检查 连接 是否已经可以开始承载新的流,不行则继续findConnection(...).

代码语言:javascript
复制
private RealConnection findHealthyConnection(...) throws IOException {
  while (true) {
    RealConnection candidate = findConnection(...);
    //如果是一个新创建的连接 则直接返回
    synchronized (connectionPool) {
      if (candidate.successCount == 0) {
        return candidate;
      }
    }
    //否则检查 连接 是否已经可以开始承载新的流
    if (!candidate.isHealthy(doExtensiveHealthChecks)) {
      noNewStreams();
      continue;
    }
    return candidate;
  }
}

findConnection(...)

  • 1.0 第一个 synchronized (connectionPool) 这一段首先尝试使用当前的变量connection. 如果当前的connectionnull, 则通过Internal.instance.get(connectionPool, address, this, null);连接池 中获取对应 address 的连接 赋值给 connection,如果当前的 connection不为null,则直接返回.
  • 2.0 如果上一步没有找到可用连接,则看是否有其他可用路由。
  • 3.0 第二个 synchronized (connectionPool) 这一段,3.1如果有其他路由则先去连接池查询看是否有对应连接, 3.2没有的话则创建一个新的 RealConnection,并赋值给变量 connection. 3.3在连接池中找到的话则直接返回。
  • 4.0 然后新创建的连接开始握手连接,然后放入连接池,然后返回。
代码语言:javascript
复制
private RealConnection findConnection(...) throws IOException {
  ...
  //1.0 尝试使用当前的 connection,或者查找 连接池
  synchronized (connectionPool) {
    ...
    releasedConnection = this.connection;
    if (this.connection != null) {
      result = this.connection;
      releasedConnection = null;
    }
    if (!reportedAcquired) {
      releasedConnection = null;
    }
    if (result == null) {
      Internal.instance.get(connectionPool, address, this, null);
      if (connection != null) {
        foundPooledConnection = true;
        result = connection;
      } else {
        selectedRoute = route;
      }
    }
  }
  ...
  if (result != null) {
    return result;
  }

  //2.0 看是否有其他的路由
  boolean newRouteSelection = false;
  if (selectedRoute == null && (routeSelection == null || !routeSelection.hasNext())) {
    newRouteSelection = true;
    routeSelection = routeSelector.next();
  }
  //3.0 
  synchronized (connectionPool) {
    if (newRouteSelection) {
      //3.1 有其他路由则先去连接池查询看是否有对应连接
    }
    if (!foundPooledConnection) {
      ...
      //3.2 没有的话则创建一个新的RealConnection,并赋值给变量 connection
      result = new RealConnection(connectionPool, selectedRoute);
      acquire(result, false);
    }
  }
  // 3.3 在连接池中找到的话则直接返回
  if (foundPooledConnection) {
    eventListener.connectionAcquired(call, result);
    return result;
  }
  //4.0 新创建的连接开始握手连接
  result.connect(...);
  synchronized (connectionPool) {
    reportedAcquired = true;
    Internal.instance.put(connectionPool, result);
    ...
  }
  ...
  return result;
}

流程图大概如下:

okhttp之StreamAllocation.findHealthyConnection(...)
okhttp之StreamAllocation.findHealthyConnection(...)

acquire(...)从连接池找到对应连接 赋值给StreamAllocation

代码语言:javascript
复制
public void acquire(RealConnection connection, boolean reportedAcquired) {
  assert (Thread.holdsLock(connectionPool));
  if (this.connection != null) throw new IllegalStateException();
  this.connection = connection;
  this.reportedAcquired = reportedAcquired;
  connection.allocations.add(new StreamAllocationReference(this, callStackTrace));
}

获取成员变量的一些方法.

代码语言:javascript
复制
public HttpCodec codec() {
  synchronized (connectionPool) {
    return codec;
  }
}
public Route route() {
  return route;
}
public synchronized RealConnection connection() {
  return connection;
}

release() 资源释放

代码语言:javascript
复制
public void release() {
  Socket socket;
  Connection releasedConnection;
  synchronized (connectionPool) {
    releasedConnection = connection;
    socket = deallocate(false, true, false);
    if (connection != null) releasedConnection = null;
  }
  closeQuietly(socket);
  if (releasedConnection != null) {
    Internal.instance.timeoutExit(call, null);
    eventListener.connectionReleased(call, releasedConnection);
    eventListener.callEnd(call);
  }
}

小结

  • 介绍了HTTP2HTTP1 的区别,HTTP2使用 多路复用 的技术,减少了同地址的TCP握手时间。
  • 介绍了 StreamAllocation 的主要方法newStream(...),以及其中获取可用 Connection 的具体逻辑.
  • newStream(...) 返回的 HttpCodec 这个 编解码的示例后面也会介绍。

参考文章

如果觉得不错的话,请帮忙点个赞。

以上

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-08,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 目录
  • 背景
  • 简介
  • StreamAllocation 的成员变量
  • StreamAllocation 的构造函数
  • StreamAllocation 的相关方法
  • 小结
  • 参考文章
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档