Skip to content

Commit 9948c04

Browse files
author
yangsen
committed
optimize caculate speed & get request server logic
1 parent e9f6774 commit 9948c04

File tree

10 files changed

+77
-44
lines changed

10 files changed

+77
-44
lines changed

.idea/gradle.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

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

QiNiu_Android.iml

Lines changed: 0 additions & 19 deletions
This file was deleted.

library/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import java.util.regex.Matcher
33
apply plugin: 'com.android.library'
44

55
def versionName() {
6-
String config = 'library/src/main/java/com/qiniu/android/common/Constants.java'
6+
String config = getProjectDir().getPath() + '/src/main/java/com/qiniu/android/common/Constants.java'
77
String fileContents = new File(config).text
88
Matcher myMatcher = fileContents =~ /VERSION = "(.+)";/
99
String version = myMatcher[0][1]

library/src/main/java/com/qiniu/android/http/networkStatus/NetworkStatusManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,15 @@ public synchronized void initData() {
4141
networkStatusManager.asyncRecoverNetworkStatusFromDisk();
4242
}
4343

44+
@Deprecated
4445
public static String getNetworkStatusType(String host, String ip) {
4546
return Utils.getIpType(ip, host);
4647
}
4748

49+
public static String getNetworkStatusType(String httpVersion, String host, String ip) {
50+
return Utils.getIpType(httpVersion, ip, host);
51+
}
52+
4853
public NetworkStatus getNetworkStatus(String type) {
4954
if (type == null || type.length() == 0) {
5055
return null;
@@ -165,7 +170,7 @@ private synchronized void setupRecorder() {
165170

166171
public static class NetworkStatus {
167172

168-
private int speed;
173+
private int speed = 600;
169174

170175
public int getSpeed() {
171176
return speed;

library/src/main/java/com/qiniu/android/http/networkStatus/UploadServerNetworkStatus.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ public static IUploadServer getBetterNetworkServer(IUploadServer serverA, IUploa
88
return isServerNetworkBetter(serverA, serverB) ? serverA : serverB;
99
}
1010

11+
// 如果两个 Server 网速相同优先使用 serverA
1112
public static boolean isServerNetworkBetter(IUploadServer serverA, IUploadServer serverB) {
1213
if (serverA == null) {
1314
return false;
1415
} else if (serverB == null) {
1516
return true;
1617
}
1718

18-
String serverTypeA = NetworkStatusManager.getNetworkStatusType(serverA.getHost(), serverA.getIp());
19-
String serverTypeB = NetworkStatusManager.getNetworkStatusType(serverB.getHost(), serverB.getIp());
20-
if (serverTypeA == null) {
19+
String serverTypeA = NetworkStatusManager.getNetworkStatusType(serverA.getHttpVersion(), serverA.getHost(), serverA.getIp());
20+
String serverTypeB = NetworkStatusManager.getNetworkStatusType(serverA.getHttpVersion(), serverB.getHost(), serverB.getIp());
21+
if (serverTypeA == null || serverTypeA.length() == 0) {
2122
return false;
22-
} else if (serverTypeB == null) {
23+
} else if (serverTypeB == null || serverTypeB.length() == 0) {
2324
return true;
2425
}
2526

library/src/main/java/com/qiniu/android/http/request/HttpSingleRequest.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,36 @@ private void updateHostNetworkStatus(ResponseInfo responseInfo, IUploadServer se
219219
return;
220220
}
221221
long byteCount = requestMetrics.bytesSend();
222-
long second = requestMetrics.totalElapsedTime();
223-
if (second > 0 && byteCount >= 1024 * 1024) {
224-
int speed = (int) (byteCount * 1000 / second);
225-
String type = NetworkStatusManager.getNetworkStatusType(server.getHost(), server.getIp());
226-
NetworkStatusManager.getInstance().updateNetworkStatus(type, speed);
222+
long milliSecond = requestMetrics.totalElapsedTime();
223+
if (milliSecond <= 0) {
224+
return;
225+
}
226+
227+
if (byteCount <= 8 * 1024) {
228+
milliSecond = (long)((float)milliSecond * 0.08);
229+
} else if (byteCount <= 16 * 1024) {
230+
milliSecond = (long)((float)milliSecond * 0.15);
231+
} else if (byteCount <= 32 * 1024) {
232+
milliSecond = (long)((float)milliSecond * 0.22);
233+
} else if (byteCount <= 64 * 1024) {
234+
milliSecond = (long)((float)milliSecond * 0.30);
235+
} else if (byteCount <= 128 * 1024) {
236+
milliSecond = (long)((float)milliSecond * 0.45);
237+
} else if (byteCount <= 256 * 1024) {
238+
milliSecond = (long)((float)milliSecond * 0.73);
239+
} else if (byteCount <= 512 * 1024) {
240+
milliSecond = (long)((float)milliSecond * 0.86);
241+
} else if (byteCount <= 1024 * 1024) {
242+
milliSecond = (long)((float)milliSecond * 0.95);
227243
}
244+
245+
if (milliSecond <= 0) {
246+
milliSecond = 10;
247+
}
248+
249+
int speed = (int) (byteCount / milliSecond);
250+
String type = NetworkStatusManager.getNetworkStatusType(server.getHttpVersion(), server.getHost(), server.getIp());
251+
NetworkStatusManager.getInstance().updateNetworkStatus(type, speed);
228252
}
229253

230254
private void updateHttpServerInfo(IUploadServer server, ResponseInfo responseInfo) {

library/src/main/java/com/qiniu/android/http/serverRegion/UploadDomainRegion.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ public IUploadServer getNextServer(UploadRequestState requestState, ResponseInfo
126126

127127
freezeServerIfNeed(responseInfo, freezeServer);
128128

129-
UploadServer server = null;
130129
ArrayList<String> hostList = domainHostList;
131130
HashMap<String, UploadServerDomain> domainInfo = domainHashMap;
132131
if (requestState.isUseOldServer()
@@ -137,6 +136,7 @@ public IUploadServer getNextServer(UploadRequestState requestState, ResponseInfo
137136
}
138137

139138
// 1. 优先选择http3
139+
UploadServer http3Server = null;
140140
if (couldUseHttp3() && requestState.couldUseHttp3()) {
141141
for (String host : hostList) {
142142
UploadServerDomain domain = domainInfo.get(host);
@@ -166,20 +166,20 @@ public boolean condition(String host, UploadServer serverP, UploadServer filterS
166166
}
167167
});
168168

169-
server = (UploadServer) UploadServerNetworkStatus.getBetterNetworkServer(domainServer, server);
169+
http3Server = (UploadServer) UploadServerNetworkStatus.getBetterNetworkServer(domainServer, http3Server);
170170

171-
if (server != null) {
171+
if (http3Server != null) {
172172
break;
173173
}
174174
}
175175

176-
if (server != null) {
177-
server.setHttpVersion(IUploadServer.HttpVersion3);
178-
return server;
176+
if (http3Server != null) {
177+
http3Server.setHttpVersion(IUploadServer.HttpVersion3);
179178
}
180179
}
181180

182181
// 2. 挑选http2
182+
UploadServer http2Server = null;
183183
for (String host : hostList) {
184184
UploadServerDomain domain = domainInfo.get(host);
185185
if (domain == null) {
@@ -208,13 +208,14 @@ public boolean condition(String host, UploadServer serverP, UploadServer filterS
208208
}
209209
});
210210

211-
server = (UploadServer) UploadServerNetworkStatus.getBetterNetworkServer(domainServer, server);
211+
http2Server = (UploadServer) UploadServerNetworkStatus.getBetterNetworkServer(domainServer, http2Server);
212212

213-
if (server != null) {
213+
if (http2Server != null) {
214214
break;
215215
}
216216
}
217217

218+
UploadServer server = (UploadServer) UploadServerNetworkStatus.getBetterNetworkServer(http3Server, http2Server);
218219
if (server == null && !hasFreezeHost && hostList.size() > 0) {
219220
int index = (int) (Math.random() * hostList.size());
220221
String host = hostList.get(index);
@@ -300,6 +301,7 @@ private void unfreezeServer(IUploadServer freezeServer) {
300301

301302
String frozenType = UploadServerFreezeUtil.getFrozenType(freezeServer.getHost(), freezeServer.getIp());
302303
partialHttp2Freezer.unfreezeType(frozenType);
304+
partialHttp3Freezer.unfreezeType(frozenType);
303305
}
304306

305307
private boolean couldUseHttp3() {

library/src/main/java/com/qiniu/android/utils/Utils.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ public static String formEscape(String string) {
153153
return ret;
154154
}
155155

156+
@Deprecated
156157
public static String getIpType(String ip, String host) {
157158
String type = host;
158159
if (ip == null || ip.length() == 0) {
@@ -166,6 +167,23 @@ public static String getIpType(String ip, String host) {
166167
return type;
167168
}
168169

170+
public static String getIpType(String httpVersion, String ip, String host) {
171+
if (httpVersion == null) {
172+
httpVersion = "";
173+
}
174+
175+
String type = host;
176+
if (ip == null || ip.length() == 0) {
177+
return httpVersion + "-" + type;
178+
}
179+
if (ip.contains(":")) {
180+
type = getIPV6StringType(ip, host);
181+
} else if (ip.contains(".")) {
182+
type = getIPV4StringType(ip, host);
183+
}
184+
return httpVersion + "-" + type;
185+
}
186+
169187
public static boolean isIpv6(String ip) {
170188
if (StringUtils.isNullOrEmpty(ip)) {
171189
return false;

0 commit comments

Comments
 (0)