Skip to content

Commit 4167e05

Browse files
author
jordanqin
committed
update qcloud sdk to 1.5.55
1 parent e626c1e commit 4167e05

File tree

14 files changed

+149
-27
lines changed

14 files changed

+149
-27
lines changed

QCloudFoundation/.idea/gradle.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

QCloudFoundation/foundation/build.gradle

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

10-
versionCode 10553
11-
versionName "1.5.53"
10+
versionCode 10555
11+
versionName "1.5.55"
1212

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

@@ -62,6 +62,20 @@ project.extensions.add('archiveFilePath', 'outputs/aar/qcloud-foundation-release
6262

6363
apply from: '../../publishMavenCentral.gradle'
6464

65+
//发布本地repo
66+
apply plugin: 'maven'
67+
uploadArchives {
68+
repositories.mavenDeployer {
69+
pom.project {
70+
groupId 'com.qcloud.cos'
71+
artifactId 'qcloud-foundation'
72+
version android.defaultConfig.versionName
73+
packaging 'aar'
74+
}
75+
repository(url: uri("${rootProject.projectDir}/repo"))
76+
}
77+
}
78+
6579
/*
6680
* Copyright (c) 2010-2020 Tencent Cloud. All rights reserved.
6781
*

QCloudFoundation/foundation/src/main/java/com/tencent/qcloud/core/auth/BasicLifecycleCredentialProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public void refresh() throws QCloudClientException {
8686
}
8787
}
8888

89+
/**
90+
* 强制让凭证失效
91+
*/
92+
public synchronized void forceInvalidationCredential(){
93+
safeSetCredentials(null);
94+
}
95+
8996
private synchronized void safeSetCredentials(QCloudLifecycleCredentials credentials) {
9097
this.credentials = credentials;
9198
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public final class HttpResult<T> {
3333
private final String message;
3434
private final Map<String, List<String>> headers;
3535
private final HttpRequest<T> request;
36+
private final long contentLength;
3637

3738
private final T content;
3839

@@ -42,6 +43,7 @@ public HttpResult(HttpResponse<T> response, T content) {
4243
this.headers = response.response.headers().toMultimap();
4344
this.content = content;
4445
this.request = response.request;
46+
this.contentLength = response.contentLength();
4547
}
4648

4749
public T content() {
@@ -64,6 +66,10 @@ public Map<String, List<String>> headers() {
6466
return headers;
6567
}
6668

69+
public long getContentLength() {
70+
return contentLength;
71+
}
72+
6773
public final boolean isSuccessful() {
6874
return code >= 200 && code < 300;
6975
}

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

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public final class QCloudHttpClient {
6161
private final HttpLogger httpLogger;
6262

6363
private final Set<String> verifiedHost;
64+
6465
private final Map<String, List<InetAddress>> dnsMap;
66+
private final ArrayList<QCloudDnsFetch> dnsFetchs;
6567

6668
private final ConnectionRepository connectionRepository;
6769

@@ -96,8 +98,25 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
9698
dns = dnsMap.get(hostname);
9799
}
98100

101+
// 使用自定义Dns获取器
102+
// 根据添加的DnsFetch顺序进行获取
103+
if (dns == null || dns.size() == 0) {
104+
for (QCloudDnsFetch dnsFetch : dnsFetchs) {
105+
if(dnsFetch != null) {
106+
try {
107+
dns = dnsFetch.fetch(hostname);
108+
if (dns != null) {
109+
break;
110+
}
111+
} catch (UnknownHostException ignored) {
112+
113+
}
114+
}
115+
}
116+
}
117+
99118
// 然后使用系统的 dns
100-
if (dns == null) {
119+
if (dns == null || dns.size() == 0) {
101120
try {
102121
dns = Dns.SYSTEM.lookup(hostname);
103122
} catch (UnknownHostException e) {
@@ -106,20 +125,20 @@ public List<InetAddress> lookup(String hostname) throws UnknownHostException {
106125
}
107126
}
108127

109-
if (dns == null && !dnsCache) {
128+
if ((dns == null || dns.size() == 0) && !dnsCache) {
110129
throw new UnknownHostException("can not resolve host name " + hostname);
111130
}
112131

113132
// 最后使用缓存的 dns
114-
if (dns == null) {
133+
if (dns == null || dns.size() == 0) {
115134
try {
116135
dns = connectionRepository.getDnsRecord(hostname);
117136
} catch (UnknownHostException e) {
118137
QCloudLogger.w(HTTP_LOG_TAG, "Not found dns in cache records.");
119138
}
120139
}
121140

122-
if (dns != null) {
141+
if (dns != null && dns.size() > 0) {
123142
ConnectionRepository.getInstance().insertDnsRecordCache(hostname, dns);
124143
} else {
125144
throw new UnknownHostException(hostname);
@@ -154,6 +173,12 @@ public void addVerifiedHost(String hostname) {
154173
}
155174
}
156175

176+
/**
177+
* 添加自定义DNS记录
178+
* @param hostName host
179+
* @param ipAddress ip集合
180+
* @throws UnknownHostException 无法解析host异常
181+
*/
157182
public void addDnsRecord(@NonNull String hostName, @NonNull String[] ipAddress) throws UnknownHostException {
158183
if (ipAddress.length > 0) {
159184
List<InetAddress> addresses = new ArrayList<>(ipAddress.length);
@@ -164,13 +189,22 @@ public void addDnsRecord(@NonNull String hostName, @NonNull String[] ipAddress)
164189
}
165190
}
166191

192+
/**
193+
* 添加自定义Dns获取器
194+
* @param dnsFetch Dns获取器
195+
*/
196+
public void addDnsFetch(@NonNull QCloudDnsFetch dnsFetch){
197+
dnsFetchs.add(dnsFetch);
198+
}
199+
167200
public void setDebuggable(boolean debuggable) {
168201
httpLogger.setDebug(debuggable);
169202
}
170203

171204
private QCloudHttpClient(Builder b) {
172205
this.verifiedHost = new HashSet<>(5);
173206
this.dnsMap = new ConcurrentHashMap<>(3);
207+
this.dnsFetchs = new ArrayList<>(3);
174208
this.taskManager = TaskManager.getInstance();
175209
this.connectionRepository = ConnectionRepository.getInstance();
176210
httpLogger = new HttpLogger(false);
@@ -237,6 +271,19 @@ private <T> HttpTask<T> handleRequest(HttpRequest<T> request,
237271
return new HttpTask<T>(request, credentialProvider, networkClientMap.get(networkClientType.hashCode()));
238272
}
239273

274+
/**
275+
* DNS获取
276+
*/
277+
public interface QCloudDnsFetch {
278+
/**
279+
* 获取dns记录
280+
* @param hostname host
281+
* @return ip集合
282+
* @throws UnknownHostException 无法解析host异常
283+
*/
284+
List<InetAddress> fetch(String hostname) throws UnknownHostException;
285+
}
286+
240287
public final static class Builder {
241288
int connectionTimeout = 15 * 1000; //in milliseconds
242289
int socketTimeout = 30 * 1000; //in milliseconds

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,6 @@
2929
import java.io.IOException;
3030
import java.io.InputStream;
3131

32-
import okio.Buffer;
33-
import okio.Okio;
34-
import okio.Source;
35-
3632
/**
3733
* 响应体转换器
3834
* @param <T>
@@ -72,7 +68,7 @@ public byte[] convert(HttpResponse<byte[]> response) throws QCloudClientExceptio
7268
}
7369
}
7470

75-
private static final class InputStreamConverter extends ResponseBodyConverter<InputStream> {
71+
private static final class InputStreamConverter extends ResponseBodyConverter<InputStream> implements SelfCloseConverter {
7672

7773
@Override
7874
public InputStream convert(HttpResponse<InputStream> response) throws QCloudClientException, QCloudServiceException {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
package com.tencent.qcloud.core.http;
22

3+
/**
4+
* 安全关闭的Converter
5+
* 设置为SelfCloseConverter 网络请求后不再自动关闭response,需要业务自己进行关闭
6+
*/
37
public interface SelfCloseConverter {
48
}

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ public MediaType contentType() {
158158
@Override
159159
public long contentLength() throws IOException {
160160
long contentMaxLength = getContentRawLength();
161+
long contentLength;
161162
if (contentMaxLength <= 0) {
162-
return Math.max(requiredLength, -1);
163+
contentLength = Math.max(requiredLength, -1);
163164
} else if (requiredLength <= 0) {
164-
return Math.max(contentMaxLength - offset, -1);
165+
contentLength = Math.max(contentMaxLength - offset, -1);
165166
} else {
166-
return Math.min(contentMaxLength - offset, requiredLength);
167+
contentLength = Math.min(contentMaxLength - offset, requiredLength);
167168
}
169+
170+
// 此处考虑文件被修改,大小发生变化,contentMaxLength - offset为负数的情况,会导致400 Bad Request
171+
if(contentLength < 0){
172+
contentLength = -1;
173+
}
174+
return contentLength;
168175
}
169176

170177
protected long getContentRawLength() throws IOException {

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
import okhttp3.Response;
4242

4343
public class TrafficControlInterceptor implements Interceptor {
44-
// TODO: 2022/5/12 maxConcurrent为多少比较合适?
45-
private TrafficStrategy uploadTrafficStrategy = new ModerateTrafficStrategy("UploadStrategy-", 2);
46-
private TrafficStrategy downloadTrafficStrategy = new AggressiveTrafficStrategy("DownloadStrategy-", 3);
47-
4844
private static class ResizableSemaphore extends Semaphore {
4945

5046
ResizableSemaphore(int permit, boolean fair) {
@@ -172,7 +168,15 @@ private TrafficStrategy getSuitableStrategy(HttpTask task) {
172168
if (!task.isEnableTraffic()) {
173169
return null;
174170
}
175-
return task.isDownloadTask() ? downloadTrafficStrategy : task.isUploadTask() ? uploadTrafficStrategy : null;
171+
TrafficStrategy uploadTrafficStrategy = new ModerateTrafficStrategy("UploadStrategy-", task.getUploadMaxThreadCount());
172+
TrafficStrategy downloadTrafficStrategy = new AggressiveTrafficStrategy("DownloadStrategy-", task.getDownloadMaxThreadCount());
173+
if(task.isDownloadTask()){
174+
return downloadTrafficStrategy;
175+
} else if(task.isUploadTask()){
176+
return uploadTrafficStrategy;
177+
} else {
178+
return null;
179+
}
176180
}
177181

178182
private double getAverageStreamingSpeed(HttpTask task, long networkMillsTook) {

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public abstract class QCloudTask<T> implements Callable<T> {
7676

7777
private int weight = WEIGHT_LOW; //
7878
private boolean enableTraffic = true;
79+
private int uploadMaxThreadCount;
80+
private int downloadMaxThreadCount;
7981
private OnRequestWeightListener onRequestWeightListener;
8082

8183
private Executor observerExecutor;
@@ -220,6 +222,22 @@ public boolean isEnableTraffic() {
220222
return enableTraffic;
221223
}
222224

225+
public int getUploadMaxThreadCount() {
226+
return uploadMaxThreadCount;
227+
}
228+
229+
public void setUploadMaxThreadCount(int uploadMaxThreadCount) {
230+
this.uploadMaxThreadCount = uploadMaxThreadCount;
231+
}
232+
233+
public int getDownloadMaxThreadCount() {
234+
return downloadMaxThreadCount;
235+
}
236+
237+
public void setDownloadMaxThreadCount(int downloadMaxThreadCount) {
238+
this.downloadMaxThreadCount = downloadMaxThreadCount;
239+
}
240+
223241
/**
224242
* 任务是否已经取消
225243
*

0 commit comments

Comments
 (0)