zoukankan      html  css  js  c++  java
  • okhttp——3. 连接池复用

    Connection

    首先看一下okhttp如何定义连接,Connection接口可略知一二

    public interface Connection {
      /** Returns the route used by this connection. */
      Route route();
    
      /**
       * Returns the socket that this connection is using. Returns an {@linkplain
       * javax.net.ssl.SSLSocket SSL socket} if this connection is HTTPS. If this is an HTTP/2
       * connection the socket may be shared by multiple concurrent calls.
       */
      Socket socket();
    
      /**
       * Returns the TLS handshake used to establish this connection, or null if the connection is not
       * HTTPS.
       */
      @Nullable Handshake handshake();
    
      /**
       * Returns the protocol negotiated by this connection, or {@link Protocol#HTTP_1_1} if no protocol
       * has been negotiated. This method returns {@link Protocol#HTTP_1_1} even if the remote peer is
       * using {@link Protocol#HTTP_1_0}.
       */
      Protocol protocol();
    }
    

    在okhttp中实现了Connection接口的类是RealConnection, 里面涉及到了如何建立一次连接。

    ConnectionPool

    该类用于管理所有网络请求的连接,对于满足特定条件的连接进行复用。

    1. 构造函数:
    // maxIdleConnections: 单个url最大的连接数 
    // keepAliveDuration: 单个连接的有效期  timeUnit: 有效期的时间单位
    
    public ConnectionPool(int maxIdleConnections, long keepAliveDuration, TimeUnit timeUnit) {
        this.maxIdleConnections = maxIdleConnections;
        this.keepAliveDurationNs = timeUnit.toNanos(keepAliveDuration);
    
        // Put a floor on the keep alive duration, otherwise cleanup will spin loop.
        if (keepAliveDuration <= 0) {
          throw new IllegalArgumentException("keepAliveDuration <= 0: " + keepAliveDuration);
        }
      }
    
      // okhttp 使用如下默认值
      public ConnectionPool() {
        this(5, 5, TimeUnit.MINUTES);
      }
    

    ConnectionPool 的构造只会在OkhttpClient的Builder里面创建一次,其它所有类持有的ConnectionPool都是通过参数传递过去的。

    重要成员:

    // 用于存放连接的双向队列
    private final Deque<RealConnection> connections = new ArrayDeque<>();
    

    获取可重用的连接

     /**
       * Returns a recycled connection to {@code address}, or null if no such connection exists. The
       * route is null if the address has not yet been routed.
       */
      @Nullable RealConnection get(Address address, StreamAllocation streamAllocation, Route route) {
        assert (Thread.holdsLock(this));
        for (RealConnection connection : connections) {
          if (connection.isEligible(address, route)) {
            streamAllocation.acquire(connection);
            return connection;
          }
        }
        return null;
      }
    
    梦想不是浮躁,而是沉淀和积累
  • 相关阅读:
    使用WCF 测试客户端测试你的WCF服务
    使用Fiddler解析WCF RIA Service传输的数据
    SqlServer中BULK INSERT用法简介
    Silverlight的安全性初探
    使用Apache cxf 和Spring在Tomcat下发布Webservice指南
    Spring 3 MVC And XML Example
    设计与开发 JAXWS 2.0 Web 服务
    如何高效、准确、自动识别网页编码
    Spring 3 MVC And RSS Feed Example
    Spring 3 MVC And JSON Example
  • 原文地址:https://www.cnblogs.com/NeilZhang/p/12744156.html
Copyright © 2011-2022 走看看