「Update」

1、增加Okhttp网络请求每个节点的日志回调
2、修改Okhttp的超时时间为总体20秒
CONNECT_TIMEOUT = 10000L
READ_TIMEOUT = 5000L
WRITE_TIMEOUT = 5000L
This commit is contained in:
donghongyu
2024-09-10 11:21:21 +08:00
parent abc02d6c81
commit b683b5dccf
6 changed files with 299 additions and 55 deletions

View File

@@ -7,9 +7,18 @@ package com.mogo.cloud.network
class NetConstants {
companion object {
const val TAG = "LogInterceptor"
const val READ_TIMEOUT = 20000L
const val WRITE_TIMEOUT = 20000L
// 这个超时设置决定了客户端等待与服务器建立连接的最大时间。
// 如果在这个时间段内连接未能建立那么请求将失败并抛出一个SocketTimeoutException。
// 这个阶段包括域名解析、TCP握手以及SSL握手如果使用的是HTTPS的话
const val CONNECT_TIMEOUT = 10000L
// 当连接已经建立之后readTimeout 设置了从服务器读取数据的最大时间。
// 这意味着客户端在等待从服务器接收响应体中的数据时如果超过了这个时间限制也会抛出SocketTimeoutException。
// 此设置对于长时间运行的服务端事件如HTTP长轮询尤其重要。
const val READ_TIMEOUT = 5000L
// 类似于readTimeoutwriteTimeout 控制着向服务器写入数据的最大时间。
// 如果客户端在发送请求体时遇到问题并且超过了这个设定的时间那么同样会抛出SocketTimeoutException。
// 这对于大文件上传或者需要发送大量数据的情况尤为重要。
const val WRITE_TIMEOUT = 5000L
const val DEVA_HOST = "http://dzt-deva.zhidaozhixing.com"
const val LAUNCHER_SNAPSHOT_HOST = "http://dzt-launcherSnapshot.zhidaozhixing.com"

View File

@@ -26,9 +26,9 @@ class OkHttpFactory private constructor() {
val okHttpClient: OkHttpClient by lazy {
//自定义上限,所有总数的最大上限
dispatcher.setMaxRequests(3000000);
dispatcher.setMaxRequests(3000000)
//自定义上限单个IP+端口的限制
dispatcher.setMaxRequestsPerHost(1000000);
dispatcher.setMaxRequestsPerHost(1000000)
OkHttpClient.Builder()
.addInterceptor(HttpHeaderInterceptor())

View File

@@ -1,9 +1,11 @@
package com.mogo.cloud.network
import okhttp3.*
import com.mogo.cloud.network.listener.OkHttpEventListener
import okhttp3.Call
import okhttp3.EventListener.Factory
import okhttp3.Protocol
import okhttp3.Response
import java.io.IOException
import java.net.InetAddress
import java.net.InetSocketAddress
import java.net.Proxy
@@ -20,7 +22,10 @@ class WeakNetworkEventFactory {
}
}
private class WeakNetworkEventListener: EventListener() {
/**
* 弱网络事件监听器
*/
private class WeakNetworkEventListener : OkHttpEventListener() {
private var timeStamp: Long = 0
@@ -29,18 +34,6 @@ class WeakNetworkEventFactory {
timeStamp = System.currentTimeMillis()
}
override fun dnsStart(call: Call, domainName: String) {
super.dnsStart(call, domainName)
}
override fun dnsEnd(
call: Call,
domainName: String,
inetAddressList: MutableList<InetAddress>
) {
super.dnsEnd(call, domainName, inetAddressList)
}
override fun connectFailed(
call: Call,
inetSocketAddress: InetSocketAddress,
@@ -49,42 +42,29 @@ class WeakNetworkEventFactory {
ioe: IOException
) {
super.connectFailed(call, inetSocketAddress, proxy, protocol, ioe)
listener?.onFailEvent(this.hashCode(), call.request().url().toString(), System.currentTimeMillis() - timeStamp)
}
override fun responseHeadersStart(call: Call) {
super.responseHeadersStart(call)
listener?.onFailEvent(
this.hashCode(),
call.request().url().toString(),
System.currentTimeMillis() - timeStamp
)
}
override fun responseHeadersEnd(call: Call, response: Response) {
super.responseHeadersEnd(call, response)
listener?.onHttpRttReceived(this.hashCode(), call.request().url().toString(), System.currentTimeMillis() - timeStamp)
listener?.onHttpRttReceived(
this.hashCode(),
call.request().url().toString(),
System.currentTimeMillis() - timeStamp
)
}
override fun responseBodyStart(call: Call) {
super.responseBodyStart(call)
}
override fun responseBodyEnd(call: Call, byteCount: Long) {
super.responseBodyEnd(call, byteCount)
}
override fun connectionReleased(call: Call, connection: Connection) {
super.connectionReleased(call, connection)
}
override fun callFailed(call: Call, ioe: IOException) {
super.callFailed(call, ioe)
}
override fun callEnd(call: Call) {
super.callEnd(call)
}
}
internal interface OnHttpRttListener {
fun onHttpRttReceived(hashCode: Int, url: String, timeStamp: Long)
fun onFailEvent(hashCode: Int, url: String, timeStamp: Long)
fun logMethod(name: String, startTime: Long)
}
}

View File

@@ -47,6 +47,10 @@ object WeakNetworkManager {
weakHttpListener?.onWeakNetworkEvent()
}
}
override fun logMethod(name: String, startTime: Long) {
weakHttpListener?.logMethod(name, startTime)
}
}
private fun increment(): Long {
@@ -80,5 +84,6 @@ object WeakNetworkManager {
fun onHttpRttReceived(hashCode: Int, url: String, timeStamp: Long)
fun onFailEvent(hashCode: Int, url: String, timeStamp: Long, currentFailCount: Long)
fun onWeakNetworkEvent()
fun logMethod(name: String, startTime: Long)
}
}

View File

@@ -0,0 +1,250 @@
package com.mogo.cloud.network.listener;
import android.util.Log;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.List;
import okhttp3.Call;
import okhttp3.Connection;
import okhttp3.EventListener;
import okhttp3.Handshake;
import okhttp3.Protocol;
import okhttp3.Request;
import okhttp3.Response;
/**
* EventListener监听实现类
* HTTP请求的基本链路
* 整个过程中DNS 解析、建立 TCP 连接、发送请求、服务器处理请求和发送响应、接收响应等环节都涉及到协议层和网络层的操作。
* HTTP 协议定义了请求和响应的格式,而 TCP/IP 协议栈提供了底层的网络传输支持
* <p>
* DNS 解析: 客户端首先通过 DNS 解析获取服务器的 IP 地址,将主机名转换为 IP 地址
* 建立 TCP 连接: 客户端通过 IP 地址和端口号与服务器建立 TCP 连接。这个过程通常包括 TCP 的三次握手
* 组装 HTTP 请求: 客户端构建 HTTP 请求报文包括请求行方法、URL、协议版本、请求头Headers、请求体如果是 POST 请求等带有主体的请求)等
* 发送请求: 客户端将构建好的 HTTP 请求报文通过建立的 TCP 连接发送给服务器
* 服务器处理请求: 服务器收到请求后,根据请求报文中的信息进行相应的处理。这可能包括解析请求、执行请求对应的处理逻辑、生成响应内容等
* 服务器发送响应: 服务器构建 HTTP 响应报文包括状态行协议版本、状态码、状态消息、响应头Headers、响应体实际的响应内容
* 接收响应: 客户端通过 TCP 连接接收服务器发送的 HTTP 响应报文
* 解析响应: 客户端解析响应报文,获取状态码、响应头和响应体等信息
* 关闭连接: 根据 HTTP 版本和服务器端的要求,决定是否保持连接或断开连接。在 HTTP/1.1 中,默认是保持连接,而在 HTTP/1.0 中默认是断开连接
* 处理响应: 客户端根据收到的响应进行相应的处理,可能包括渲染页面、执行后续操作等
*/
public class OkHttpEventListener extends EventListener {
// 请求开始
private long mCallStartTime;
// DNS开始
private long mDnsStartTime;
// 建立 TCP 连接
private long mConnectStartTime;
// TLS安全连接开始
private long mSecureConnectStartTime;
// 连接绑定开始
private long mConnectionStartTime;
// 请求Header开始
private long mRequestHeadersStartTime;
// 请求Body开始
private long mRequestBodyStartTime;
// 响应Header开始
private long mResponseHeadersStartTime;
// 响应Body开始
private long mResponseBodyStartTime;
/**
* 请求开始
*/
@Override
public void callStart(Call call) {
super.callStart(call);
mCallStartTime = System.nanoTime();
}
/**
* 请求正常结束
*/
@Override
public void callEnd(Call call) {
super.callEnd(call);
logMethod(call.request().toString() + "方法的 callEnd", mCallStartTime);
}
/**
* 请求异常结束
*/
@Override
public void callFailed(Call call, IOException ioe) {
super.callFailed(call, ioe);
logMethod(call.request().toString() + "\n方法的 callFailed", mCallStartTime);
}
/**
* dns解析开始
* DNS解析是请求DNSDomain Name System服务器。将域名解析成ip的过程。
*/
@Override
public void dnsStart(Call call, String domainName) {
super.dnsStart(call, domainName);
mDnsStartTime = System.nanoTime();
}
/**
* dns解析结束
*/
@Override
public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressList) {
super.dnsEnd(call, domainName, inetAddressList);
logMethod(call.request().toString() + "方法的 dnsEnd", mDnsStartTime);
}
/**
* 连接开始
*/
@Override
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
super.connectStart(call, inetSocketAddress, proxy);
mConnectStartTime = System.nanoTime();
}
/**
* 连接结束
*/
@Override
public void connectEnd(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol) {
super.connectEnd(call, inetSocketAddress, proxy, protocol);
logMethod(call.request().toString() + "方法的 connectEnd", mConnectStartTime);
}
/**
* 连接失败
*/
@Override
public void connectFailed(Call call, InetSocketAddress inetSocketAddress, Proxy proxy, Protocol protocol, IOException ioe) {
super.connectFailed(call, inetSocketAddress, proxy, protocol, ioe);
logMethod(call.request().toString() + "方法的 connectFailed", mConnectStartTime);
}
/**
* TLS安全连接开始
*/
@Override
public void secureConnectStart(Call call) {
super.secureConnectStart(call);
mSecureConnectStartTime = System.nanoTime();
}
/**
* TLS安全连接结束
*/
@Override
public void secureConnectEnd(Call call, Handshake handshake) {
super.secureConnectEnd(call, handshake);
logMethod(call.request().toString() + "方法的 secureConnectEnd", mSecureConnectStartTime);
}
/**
* 连接绑定
*/
@Override
public void connectionAcquired(Call call, Connection connection) {
super.connectionAcquired(call, connection);
mConnectionStartTime = System.nanoTime();
}
/**
* 连接释放
*/
@Override
public void connectionReleased(Call call, Connection connection) {
super.connectionReleased(call, connection);
logMethod(call.request().toString() + "方法的 connectionReleased", mConnectionStartTime);
}
/**
* 请求Header开始
*/
@Override
public void requestHeadersStart(Call call) {
super.requestHeadersStart(call);
mRequestHeadersStartTime = System.nanoTime();
}
/**
* 请求Header结束
*/
@Override
public void requestHeadersEnd(Call call, Request request) {
super.requestHeadersEnd(call, request);
logMethod(call.request().toString() + "方法的 requestHeadersEnd", mRequestHeadersStartTime);
}
/**
* 请求Body开始
*/
@Override
public void requestBodyStart(Call call) {
super.requestBodyStart(call);
mRequestBodyStartTime = System.nanoTime();
}
/**
* 请求Body结束
*/
@Override
public void requestBodyEnd(Call call, long byteCount) {
super.requestBodyEnd(call, byteCount);
logMethod(call.request().toString() + "方法的 requestBodyEnd", mRequestBodyStartTime);
}
/**
* 响应Header开始
*/
@Override
public void responseHeadersStart(Call call) {
super.responseHeadersStart(call);
mResponseHeadersStartTime = System.nanoTime();
}
/**
* 响应Header结束
*/
@Override
public void responseHeadersEnd(Call call, Response response) {
super.responseHeadersEnd(call, response);
logMethod(call.request().toString() + "方法的 responseHeadersEnd", mResponseHeadersStartTime);
}
/**
* 响应Body开始
*/
@Override
public void responseBodyStart(Call call) {
super.responseBodyStart(call);
mResponseBodyStartTime = System.nanoTime();
}
/**
* 响应Body结束
*/
@Override
public void responseBodyEnd(Call call, long byteCount) {
super.responseBodyEnd(call, byteCount);
logMethod(call.request().toString() + "方法的 responseBodyEnd", mResponseBodyStartTime);
}
/**
* 打印公共方法
*/
private void logMethod(String name, long startTime) {
long time = System.nanoTime() - startTime;
// 对超出1秒的网络请求打印日志
if (time > 1000000000d) {
Log.d("OkHttpOptimizeActivity", name + " 方法 耗时:" + (time / 1000000000d) + "");
}
}
}

View File

@@ -36,24 +36,24 @@ PASSWORD=xintai2018
RELEASE=true
# AI CLOUD 云平台
# 工具类
MOGO_UTILS_VERSION=1.4.7.33
MOGO_UTILS_VERSION=1.4.7.35
# 网络请求
MOGO_NETWORK_VERSION=1.4.7.33
MOGO_NETWORK_VERSION=1.4.7.35
# 鉴权
MOGO_PASSPORT_VERSION=1.4.7.33
MOGO_PASSPORT_VERSION=1.4.7.35
# 常链接
MOGO_SOCKET_VERSION=1.4.7.33
MOGO_SOCKET_VERSION=1.4.7.35
# 数据采集
MOGO_REALTIME_VERSION=1.4.7.33
MOGO_REALTIME_VERSION=1.4.7.35
# 探路,道路事件发布,获取
MOGO_TANLU_VERSION=1.4.7.33
MOGO_TANLU_VERSION=1.4.7.35
# 直播推流
MOGO_LIVE_VERSION=1.4.7.33
MOGO_LIVE_VERSION=1.4.7.35
# 直播拉流
MOGO_TRAFFICLIVE_VERSION=1.4.7.33
MOGO_TRAFFICLIVE_VERSION=1.4.7.35
# 定位服务
MOGO_LOCATION_VERSION=1.4.7.33
MOGO_LOCATION_VERSION=1.4.7.35
# 远程通讯模块
MOGO_TELEMATIC_VERSION=1.4.7.33
MOGO_TELEMATIC_VERSION=1.4.7.35
# v2x
MOGO_V2X_VERSION=1.4.7.33
MOGO_V2X_VERSION=1.4.7.35