Skip to content

Commit d92ad10

Browse files
committed
update qcloud sdk to 1.5.29
1 parent 721a8a9 commit d92ad10

File tree

10 files changed

+99
-40
lines changed

10 files changed

+99
-40
lines changed

QCloudFoundation/foundation/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
minSdkVersion 15
88
targetSdkVersion 27
99

10-
versionCode 10528
11-
versionName "1.5.28"
10+
versionCode 10529
11+
versionName "1.5.29"
1212

1313
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
1414

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/CallMetricsListener.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public class CallMetricsListener extends EventListener {
6565
*/
6666
private String domainName;
6767
private List<InetAddress> inetAddressList;
68-
private InetSocketAddress connectAddress;
68+
private InetAddress connectAddress;
6969

7070
private long requestBodyByteCount;
7171
private long responseBodyByteCount;
@@ -99,7 +99,6 @@ public void dnsEnd(Call call, String domainName, List<InetAddress> inetAddressLi
9999
@Override
100100
public void connectStart(Call call, InetSocketAddress inetSocketAddress, Proxy proxy) {
101101
super.connectStart(call, inetSocketAddress, proxy);
102-
connectAddress = inetSocketAddress;
103102
connectStartTime = System.nanoTime();
104103
}
105104

@@ -177,6 +176,13 @@ public void responseBodyEnd(Call call, long byteCount) {
177176
responseBodyByteCount = byteCount;
178177
}
179178

179+
public void recordConnection(InetAddress address) {
180+
if (address != null) {
181+
domainName = address.getHostName();
182+
connectAddress = address;
183+
}
184+
}
185+
180186
public void dumpMetrics(HttpTaskMetrics metrics) {
181187
metrics.domainName = domainName;
182188
metrics.remoteAddress = inetAddressList;
@@ -187,13 +193,9 @@ public void dumpMetrics(HttpTaskMetrics metrics) {
187193
metrics.writeRequestBodyTookTime += writeRequestBodyTookTime;
188194
metrics.readResponseHeaderTookTime += readResponseHeaderTookTime;
189195
metrics.readResponseBodyTookTime += readResponseBodyTookTime;
190-
metrics.connectAddress = connectAddress;
191196
metrics.requestBodyByteCount = requestBodyByteCount;
192197
metrics.responseBodyByteCount = responseBodyByteCount;
193-
}
194-
195-
public InetSocketAddress getConnectAddress() {
196-
return connectAddress;
198+
metrics.connectAddress = connectAddress;
197199
}
198200

199201
public List<InetAddress> dumpDns() {

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/ConnectionRepository.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public class ConnectionRepository {
5050

5151
private Map<String, List<InetAddress>> dnsRecords;
5252

53-
private Map<String, InetSocketAddress> connectAddressRecords;
54-
5553
private static volatile ConnectionRepository instance;
5654

5755
private LocalDnsCache localDnsCache;
@@ -77,7 +75,6 @@ private ConnectionRepository() {
7775
localDnsCache = new LocalDnsCache(ContextHolder.getAppContext());
7876
dnsFetcher = new DnsFetcher();
7977
dnsRecords = new ConcurrentHashMap<>();
80-
connectAddressRecords = new ConcurrentHashMap<>();
8178
singleExecutor = Executors.newSingleThreadExecutor();
8279
}
8380

@@ -106,14 +103,6 @@ public void insertDnsRecordCache(final String host, final List<InetAddress> inet
106103
insertDnsRecordCache(host, inetAddresses, null);
107104
}
108105

109-
public void setConnectAddress(String host, InetSocketAddress socketAddress) {
110-
connectAddressRecords.put(host, socketAddress);
111-
}
112-
113-
public @Nullable InetSocketAddress getConnectAddress(String host) {
114-
return connectAddressRecords.get(host);
115-
}
116-
117106
// public void deleteDnsRecordCache
118107

119108
/**

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/HttpTaskMetrics.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public class HttpTaskMetrics {
6868

6969
@Nullable String domainName;
7070
@Nullable List<InetAddress> remoteAddress;
71-
@Nullable InetSocketAddress connectAddress;
71+
@Nullable InetAddress connectAddress;
7272

7373
void onTaskStart() {
7474
fullTaskStartTime = System.nanoTime();
@@ -218,7 +218,7 @@ public double fullTaskTookTime() {
218218
return remoteAddress;
219219
}
220220

221-
@Nullable public InetSocketAddress getConnectAddress() {
221+
@Nullable public InetAddress getConnectAddress() {
222222
return connectAddress;
223223
}
224224

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/OkHttpClientImpl.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
package com.tencent.qcloud.core.http;
2424

25+
import com.tencent.qcloud.core.http.interceptor.HttpMetricsInterceptor;
2526
import com.tencent.qcloud.core.http.interceptor.RetryInterceptor;
2627
import com.tencent.qcloud.core.http.interceptor.TrafficControlInterceptor;
2728

@@ -30,6 +31,7 @@
3031
import javax.net.ssl.HostnameVerifier;
3132

3233
import okhttp3.Call;
34+
import okhttp3.ConnectionPool;
3335
import okhttp3.Dns;
3436
import okhttp3.OkHttpClient;
3537

@@ -60,6 +62,7 @@ public void init(QCloudHttpClient.Builder b, HostnameVerifier hostnameVerifier,
6062
.readTimeout(b.socketTimeout, TimeUnit.MILLISECONDS)
6163
.writeTimeout(b.socketTimeout, TimeUnit.MILLISECONDS)
6264
.eventListenerFactory(mEventListenerFactory)
65+
.addNetworkInterceptor(new HttpMetricsInterceptor())
6366
.addInterceptor(logInterceptor)
6467
.addInterceptor(new RetryInterceptor(b.retryStrategy))
6568
.addInterceptor(new TrafficControlInterceptor())

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/OkHttpProxy.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,6 @@ protected HttpResult<T> executeHttpRequest(HttpRequest<T> httpRequest) throws QC
8787

8888
if (eventListener != null) {
8989
eventListener.dumpMetrics(metrics);
90-
if (eventListener.getConnectAddress() != null) {
91-
ConnectionRepository.getInstance().setConnectAddress(httpRequest.host(),
92-
eventListener.getConnectAddress());
93-
}
9490
}
9591

9692
if (response != null) {
@@ -120,9 +116,9 @@ protected HttpResult<T> executeHttpRequest(HttpRequest<T> httpRequest) throws QC
120116
} else if (serviceException != null) {
121117
throw serviceException;
122118
} else {
123-
if (isCosResponse(response)) {
124-
recordDns(httpRequest.host(), eventListener);
125-
}
119+
// if (isCosResponse(response)) {
120+
// recordDns(httpRequest.host(), eventListener);
121+
// }
126122
return httpResult;
127123
}
128124
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/http/QCloudHttpClient.java

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,44 @@ public boolean verify(String hostname, SSLSession session) {
8686

8787
@Override
8888
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
89+
90+
List<InetAddress> dns = null;
91+
92+
// 首先使用用户设置的 dns
8993
if (dnsMap.containsKey(hostname)) {
90-
return dnsMap.get(hostname);
94+
dns = dnsMap.get(hostname);
9195
}
9296

93-
try {
94-
return Dns.SYSTEM.lookup(hostname);
95-
} catch (UnknownHostException e) {
96-
// e.printStackTrace();
97-
QCloudLogger.w(HTTP_LOG_TAG, "system dns failed, retry cache dns records.");
97+
// 然后使用系统的 dns
98+
if (dns == null) {
99+
try {
100+
dns = Dns.SYSTEM.lookup(hostname);
101+
} catch (UnknownHostException e) {
102+
// e.printStackTrace();
103+
QCloudLogger.w(HTTP_LOG_TAG, "system dns failed, retry cache dns records.");
104+
}
98105
}
99106

100-
if (!dnsCache) {
107+
if (dns == null && !dnsCache) {
101108
throw new UnknownHostException("can not resolve host name " + hostname);
102109
}
103110

104-
return connectionRepository.getDnsRecord(hostname);
111+
// 最后使用缓存的 dns
112+
if (dns == null) {
113+
try {
114+
dns = connectionRepository.getDnsRecord(hostname);
115+
} catch (UnknownHostException e) {
116+
QCloudLogger.w(HTTP_LOG_TAG, "Not found dns in cache records.");
117+
}
118+
}
119+
120+
if (dns != null) {
121+
ConnectionRepository.getInstance().insertDnsRecordCache(hostname, dns);
122+
} else {
123+
throw new UnknownHostException(hostname);
124+
}
125+
126+
return dns;
105127
}
106128
};
107129

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.tencent.qcloud.core.http.interceptor;
2+
3+
import com.tencent.qcloud.core.http.CallMetricsListener;
4+
5+
import java.io.IOException;
6+
import java.net.Socket;
7+
8+
import okhttp3.Connection;
9+
import okhttp3.EventListener;
10+
import okhttp3.Interceptor;
11+
import okhttp3.Request;
12+
import okhttp3.Response;
13+
import okhttp3.internal.connection.RealConnection;
14+
import okhttp3.internal.http.RealInterceptorChain;
15+
16+
/**
17+
* <p>
18+
* Created by rickenwang on 2021/4/25.
19+
* Copyright 2010-2021 Tencent Cloud. All Rights Reserved.
20+
*/
21+
public class HttpMetricsInterceptor implements Interceptor {
22+
23+
24+
@Override
25+
public Response intercept(Chain chain) throws IOException {
26+
27+
Request request = chain.request();
28+
29+
if (chain instanceof RealInterceptorChain) {
30+
RealInterceptorChain realChain = (RealInterceptorChain) chain;
31+
Connection connection = chain.connection();
32+
if (connection instanceof RealConnection) {
33+
RealConnection realConnection = (RealConnection) connection;
34+
Socket socket = realConnection.socket();
35+
EventListener eventListener = realChain.eventListener();
36+
if (eventListener instanceof CallMetricsListener && socket != null) {
37+
CallMetricsListener callMetricsListener = (CallMetricsListener) eventListener;
38+
callMetricsListener.recordConnection(socket.getInetAddress());
39+
}
40+
}
41+
}
42+
43+
return chain.proceed(request);
44+
}
45+
}

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/task/QCloudTask.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,10 @@ protected void onFailure() {
375375
for (QCloudResultListener resultListener : listeners) {
376376
if (exception instanceof QCloudClientException) {
377377
resultListener.onFailure((QCloudClientException) exception, null);
378-
} else {
378+
} else if (exception instanceof QCloudServiceException){
379379
resultListener.onFailure(null, (QCloudServiceException) exception);
380+
} else {
381+
resultListener.onFailure(new QCloudClientException(exception.getCause()), null);
380382
}
381383
}
382384
}

QCloudFoundation/quic/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ android {
3131
minSdkVersion 15
3232
targetSdkVersion 27
3333

34-
versionCode 10528
34+
versionCode 10529
3535

36-
versionName "1.5.28"
36+
versionName "1.5.29"
3737

3838
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
3939

0 commit comments

Comments
 (0)