Skip to content

Commit 67f35d9

Browse files
authored
Merge pull request #326 from YangSen-qn/dns_dev_ys
dns prefetch
2 parents bf8c6b5 + bdd06f9 commit 67f35d9

File tree

84 files changed

+4789
-704
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+4789
-704
lines changed

Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source 'https://github.com/CocoaPods/Specs.git'
22

33
def shared_dependencies
4-
# pod 'HappyDNS', '>= 0.3'
4+
pod 'HappyDNS', '0.3.16'
55
end
66

77
def test_dependencies

QiniuDemo/Podfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source 'https://github.com/CocoaPods/Specs.git'
33
target "QiniuDemo" do
44
platform :ios, "7.0"
55
pod 'Qiniu',:path => '../'
6+
pod 'HappyDNS', :path => '../../HappyDns_iOS'
67
end
78

89
target "QiniuDemoTests" do

QiniuDemo/QiniuDemo/ViewController.m

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "ViewController.h"
10+
#import "QNTransactionManager.h"
1011

1112
@interface ViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
1213

@@ -112,6 +113,31 @@ - (NSString *)getImagePath:(UIImage *)Image {
112113
return filePath;
113114
}
114115

116+
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
117+
118+
// QNTransaction *t0 = [QNTransaction transaction:@"0" after:0 action:^{
119+
// NSLog(@"== 0 == %@", [NSThread currentThread]);
120+
// }];
121+
// QNTransaction *t1 = [QNTransaction timeTransaction:@"1" after:0 interval:2 action:^{
122+
// NSLog(@"== 1 == %@", [NSThread currentThread]);
123+
// NSDate *date_from = [NSDate date];
124+
// NSDate *date_current = [NSDate date];
125+
//
126+
// while ([date_current timeIntervalSinceDate:date_from] < 3) {
127+
// date_current = [NSDate date];
128+
// }
129+
// }];
130+
// QNTransactionManager *manager = [QNTransactionManager manager];
131+
//
132+
// [manager destroyResource];
133+
// [manager addTransaction:t0];
134+
// [manager addTransaction:t1];
135+
//
136+
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(20 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
137+
// [manager destroyResource];
138+
// });
139+
}
140+
115141
- (void)didReceiveMemoryWarning {
116142
[super didReceiveMemoryWarning];
117143
// Dispose of any resources that can be recreated.

QiniuSDK.xcodeproj/project.pbxproj

Lines changed: 351 additions & 121 deletions
Large diffs are not rendered by default.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

QiniuSDK.xcodeproj/xcshareddata/xcschemes/QiniuSDK_Mac.xcscheme

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
ReferencedContainer = "container:QiniuSDK.xcodeproj">
2121
</BuildableReference>
2222
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "NO"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "NO">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "DF2CDE0919DAC05500CE01FB"
32+
BuildableName = "QiniuSDK_MacTests.xctest"
33+
BlueprintName = "QiniuSDK_MacTests"
34+
ReferencedContainer = "container:QiniuSDK.xcodeproj">
35+
</BuildableReference>
36+
</BuildActionEntry>
2337
</BuildActionEntries>
2438
</BuildAction>
2539
<TestAction

QiniuSDK.xcodeproj/xcshareddata/xcschemes/QiniuSDK_iOS.xcscheme

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@
2020
ReferencedContainer = "container:QiniuSDK.xcodeproj">
2121
</BuildableReference>
2222
</BuildActionEntry>
23+
<BuildActionEntry
24+
buildForTesting = "YES"
25+
buildForRunning = "NO"
26+
buildForProfiling = "NO"
27+
buildForArchiving = "NO"
28+
buildForAnalyzing = "NO">
29+
<BuildableReference
30+
BuildableIdentifier = "primary"
31+
BlueprintIdentifier = "DF2CDE2319DAC08400CE01FB"
32+
BuildableName = "QiniuSDK_iOSTests.xctest"
33+
BlueprintName = "QiniuSDK_iOSTests"
34+
ReferencedContainer = "container:QiniuSDK.xcodeproj">
35+
</BuildableReference>
36+
</BuildActionEntry>
2337
</BuildActionEntries>
2438
</BuildAction>
2539
<TestAction

QiniuSDK/Common/QNAutoZone.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// QNAutoZone.h
3+
// QiniuSDK
4+
//
5+
// Created by yangsen on 2020/4/16.
6+
// Copyright © 2020 Qiniu. All rights reserved.
7+
//
8+
9+
#import "QNZone.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface QNAutoZone : QNZone
14+
15+
@end
16+
17+
NS_ASSUME_NONNULL_END

QiniuSDK/Common/QNAutoZone.m

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
//
2+
// QNAutoZone.m
3+
// QiniuSDK
4+
//
5+
// Created by yangsen on 2020/4/16.
6+
// Copyright © 2020 Qiniu. All rights reserved.
7+
//
8+
9+
#import "QNAutoZone.h"
10+
#import "QNSessionManager.h"
11+
#import "QNZoneInfo.h"
12+
#import "QNUpToken.h"
13+
#import "QNResponseInfo.h"
14+
15+
@interface QNAutoZoneCache : NSObject
16+
@property(nonatomic, strong)NSMutableDictionary *cache;
17+
@end
18+
@implementation QNAutoZoneCache
19+
20+
+ (instancetype)share{
21+
static QNAutoZoneCache *cache = nil;
22+
static dispatch_once_t onceToken;
23+
dispatch_once(&onceToken, ^{
24+
cache = [[QNAutoZoneCache alloc] init];
25+
[cache setupData];
26+
});
27+
return cache;
28+
}
29+
30+
- (void)setupData{
31+
self.cache = [NSMutableDictionary dictionary];
32+
}
33+
34+
- (void)cache:(NSDictionary *)zonesInfo
35+
forToken:(QNUpToken *)token{
36+
37+
NSString *cacheKey = token.index;
38+
if (!cacheKey || [cacheKey isEqualToString:@""]) {
39+
return;
40+
}
41+
42+
@synchronized (self) {
43+
if (zonesInfo) {
44+
self.cache[cacheKey] = zonesInfo;
45+
} else {
46+
[self.cache removeObjectForKey:cacheKey];
47+
}
48+
}
49+
}
50+
51+
- (QNZonesInfo *)zonesInfoForToken:(QNUpToken *)token{
52+
53+
NSString *cacheKey = token.index;
54+
if (!cacheKey || [cacheKey isEqualToString:@""]) {
55+
return nil;
56+
}
57+
58+
NSDictionary *zonesInfoDic = nil;
59+
@synchronized (self) {
60+
zonesInfoDic = self.cache[cacheKey];
61+
}
62+
63+
if (zonesInfoDic == nil) {
64+
return nil;
65+
}
66+
67+
QNZonesInfo *zonesInfo = [QNZonesInfo buildZonesInfoWithResp:zonesInfoDic];
68+
NSMutableArray *zonesInfoArray = [NSMutableArray array];
69+
for (QNZoneInfo *zoneInfo in zonesInfo.zonesInfo) {
70+
if ([zoneInfo isValid]) {
71+
[zonesInfoArray addObject:zoneInfo];
72+
}
73+
}
74+
zonesInfo.zonesInfo = [zonesInfoArray copy];
75+
return zonesInfo;
76+
}
77+
78+
@end
79+
80+
@implementation QNAutoZone {
81+
NSString *server;
82+
NSMutableDictionary *cache;
83+
NSLock *lock;
84+
QNSessionManager *sesionManager;
85+
}
86+
87+
- (instancetype)init{
88+
if (self = [super init]) {
89+
server = @"https://uc.qbox.me";
90+
cache = [NSMutableDictionary new];
91+
lock = [NSLock new];
92+
sesionManager = [[QNSessionManager alloc] initWithProxy:nil timeout:10 urlConverter:nil];
93+
}
94+
return self;
95+
}
96+
97+
- (NSString *)up:(QNUpToken *)token
98+
zoneInfoType:(QNZoneInfoType)zoneInfoType
99+
isHttps:(BOOL)isHttps
100+
frozenDomain:(NSString *)frozenDomain {
101+
102+
NSString *index = [token index];
103+
[lock lock];
104+
QNZonesInfo *zonesInfo = [cache objectForKey:index];
105+
[lock unlock];
106+
if (zonesInfo == nil) {
107+
return nil;
108+
}
109+
return [self upHost:[zonesInfo getZoneInfoWithType:zoneInfoType] isHttps:isHttps lastUpHost:frozenDomain];
110+
}
111+
112+
- (QNZonesInfo *)getZonesInfoWithToken:(QNUpToken *)token {
113+
if (token == nil) return nil;
114+
[lock lock];
115+
QNZonesInfo *zonesInfo = [cache objectForKey:[token index]];
116+
[lock unlock];
117+
return zonesInfo;
118+
}
119+
120+
- (void)preQuery:(QNUpToken *)token
121+
on:(QNPrequeryReturn)ret {
122+
123+
if (token == nil) {
124+
ret(-1, nil);
125+
return;
126+
}
127+
128+
[lock lock];
129+
QNZonesInfo *zonesInfo = [cache objectForKey:[token index]];
130+
[lock unlock];
131+
132+
if (zonesInfo == nil) {
133+
zonesInfo = [[QNAutoZoneCache share] zonesInfoForToken:token];
134+
[self->lock lock];
135+
[self->cache setValue:zonesInfo forKey:[token index]];
136+
[self->lock unlock];
137+
}
138+
139+
if (zonesInfo != nil) {
140+
ret(0, nil);
141+
return;
142+
}
143+
144+
//https://uc.qbox.me/v3/query?ak=T3sAzrwItclPGkbuV4pwmszxK7Ki46qRXXGBBQz3&bucket=if-pbl
145+
NSString *url = [NSString stringWithFormat:@"%@/v3/query?ak=%@&bucket=%@", server, token.access, token.bucket];
146+
[sesionManager get:url withHeaders:nil withCompleteBlock:^(QNHttpResponseInfo *httpResponseInfo, NSDictionary *respBody) {
147+
if (!httpResponseInfo.error) {
148+
149+
QNZonesInfo *zonesInfo = [QNZonesInfo buildZonesInfoWithResp:respBody];
150+
if (httpResponseInfo == nil) {
151+
ret(kQNInvalidToken, httpResponseInfo);
152+
} else {
153+
[self->lock lock];
154+
[self->cache setValue:zonesInfo forKey:[token index]];
155+
[self->lock unlock];
156+
[[QNAutoZoneCache share] cache:respBody forToken:token];
157+
ret(0, httpResponseInfo);
158+
}
159+
} else {
160+
ret(kQNNetworkError, httpResponseInfo);
161+
}
162+
}];
163+
}
164+
165+
@end

QiniuSDK/Common/QNConfig.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//
2+
// QNConfig.h
3+
// QiniuSDK
4+
//
5+
// Created by yangsen on 2020/3/26.
6+
// Copyright © 2020 Qiniu. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
11+
//MArk: -- 内部布置 尽量不要修改
12+
#define kQNPreQueryHost @"uc.qbox.me"

0 commit comments

Comments
 (0)