当前位置: 首页 > news >正文

okhttp系列-一些上限值

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

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

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 removei.remove();//callsPerHost+1asyncCall.callsPerHost().incrementAndGet();//添加到executableCallsexecutableCalls.add(asyncCall);//添加到runningAsyncCallsrunningAsyncCalls.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。

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的callsPerHostcall.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;}
}

http://www.lryc.cn/news/265807.html

相关文章:

  • C++面向对象(OOP)编程-STL详解(vector)
  • postman几种常见的请求方式
  • openai最新探索:超级对齐是否可行?
  • 本地websocket服务端结合cpolar内网穿透实现公网访问
  • 关于“Python”的核心知识点整理大全37
  • Vivado中的FFT IP核使用(含代码)
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • 什么是“人机协同”机器学习?
  • 数学建模笔记-拟合算法
  • 非线性约束的优化问题_序列二次规划算法代码
  • 【数据结构之顺序表】
  • junit-mock-dubbo
  • json解析之fastjson和jackson使用对比
  • 设计模式之-模板方法模式,通俗易懂快速理解,以及模板方法模式的使用场景
  • 微软官方出品:GPT大模型编排工具,支持C#、Python等多个语言版本
  • docker安装的php 在cli中使用
  • tcp vegas 为什么好
  • 【设计模式】命令模式
  • Unity头发飘动效果
  • 【MIKE】MIKE河网编辑器操作说明
  • RIPV1配置实验
  • 快速实现农业机械设备远程监控
  • 解决用Fiddler抓包,网页显示你的连接不是专用/私密连接
  • 单片机原理及应用:流水灯的点亮
  • 蓝桥杯宝藏排序算法(冒泡、选择、插入)
  • 使用@jiaminghi/data-view实现一个数据大屏
  • 神经网络:池化层知识点
  • 微服务常见的配置中心简介
  • 银河麒麟v10 rpm安装包 安装mysql 8.35
  • 一篇文章带你搞定CTFMice基本操作