OkHttpClient limit number of connections?

前端 未结 2 740
小鲜肉
小鲜肉 2021-01-17 14:58

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 start

2条回答
  •  时光取名叫无心
    2021-01-17 15:26

    You could attempt to configure the maximum number of idle network connections by setting a ConnectionPool on your OkHttpClient.Builder.

    int maxConnections = 5;
    int keepAliveDuration = 15000;
    ConnectionPool cp = new ConnectionPool(maxConnections, keepAliveDuration, TimeUnit.MILLISECONDS);
    
    new OkHttpClient.Builder()
        .connectionPool(cp)
        .build();
    

提交回复
热议问题