Skip to content

Commit 591f7e6

Browse files
author
JemyCheung
committed
update dnspre_cache
1 parent 6ab2280 commit 591f7e6

File tree

8 files changed

+121
-87
lines changed

8 files changed

+121
-87
lines changed

.idea/caches/gradle_models.ser

-51.8 KB
Binary file not shown.

.idea/codeStyles/Project.xml

Lines changed: 3 additions & 0 deletions
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 & 45 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

android-sdk.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<content url="file://$MODULE_DIR$">
1414
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
1515
</content>
16-
<orderEntry type="inheritedJdk" />
16+
<orderEntry type="jdk" jdkName="JDK" jdkType="JavaSDK" />
1717
<orderEntry type="sourceFolder" forTests="false" />
1818
</component>
1919
</module>

build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ buildscript {
1010
}
1111
dependencies {
1212
classpath 'com.android.tools.build:gradle:3.5.2'
13-
1413
// NOTE: Do not place your application dependencies here; they belong
1514
// in the individual module build.gradle files
1615
}

library/src/main/java/com/qiniu/android/common/Constants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
public final class Constants {
5-
public static final String VERSION = "7.5.0";
5+
public static final String VERSION = "7.5.1";
66

77
public static final String UTF_8 = "utf-8";
88
}

library/src/main/java/com/qiniu/android/http/DnsPrefetcher.java

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public class DnsPrefetcher {
3636

3737
private static ConcurrentHashMap<String, List<InetAddress>> mConcurrentHashMap = new ConcurrentHashMap<String, List<InetAddress>>();
3838
private static List<String> mHosts = new ArrayList<String>();
39+
private static DnsCacheKey mDnsCacheKey = null;
3940

4041
private DnsPrefetcher() {
4142

@@ -52,10 +53,32 @@ public static DnsPrefetcher getDnsPrefetcher() {
5253
return dnsPrefetcher;
5354
}
5455

56+
/**
57+
* 不用判断是否预取,每次调用UploadManager都直接预取一次。
58+
* uploadManager只会new一次,但是uploadManager.put方法有多次,如果期间网络发生变化
59+
* 这个方法预取的结果应该被ConcurrentHashMap自动覆盖
60+
*/
61+
public void localFetch() {
62+
List<String> localHosts = new ArrayList<String>();
63+
//local
64+
List<ZoneInfo> listZoneinfo = getLocalZone();
65+
for (ZoneInfo zone : listZoneinfo) {
66+
for (String host : zone.upDomainsList) {
67+
localHosts.add(host);
68+
}
69+
}
70+
localHosts.add(Config.preQueryHost);
71+
72+
if (localHosts != null && localHosts.size() > 0)
73+
preFetch(localHosts);
74+
}
75+
76+
5577
public DnsPrefetcher init(String token) throws UnknownHostException {
5678
this.token = token;
57-
preHosts();
58-
preFetch();
79+
List<String> preHosts = preHosts();
80+
if (preHosts != null && preHosts.size() > 0)
81+
preFetch(preHosts);
5982
return this;
6083
}
6184

@@ -86,43 +109,47 @@ public List<InetAddress> getInetAddressByHost(String host) {
86109
return mConcurrentHashMap.get(host);
87110
}
88111

89-
private void preHosts() {
112+
private List<String> preHosts() {
90113
HashSet<String> set = new HashSet<String>();
91-
114+
List<String> preHosts = new ArrayList<>();
92115
//preQuery sync
93116
ZoneInfo zoneInfo = getPreQueryZone();
94117
if (zoneInfo != null) {
95118
for (String host : zoneInfo.upDomainsList) {
96119
if (set.add(host))
97-
mHosts.add(host);
120+
preHosts.add(host);
98121
}
99122
}
123+
100124
//local
101125
List<ZoneInfo> listZoneinfo = getLocalZone();
102126
for (ZoneInfo zone : listZoneinfo) {
103127
for (String host : zone.upDomainsList) {
104128
if (set.add(host))
105-
mHosts.add(host);
129+
preHosts.add(host);
106130
}
107131
}
108132
if (set.add(Config.preQueryHost))
109-
mHosts.add(Config.preQueryHost);
133+
preHosts.add(Config.preQueryHost);
134+
return preHosts;
110135
}
111136

112137

113-
private void preFetch() {
138+
private void preFetch(List<String> fetchHost) {
114139
List<String> rePreHosts = new ArrayList<String>();
115-
for (String host : mHosts) {
140+
for (String host : fetchHost) {
116141
List<InetAddress> inetAddresses = null;
117142
try {
118143
inetAddresses = okhttp3.Dns.SYSTEM.lookup(host);
119144
mConcurrentHashMap.put(host, inetAddresses);
145+
mHosts.add(host);
120146
} catch (UnknownHostException e) {
121147
e.printStackTrace();
122148
rePreHosts.add(host);
123149
}
124150
}
125-
rePreFetch(rePreHosts, null);
151+
if (rePreHosts.size() > 0)
152+
rePreFetch(rePreHosts, null);
126153
}
127154

128155
/**
@@ -151,6 +178,7 @@ private boolean rePreFetch(String host, Dns customeDns) {
151178
inetAddresses = customeDns.lookup(host);
152179
}
153180
mConcurrentHashMap.put(host, inetAddresses);
181+
mHosts.add(host);
154182
return true;
155183
} catch (UnknownHostException e) {
156184
e.printStackTrace();
@@ -264,6 +292,30 @@ public boolean equals(Object obj) {
264292
* @return true:重新预期并缓存, false:不需要重新预取和缓存
265293
*/
266294
public static boolean checkRePrefetchDns(String token, Configuration config) {
295+
if (mDnsCacheKey == null)
296+
return true;
297+
298+
String currentTime = String.valueOf(System.currentTimeMillis());
299+
String localip = AndroidNetwork.getHostIP();
300+
String akScope = StringUtils.getAkAndScope(token);
301+
302+
if (currentTime == null || localip == null || akScope == null)
303+
return true;
304+
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(mDnsCacheKey.getCurrentTime())) / 1000;
305+
if (!mDnsCacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs || !mDnsCacheKey.getAkScope().equals(akScope)) {
306+
return true;
307+
}
308+
309+
return false;
310+
}
311+
312+
/**
313+
* uploadManager初始化时,加载本地缓存到内存
314+
*
315+
* @param config
316+
* @return 如果不存在缓存返回true,需要重新预取;如果存在且满足使用,返回false
317+
*/
318+
public static boolean recoverCache(Configuration config) {
267319
Recorder recorder = null;
268320
try {
269321
recorder = new DnsCacheFile(Config.dnscacheDir);
@@ -285,15 +337,14 @@ public static boolean checkRePrefetchDns(String token, Configuration config) {
285337

286338
String currentTime = String.valueOf(System.currentTimeMillis());
287339
String localip = AndroidNetwork.getHostIP();
288-
String akScope = StringUtils.getAkAndScope(token);
289340

290-
if (currentTime == null || localip == null || akScope == null)
341+
if (currentTime == null || localip == null)
291342
return true;
292343
long cacheTime = (Long.parseLong(currentTime) - Long.parseLong(cacheKey.getCurrentTime())) / 1000;
293-
if (!cacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs || !cacheKey.getAkScope().equals(akScope)) {
344+
if (!cacheKey.getLocalIp().equals(localip) || cacheTime > config.dnsCacheTimeMs) {
294345
return true;
295346
}
296-
347+
mDnsCacheKey = cacheKey;
297348
return recoverDnsCache(data);
298349
}
299350

@@ -308,12 +359,16 @@ public static void startPrefetchDns(String token, Configuration config) {
308359
String akScope = StringUtils.getAkAndScope(token);
309360
if (currentTime == null || localip == null || akScope == null)
310361
return;
311-
String cacheKey = new DnsCacheKey(currentTime, localip, akScope).toString();
362+
DnsCacheKey dnsCacheKey = new DnsCacheKey(currentTime, localip, akScope);
363+
String cacheKey = dnsCacheKey.toString();
364+
312365
Recorder recorder = null;
313366
DnsPrefetcher dnsPrefetcher = null;
314367
try {
315368
recorder = new DnsCacheFile(Config.dnscacheDir);
316369
dnsPrefetcher = DnsPrefetcher.getDnsPrefetcher().init(token);
370+
//确认预取结束后,需要更新缓存mDnsCacheKey
371+
mDnsCacheKey = dnsCacheKey;
317372
} catch (IOException e) {
318373
e.printStackTrace();
319374
return;
@@ -352,4 +407,8 @@ public static boolean recoverDnsCache(byte[] data) {
352407
DnsPrefetcher.getDnsPrefetcher().setHosts(list);
353408
return false;
354409
}
410+
411+
private void setmDnsCacheKey(DnsCacheKey mDnsCacheKey) {
412+
mDnsCacheKey = mDnsCacheKey;
413+
}
355414
}

library/src/main/java/com/qiniu/android/storage/UploadManager.java

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public final class UploadManager {
2828
/**
2929
* 保证代码只执行一次,防止多个uploadManager同时开始预取dns
3030
*/
31-
static AtomicBoolean atomicStruct = new AtomicBoolean(false);
31+
static AtomicBoolean atomicLocalPrefetch = new AtomicBoolean(false);
3232

3333
/**
3434
* default 3 Threads
@@ -44,13 +44,15 @@ public UploadManager(Configuration config) {
4444
this.config = config;
4545
this.client = new Client(config.proxy, config.connectTimeout, config.responseTimeout,
4646
config.urlConverter, config.dns);
47+
startLocalPrefetch(config);
4748
}
4849

4950
public UploadManager(Configuration config, int multitread) {
5051
this.config = config;
5152
this.multithreads = multitread >= 1 ? multitread : DEF_THREAD_NUM;
5253
this.client = new Client(config.proxy, config.connectTimeout, config.responseTimeout,
5354
config.urlConverter, config.dns);
55+
startLocalPrefetch(config);
5456
}
5557

5658
public UploadManager(Recorder recorder) {
@@ -69,6 +71,20 @@ public UploadManager(Recorder recorder, KeyGenerator keyGen, int multitread) {
6971
this(new Configuration.Builder().recorder(recorder, keyGen).build(), multitread);
7072
}
7173

74+
//初始化一个UploadManager只允许执行一次,开启一个线程,对sdk内置host进行预取
75+
private void startLocalPrefetch(final Configuration config) {
76+
if (atomicLocalPrefetch.compareAndSet(false, true)) {
77+
if (DnsPrefetcher.recoverCache(config)) {
78+
new Thread(new Runnable() {
79+
@Override
80+
public void run() {
81+
DnsPrefetcher.getDnsPrefetcher().localFetch();
82+
}
83+
}).start();
84+
}
85+
}
86+
}
87+
7288
private static boolean areInvalidArg(final String key, byte[] data, File f, String token,
7389
UpToken decodedToken, final UpCompletionHandler complete) {
7490
if (complete == null) {
@@ -98,7 +114,8 @@ private static boolean areInvalidArg(final String key, byte[] data, File f, Stri
98114
return false;
99115
}
100116

101-
private static ResponseInfo areInvalidArg(final String key, byte[] data, File f, String token,
117+
private static ResponseInfo areInvalidArg(final String key, byte[] data, File f, String
118+
token,
102119
UpToken decodedToken) {
103120
String message = null;
104121
if (f == null && data == null) {
@@ -141,17 +158,16 @@ public void put(final byte[] data, final String key, final String token,
141158
if (areInvalidArg(key, data, null, token, decodedToken, complete)) {
142159
return;
143160
}
144-
145-
if (atomicStruct.compareAndSet(false, true)) {
146-
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
147-
new Thread(new Runnable() {
148-
@Override
149-
public void run() {
150-
DnsPrefetcher.startPrefetchDns(token, config);
151-
}
152-
}).start();
153-
}
161+
//此处是对uc.qbox.me接口获取的域名进行预取
162+
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
163+
new Thread(new Runnable() {
164+
@Override
165+
public void run() {
166+
DnsPrefetcher.startPrefetchDns(token, config);
167+
}
168+
}).start();
154169
}
170+
155171
Zone z = config.zone;
156172
z.preQuery(token, new Zone.QueryHandler() {
157173
@Override
@@ -179,7 +195,8 @@ public void onFailure(int reason) {
179195
* @param completionHandler 上传完成的后续处理动作
180196
* @param options 上传数据的可选参数
181197
*/
182-
public void put(String filePath, String key, String token, UpCompletionHandler completionHandler,
198+
public void put(String filePath, String key, String token, UpCompletionHandler
199+
completionHandler,
183200
final UploadOptions options) {
184201
put(new File(filePath), key, token, completionHandler, options);
185202
}
@@ -194,23 +211,23 @@ public void put(String filePath, String key, String token, UpCompletionHandler c
194211
* @param complete 上传完成的后续处理动作
195212
* @param options 上传数据的可选参数
196213
*/
197-
public void put(final File file, final String key, final String token, final UpCompletionHandler complete,
214+
public void put(final File file, final String key, final String token,
215+
final UpCompletionHandler complete,
198216
final UploadOptions options) {
199217
final UpToken decodedToken = UpToken.parse(token);
200218
if (areInvalidArg(key, null, file, token, decodedToken, complete)) {
201219
return;
202220
}
203-
204-
if (atomicStruct.compareAndSet(false, true)) {
205-
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
206-
new Thread(new Runnable() {
207-
@Override
208-
public void run() {
209-
DnsPrefetcher.startPrefetchDns(token, config);
210-
}
211-
}).start();
212-
}
221+
//此处是每次上传时判断,对uc.qbox.me接口获取的host+sdk内置的host进行预取(去重)
222+
if (DnsPrefetcher.checkRePrefetchDns(token, config)) {
223+
new Thread(new Runnable() {
224+
@Override
225+
public void run() {
226+
DnsPrefetcher.startPrefetchDns(token, config);
227+
}
228+
}).start();
213229
}
230+
214231
Zone z = config.zone;
215232
z.preQuery(token, new Zone.QueryHandler() {
216233
@Override

0 commit comments

Comments
 (0)