okhttp系列-一些上限值

1.正在执行的任务数量最大值是64

异步请求放入readyAsyncCalls后,遍历readyAsyncCalls取出任务去执行的时候,如果发现runningAsyncCalls的数量大于等于64,就不从readyAsyncCalls取出任务执行。

java 复制代码
public final class Dispatcher {
    private int maxRequests = 64;

    private final Deque<AsyncCall> runningAsyncCalls = new ArrayDeque<>();

    private boolean promoteAndExecute() {
    assert (!Thread.holdsLock(this));

    List<AsyncCall> executableCalls = new ArrayList<>();
    boolean isRunning;
    synchronized (this) {
      for (Iterator<AsyncCall> i = readyAsyncCalls.iterator(); i.hasNext(); ) {
        AsyncCall asyncCall = i.next();
        //如果超过了最大数目
        if (runningAsyncCalls.size() >= maxRequests) break; // Max capacity.

        if (asyncCall.callsPerHost().get() >= maxRequestsPerHost) continue; // Host max capacity.
        //从readyAsyncCalls remove
        i.remove();

        //callsPerHost+1
        asyncCall.callsPerHost().incrementAndGet();
        //添加到executableCalls
        executableCalls.add(asyncCall);
        //添加到runningAsyncCalls
        runningAsyncCalls.add(asyncCall);
      }
      isRunning = runningCallsCount() > 0;
    }

    for (int i = 0, size = executableCalls.size(); i < size; i++) {
      AsyncCall asyncCall = executableCalls.get(i);
      //执行
      asyncCall.executeOn(executorService());
    }

    return isRunning;
  }
}

2.同一个主机的最大连接数为5

异步请求放入readyAsyncCalls后,遍历readyAsyncCalls取出任务去执行的时候,如果发现asyncCall的callsPerHost大于等于5,就不从readyAsyncCalls取出任务执行;否则callsPerHost加1。

java 复制代码
public final class Dispatcher {
    private int maxRequestsPerHost = 5; //默认5。这是okhttp对同一主机允许的最大请求数量。

    void enqueue(AsyncCall call) {
        synchronized (this) {
            readyAsyncCalls.add(call);

            //Mutate the AsyncCall so that it shares the AtomicInteger 
            //of an existing running call to the same host.
            if (!call.get().forWebSocket) {
                //从已经存在的任务里面找同一个主机的任务
                AsyncCall existingCall = findExistingCallWithHost(call.host());
                if (existingCall != null) {
                    //call的将callsPerHost赋值为existingCall的callsPerHost
                    call.reuseCallsPerHostFrom(existingCall);
                }
            }
        }
        promoteAndExecute();
    }

    //有个疑问,这里是不是要从ArrayDeque尾向前获取,才能获取到最新的AsyncCall,这样获取到的
    //callsPerHost才会是最大的?
    //目前从头开始获取,是不是有问题?
    //先从runningAsyncCalls找,再从readyAsyncCalls找
    @Nullable 
    private AsyncCall findExistingCallWithHost(String host) {
        for (AsyncCall existingCall : runningAsyncCalls) {
            if (existingCall.host().equals(host)) {
                return existingCall;
            }
        }
        for (AsyncCall existingCall : readyAsyncCalls) {
            if (existingCall.host().equals(host)) {
                return existingCall;
            }
        }
        return null;
    }
}
相关推荐
清风细雨_林木木17 小时前
wangEditor 编辑器 Vue 2.0 + Nodejs 配置
vue.js·okhttp·编辑器
WeiLai11125 天前
DeepSeekApi对接流式输出异步聊天功能:基于Spring Boot和OkHttp的SSE应用实现
人工智能·spring boot·后端·okhttp
闲暇部落5 天前
Android网络框架——OKHttp
android·java·okhttp
网络安全(华哥)7 天前
CSRF攻击&XSS攻击
okhttp·xss·csrf
不亭7 天前
python自动化测试之统一请求封装及通过文件实现接口关联
okhttp
2501_9032386511 天前
PrimeFaces Poll组件实现周期性Ajax调用
前端·ajax·okhttp·个人开发
张小勇11 天前
fetch请求总结,fastadmin中后台接口强制返回json数据
okhttp·json
ianozo16 天前
BUU17 [RoarCTF 2019]Easy Calc1
okhttp
袁震22 天前
Android-okhttp详解
android·okhttp
m0_7482565622 天前
跨域问题解释及前后端解决方案(SpringBoot)
spring boot·后端·okhttp