OkHttpClient限制连接数? [英] OkHttpClient limit number of connections?

查看:887
本文介绍了OkHttpClient限制连接数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OkHttpClient是否可以限制活动连接的数量?那么,如果达到限制,就不会选择并建立新的连接?

Is it possible with OkHttpClient to limit the number of live connections? So if limit reached, no new connection is picked and established?

我的应用程序同时启动许多连接.

My app starts many connection at same time.

推荐答案

连接数可在

The number of connections is configurable in the Dispatcher, not in the ConnectionPool that only allows to configure the max idle connections and the keep alive functionality.

分派器允许按主机配置连接数和最大连接数,默认值为每个主机5个,总共64个.对于HTTP/1来说,这似乎很低,但是如果您使用HTTP/2,因为可以将多个请求发送到一个连接,所以可以.

The dispatcher allows to configure the number of connections by hosts and the max number of connections, defaults are 5 per hosts and 64 in total. This can seems low for HTTP/1 but are OK if you use HTTP/2 as multiple requests can be send to one connection.

要配置调度程序,请按照下列步骤操作:

To configure the dispatcher, follow these steps :

Dispatcher dispatcher = new Dispatcher();
dispatcher.setMaxRequests(100);
dispatcher.setMaxRequestsPerHost(10);
OkHttpClient client = new OkHttpClient.Builder()
    .dispatcher(dispatcher)
    .build();

这篇关于OkHttpClient限制连接数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆