OkHttp并发连接异常:Android应用并行请求延迟问题排查
OkHttp并行请求启动延迟问题
在Android应用中使用OkHttp时遇到了并行请求异常:应用刚启动时同时发起多个请求,部分请求(如第二个)会出现启动延迟;当增加请求数量后,多数请求可并发执行,但仍有少数请求存在启动延迟。
代码示例
lifecycleScope.launch { delay(5000) val jobs= (0 until 2).map { async { homeRemoteDataSource.getEnums() } } jobs.awaitAll() }
OkHttp客户端配置
val client = OkHttpClient().newBuilder() client.connectTimeout(60, TimeUnit.SECONDS) client.readTimeout(60, TimeUnit.SECONDS) client.addInterceptor(authorization) client.addInterceptor(contentType) client.dispatcher(Dispatcher().apply { maxRequests = 100 maxRequestsPerHost = 100 }) client.connectionPool(ConnectionPool(maxIdleConnections = 10, keepAliveDuration = 5,TimeUnit.MINUTES)) client.build()
相关截图
内容的提问来源于stack exchange,提问作者Vikas Godara
相关产品推荐
相关产品推荐





