Skip to content

Commit 32853c3

Browse files
authored
Merge pull request #413 from YangSen-qn/develop
optimize network status manager
2 parents d073363 + 4cd52b8 commit 32853c3

File tree

11 files changed

+183
-40
lines changed

11 files changed

+183
-40
lines changed

QiniuDemo/QiniuDemo.xcodeproj/project.pbxproj

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
/* Begin PBXBuildFile section */
1010
26F62ADDD5B6306E978C9A3F /* libPods-QiniuDemoTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14CB2139715DD7B6FD5B146E /* libPods-QiniuDemoTests.a */; };
1111
2B848FF719DFA7ECB394CA9F /* libPods-QiniuDemo.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 6B9BC5A8D93F988C1240E05F /* libPods-QiniuDemo.a */; };
12-
456EB87E280003D500D8BB3E /* UploadResource_118M.zip in Resources */ = {isa = PBXBuildFile; fileRef = 456EB87D280003D500D8BB3E /* UploadResource_118M.zip */; };
13-
456EB8802800097C00D8BB3E /* UploadResource_18M.zip in Resources */ = {isa = PBXBuildFile; fileRef = 456EB87F2800097C00D8BB3E /* UploadResource_18M.zip */; };
1412
93D230241C86D7F700434F6D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230231C86D7F700434F6D /* main.m */; };
1513
93D230271C86D7F700434F6D /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230261C86D7F700434F6D /* AppDelegate.m */; };
1614
93D2302A1C86D7F700434F6D /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230291C86D7F700434F6D /* ViewController.m */; };
@@ -42,8 +40,6 @@
4240
14CB2139715DD7B6FD5B146E /* libPods-QiniuDemoTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-QiniuDemoTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
4341
24EE530BBB100BED89B08A3D /* Pods-QiniuDemo.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-QiniuDemo.release.xcconfig"; path = "Pods/Target Support Files/Pods-QiniuDemo/Pods-QiniuDemo.release.xcconfig"; sourceTree = "<group>"; };
4442
3189882026469145003CCA68 /* QiniuDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = QiniuDemo.entitlements; sourceTree = "<group>"; };
45-
456EB87D280003D500D8BB3E /* UploadResource_118M.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = UploadResource_118M.zip; sourceTree = "<group>"; };
46-
456EB87F2800097C00D8BB3E /* UploadResource_18M.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = UploadResource_18M.zip; sourceTree = "<group>"; };
4743
6B9BC5A8D93F988C1240E05F /* libPods-QiniuDemo.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-QiniuDemo.a"; sourceTree = BUILT_PRODUCTS_DIR; };
4844
93D2301F1C86D7F700434F6D /* QiniuDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QiniuDemo.app; sourceTree = BUILT_PRODUCTS_DIR; };
4945
93D230231C86D7F700434F6D /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
@@ -96,8 +92,6 @@
9692
318191A92531516F00D18536 /* DemoTestResource */ = {
9793
isa = PBXGroup;
9894
children = (
99-
456EB87F2800097C00D8BB3E /* UploadResource_18M.zip */,
100-
456EB87D280003D500D8BB3E /* UploadResource_118M.zip */,
10195
);
10296
path = DemoTestResource;
10397
sourceTree = "<group>";
@@ -297,8 +291,6 @@
297291
93D230321C86D7F700434F6D /* LaunchScreen.storyboard in Resources */,
298292
93D2302F1C86D7F700434F6D /* Assets.xcassets in Resources */,
299293
93D2302D1C86D7F700434F6D /* Main.storyboard in Resources */,
300-
456EB87E280003D500D8BB3E /* UploadResource_118M.zip in Resources */,
301-
456EB8802800097C00D8BB3E /* UploadResource_18M.zip in Resources */,
302294
);
303295
runOnlyForDeploymentPostprocessing = 0;
304296
};

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,8 @@
260260
31C2EEE5242DE86D00713A33 /* QNUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C2EEE2242DE86300713A33 /* QNUtils.m */; };
261261
31C2EEEC242DEF6A00713A33 /* QNUtilTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C2EEEB242DEF6A00713A33 /* QNUtilTest.m */; };
262262
31C2EEED242DEF6A00713A33 /* QNUtilTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C2EEEB242DEF6A00713A33 /* QNUtilTest.m */; };
263+
31C30FAC2852EA7900BFD8F7 /* QNNetworkStatusTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C30FAB2852EA7900BFD8F7 /* QNNetworkStatusTest.m */; };
264+
31C30FAD2852EA7900BFD8F7 /* QNNetworkStatusTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 31C30FAB2852EA7900BFD8F7 /* QNNetworkStatusTest.m */; };
263265
31C6E68824ECC75700633629 /* NSData+QNGZip.m in Sources */ = {isa = PBXBuildFile; fileRef = 31259E3124E3D02C00CF7531 /* NSData+QNGZip.m */; };
264266
31C9D24427141252006B9433 /* QNCFHttpClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 31C9D24227141252006B9433 /* QNCFHttpClient.h */; };
265267
31C9D24527141252006B9433 /* QNCFHttpClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 31C9D24227141252006B9433 /* QNCFHttpClient.h */; };
@@ -534,6 +536,7 @@
534536
31C2EEE1242DE86300713A33 /* QNUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QNUtils.h; sourceTree = "<group>"; };
535537
31C2EEE2242DE86300713A33 /* QNUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNUtils.m; sourceTree = "<group>"; };
536538
31C2EEEB242DEF6A00713A33 /* QNUtilTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNUtilTest.m; sourceTree = "<group>"; };
539+
31C30FAB2852EA7900BFD8F7 /* QNNetworkStatusTest.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNNetworkStatusTest.m; sourceTree = "<group>"; };
537540
31C9D24227141252006B9433 /* QNCFHttpClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = QNCFHttpClient.h; sourceTree = "<group>"; };
538541
31C9D24327141252006B9433 /* QNCFHttpClient.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = QNCFHttpClient.m; sourceTree = "<group>"; };
539542
31C9D2502717E106006B9433 /* QNCFHttpThreadPool.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = QNCFHttpThreadPool.m; sourceTree = "<group>"; };
@@ -1092,6 +1095,7 @@
10921095
31C2EEEB242DEF6A00713A33 /* QNUtilTest.m */,
10931096
3149445D24470B9E00386F16 /* XCTestCase+QNTest.h */,
10941097
3149445E24470B9E00386F16 /* XCTestCase+QNTest.m */,
1098+
31C30FAB2852EA7900BFD8F7 /* QNNetworkStatusTest.m */,
10951099
);
10961100
path = QiniuSDKTests;
10971101
sourceTree = SOURCE_ROOT;
@@ -1628,6 +1632,7 @@
16281632
31304CBF25676921001034D2 /* QNUploadDomainRegionTest.m in Sources */,
16291633
316A4B832431C8BA007BF564 /* QNDnsPrefetcherTest.m in Sources */,
16301634
317211E526FAD36400EF0947 /* QNServerConfigTest.m in Sources */,
1635+
31C30FAC2852EA7900BFD8F7 /* QNNetworkStatusTest.m in Sources */,
16311636
31C2EEEC242DEF6A00713A33 /* QNUtilTest.m in Sources */,
16321637
3142419E2449553F00BD9A21 /* QNCFHttpClientTest.m in Sources */,
16331638
DF0A03241B3BAC3900E3778C /* QNFormUploadTest.m in Sources */,
@@ -1751,6 +1756,7 @@
17511756
31304CC025676921001034D2 /* QNUploadDomainRegionTest.m in Sources */,
17521757
316A4B842431C8D2007BF564 /* QNDnsPrefetcherTest.m in Sources */,
17531758
317211E626FAD36400EF0947 /* QNServerConfigTest.m in Sources */,
1759+
31C30FAD2852EA7900BFD8F7 /* QNNetworkStatusTest.m in Sources */,
17541760
31C2EEED242DEF6A00713A33 /* QNUtilTest.m in Sources */,
17551761
3142419F2449554200BD9A21 /* QNCFHttpClientTest.m in Sources */,
17561762
DF0A03261B3BAC6E00E3778C /* QNBase64Test.m in Sources */,

QiniuSDK/Http/ConnectCheck/QNConnectChecker.m

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,31 @@ + (void)checkHost:(NSString *)host complete:(void (^)(QNUploadSingleRequestMetri
113113

114114
QNUploadSingleRequestMetrics *timeoutMetric = [QNUploadSingleRequestMetrics emptyMetrics];
115115
[timeoutMetric start];
116-
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * kQNGlobalConfiguration.connectCheckTimeout), [self checkQueue], ^{
116+
117+
QNUploadSystemClient *client = [[QNUploadSystemClient alloc] init];
118+
[client request:request server:nil connectionProxy:nil progress:nil complete:^(NSURLResponse *response, QNUploadSingleRequestMetrics * metrics, NSData * _Nullable data, NSError * error) {
117119
@synchronized (self) {
118120
if (hasCallback) {
119121
return;
120122
}
121123
hasCallback = true;
122124
}
123-
[timeoutMetric end];
124-
timeoutMetric.error = [NSError errorWithDomain:@"com.qiniu.NetworkCheck" code:NSURLErrorTimedOut userInfo:nil];
125-
complete(timeoutMetric);
126-
});
125+
QNLogInfo(@"== checkHost:%@ responseInfo:%@", host, response);
126+
complete(metrics);
127+
}];
127128

128-
QNUploadSystemClient *client = [[QNUploadSystemClient alloc] init];
129-
[client request:request server:nil connectionProxy:nil progress:nil complete:^(NSURLResponse *response, QNUploadSingleRequestMetrics * metrics, NSData * _Nullable data, NSError * error) {
129+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * kQNGlobalConfiguration.connectCheckTimeout), [self checkQueue], ^{
130130
@synchronized (self) {
131131
if (hasCallback) {
132132
return;
133133
}
134134
hasCallback = true;
135135
}
136-
QNLogInfo(@"== checkHost:%@ responseInfo:%@", host, response);
137-
complete(metrics);
138-
}];
136+
[client cancel];
137+
[timeoutMetric end];
138+
timeoutMetric.error = [NSError errorWithDomain:@"com.qiniu.NetworkCheck" code:NSURLErrorTimedOut userInfo:nil];
139+
complete(timeoutMetric);
140+
});
139141
}
140142

141143
@end

QiniuSDK/Http/Dns/QNDnsPrefetch.m

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,10 @@ - (BOOL)preFetchHost:(NSString *)preHost dns:(QNInternalDns *)dns error:(NSError
530530
return NO;
531531
}
532532

533-
NSDictionary *addressDictionary = self.addressDictionary;
533+
NSDictionary *addressDictionary = nil;
534+
@synchronized (self) {
535+
addressDictionary = [self.addressDictionary copy];
536+
}
534537
NSArray<QNDnsNetworkAddress *>* preAddressList = addressDictionary[preHost];
535538
if (preAddressList && ![preAddressList.firstObject needRefresh]) {
536539
return YES;
@@ -610,7 +613,10 @@ - (BOOL)recorderDnsCache{
610613
return NO;
611614
}
612615

613-
NSDictionary *addressDictionary = self.addressDictionary;
616+
NSDictionary *addressDictionary = nil;
617+
@synchronized (self) {
618+
addressDictionary = [self.addressDictionary copy];
619+
}
614620
NSMutableDictionary *addressInfo = [NSMutableDictionary dictionary];
615621
for (NSString *key in addressDictionary.allKeys) {
616622

@@ -688,11 +694,6 @@ - (void)clearDnsDiskCache:(NSError **)error {
688694
return [autoHosts copy];
689695
}
690696

691-
- (NSArray <NSString *> *)getCacheHosts{
692-
NSDictionary *addressDictionary = self.addressDictionary;
693-
return [addressDictionary copy];
694-
}
695-
696697

697698
//MARK: --
698699
- (BOOL)isDnsOpen{

QiniuSDK/Http/NetworkStatus/QNNetworkStatusManager.m

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ - (void)updateNetworkStatus:(NSString *)type speed:(int)speed{
9999

100100

101101
// ----- status 持久化
102-
#define kNetworkStatusDiskKey @"NetworkStatus:v1.0.0"
102+
#define kNetworkStatusDiskKey @"NetworkStatus:v1.0.1"
103103
- (void)asyncRecordNetworkStatusInfo{
104104
@synchronized (self) {
105105
if (self.isHandlingNetworkInfoOfDisk) {
@@ -112,6 +112,7 @@ - (void)asyncRecordNetworkStatusInfo{
112112
self.isHandlingNetworkInfoOfDisk = NO;
113113
});
114114
}
115+
115116
- (void)asyncRecoverNetworkStatusFromDisk{
116117
@synchronized (self) {
117118
if (self.isHandlingNetworkInfoOfDisk) {
@@ -124,13 +125,19 @@ - (void)asyncRecoverNetworkStatusFromDisk{
124125
self.isHandlingNetworkInfoOfDisk = NO;
125126
});
126127
}
128+
127129
- (void)recordNetworkStatusInfo{
128130
if (self.recorder == nil || self.networkStatusInfo == nil) {
129131
return;
130132
}
133+
134+
NSDictionary *networkStatusInfo = nil;
135+
@synchronized(self) {
136+
networkStatusInfo = [self.networkStatusInfo copy];
137+
}
131138
NSMutableDictionary *statusInfo = [NSMutableDictionary dictionary];
132-
for (NSString *key in self.networkStatusInfo.allKeys) {
133-
NSDictionary *statusDictionary = [self.networkStatusInfo[key] toDictionary];
139+
for (NSString *key in networkStatusInfo.allKeys) {
140+
NSDictionary *statusDictionary = [networkStatusInfo[key] toDictionary];
134141
if (statusDictionary) {
135142
[statusInfo setObject:statusDictionary forKey:key];
136143
}
@@ -167,7 +174,9 @@ - (void)recoverNetworkStatusFromDisk{
167174
}
168175
}
169176

170-
[self.networkStatusInfo setValuesForKeysWithDictionary:networkStatusInfo];
177+
@synchronized(self) {
178+
[self.networkStatusInfo setValuesForKeysWithDictionary:networkStatusInfo];
179+
}
171180
}
172181

173182
@end

QiniuSDK/Storage/QNConfiguration.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ typedef void (^QNConfigurationBuilderBlock)(QNConfigurationBuilder *builder);
263263

264264
/**
265265
* 断点上传时的分片大小
266+
* 分片 v1 最小为 1024,即 1K,建议用户配置 >= 512K
267+
* 分片 v2 最小为 1024 * 1024,即 1M
266268
*/
267269
@property (assign) UInt32 chunkSize;
268270

QiniuSDK/Utils/QNVersion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
/**
1212
* sdk 版本
1313
*/
14-
static NSString *const kQiniuVersion = @"8.4.3";
14+
static NSString *const kQiniuVersion = @"8.4.5";

QiniuSDKTests/QNConnctCheckTest.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ - (void)testCustomCheckHosts {
4848
}
4949
}
5050

51-
XCTAssertTrue(maxCount == successCount, @"maxCount:%d successCount:%d", maxCount, successCount);
51+
XCTAssertTrue(successCount > 0 , @"maxCount:%d successCount:%d", maxCount, successCount);
5252
}
5353

5454
- (void)testNotConnected {
5555
kQNGlobalConfiguration.connectCheckURLStrings = @[@"https://www.test1.com", @"https://www.test2.com"];
5656

57-
int maxCount = 100;
57+
int maxCount = 10;
5858
int successCount = 0;
5959
for (int i = 0; i < maxCount; i++) {
6060
if ([QNConnectChecker isConnected:[QNConnectChecker check]]) {

QiniuSDKTests/QNDnsPrefetcherTest.m

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,18 @@ @implementation InetAddress
2727
@end
2828

2929
#define CustomIPValue @"192.168.1.1"
30+
#define kCustomHost @"uplog.qbox.me"
31+
#define kDnsTestToken token_na0
32+
3033
@interface CustomDns : NSObject <QNDnsDelegate>
3134
@property(nonatomic, assign)BOOL isTestTtl;
3235
@end
3336
@implementation CustomDns
3437

3538
- (NSArray<id<QNIDnsNetworkAddress>> *)lookup:(NSString *)host{
39+
if (![host isEqualToString:kCustomHost]) {
40+
return nil;
41+
}
3642

3743
InetAddress *inetAddress = [[InetAddress alloc] init];
3844
inetAddress.hostValue = host;
@@ -56,8 +62,6 @@ @interface QNDnsPrefetcherTest : XCTestCase
5662

5763
@implementation QNDnsPrefetcherTest
5864

59-
#define kCustomHost @"uplog.qbox.me"
60-
#define kDnsTestToken token_na0
6165
- (void)setUp {
6266

6367
[kQNTransactionManager destroyResource];
@@ -73,7 +77,10 @@ - (void)tearDown {
7377
}
7478

7579
- (void)testLocalLoad {
80+
[self testPreFetch];
7681

82+
kQNGlobalConfiguration.dns = [[CustomDns alloc] init];
83+
kQNGlobalConfiguration.isDnsOpen = YES;
7784
[kQNTransactionManager addDnsLocalLoadTransaction];
7885

7986
AGWW_WAIT_WHILE([kQNDnsPrefetch getInetAddressByHost:kCustomHost] == nil, 60 * 5);
@@ -156,4 +163,41 @@ - (void)testMutiThreadPrefetch{
156163
QN_TEST_CASE_WAIT_TIME(2);
157164
}
158165

166+
- (void)testClearCache{
167+
[kQNTransactionManager destroyResource];
168+
QN_TEST_CASE_WAIT_TIME(2);
169+
170+
CustomDns *dns = [[CustomDns alloc] init];
171+
dns.isTestTtl = YES;
172+
kQNGlobalConfiguration.dns = dns;
173+
NSString *host = @"uplog.qbox.me";
174+
dispatch_group_t group = dispatch_group_create();
175+
176+
int times = 10;
177+
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
178+
dispatch_group_enter(group);
179+
[kQNDnsPrefetch prefetchHostBySafeDns:host error:nil];
180+
for (int i=0; i<times; i++) {
181+
for (int i=0; i<times; i++) {
182+
[kQNDnsPrefetch prefetchHostBySafeDns:[NSString stringWithFormat:@"%d%@", i, host] error:nil];
183+
}
184+
[NSThread sleepForTimeInterval:0.0001];
185+
}
186+
dispatch_group_leave(group);
187+
});
188+
189+
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
190+
dispatch_group_enter(group);
191+
for (int i=0; i<times; i++) {
192+
[kQNDnsPrefetch clearDnsCache:nil];
193+
[NSThread sleepForTimeInterval:0.001];
194+
}
195+
dispatch_group_leave(group);
196+
});
197+
198+
dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC));
199+
200+
QN_TEST_CASE_WAIT_TIME(2);
201+
}
202+
159203
@end

0 commit comments

Comments
 (0)