Skip to content

Commit

Permalink
support acc upload
Browse files Browse the repository at this point in the history
  • Loading branch information
YangSen-qn committed May 28, 2024
1 parent da1929d commit f7e8511
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 30 deletions.
4 changes: 0 additions & 4 deletions QiniuDemo/QiniuDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* Begin PBXBuildFile section */
26F62ADDD5B6306E978C9A3F /* libPods-QiniuDemoTests.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 14CB2139715DD7B6FD5B146E /* libPods-QiniuDemoTests.a */; };
4545D3142BAD7D0C00932C3F /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 4545D3132BAD7D0C00932C3F /* PrivacyInfo.xcprivacy */; };
4561F02C28D9A6F80098A697 /* UploadResource_14M.zip in Resources */ = {isa = PBXBuildFile; fileRef = 4561F02B28D9A6F80098A697 /* UploadResource_14M.zip */; };
45E6080929ADD57100634200 /* UploadResource_1G.zip in Resources */ = {isa = PBXBuildFile; fileRef = 45E6080829ADD57100634200 /* UploadResource_1G.zip */; };
93D230241C86D7F700434F6D /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 93D230231C86D7F700434F6D /* main.m */; };
Expand Down Expand Up @@ -43,7 +42,6 @@
14CB2139715DD7B6FD5B146E /* libPods-QiniuDemoTests.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-QiniuDemoTests.a"; sourceTree = BUILT_PRODUCTS_DIR; };
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>"; };
3189882026469145003CCA68 /* QiniuDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = QiniuDemo.entitlements; sourceTree = "<group>"; };
4545D3132BAD7D0C00932C3F /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
4561F02B28D9A6F80098A697 /* UploadResource_14M.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = UploadResource_14M.zip; sourceTree = "<group>"; };
4561F02F28D9AB090098A697 /* Configure.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Configure.h; sourceTree = "<group>"; };
45E6080829ADD57100634200 /* UploadResource_1G.zip */ = {isa = PBXFileReference; lastKnownFileType = archive.zip; path = UploadResource_1G.zip; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,7 +169,6 @@
93D230301C86D7F700434F6D /* LaunchScreen.storyboard */,
93D230331C86D7F700434F6D /* Info.plist */,
93D230221C86D7F700434F6D /* Supporting Files */,
4545D3132BAD7D0C00932C3F /* PrivacyInfo.xcprivacy */,
);
path = QiniuDemo;
sourceTree = "<group>";
Expand Down Expand Up @@ -309,7 +306,6 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
4545D3142BAD7D0C00932C3F /* PrivacyInfo.xcprivacy in Resources */,
93D230321C86D7F700434F6D /* LaunchScreen.storyboard in Resources */,
45E6080929ADD57100634200 /* UploadResource_1G.zip in Resources */,
4561F02C28D9A6F80098A697 /* UploadResource_14M.zip in Resources */,
Expand Down
7 changes: 6 additions & 1 deletion QiniuDemo/QiniuDemo/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ - (void)uploadImageToQNFilePath:(NSString *)filePath complete:(dispatch_block_t)
builder.resumeUploadVersion = QNResumeUploadVersionV2;
builder.putThreshold = 4*1024*1024;
builder.chunkSize = 1*1024*1024;
// builder.zone = [[QNFixedZone alloc] initWithUpDomainList:@[kUploadFixHost00, kUploadFixHost01]];
builder.accelerateUploading = true;
builder.zone = [[QNFixedZone alloc] initWithAccUpDomainList:@[@"zone0-space.kodo-accelerate.cn-east-1.qiniucs.com"]
upList:@[kUploadFixHost00]
oldUpList:@[]
regionId:@"custom"];
// [[QNFixedZone alloc] initWithUpDomainList:@[kUploadFixHost00, kUploadFixHost01]];
NSString *recorderPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSLog(@"== record path:%@", recorderPath);
builder.recorder = [QNFileRecorder fileRecorderWithFolder:recorderPath error:nil];
Expand Down
15 changes: 15 additions & 0 deletions QiniuSDK/Common/QNFixedZone.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ NS_ASSUME_NONNULL_BEGIN
*/
- (instancetype)initWithUpDomainList:(NSArray<NSString *> *)upList;


/**
* Zone初始化方法
*
* @param accUpList 加速上传服务器地址列表
* @param upList 默认上传服务器地址列表
* @param oldUpList 支持 SNI 的上传服务器地址列表
* @param regionId 区域 ID
* @return Zone实例
*/
- (instancetype)initWithAccUpDomainList:(NSArray<NSString *> *)accUpList
upList:(NSArray<NSString *> *)upList
oldUpList:(NSArray<NSString *> *)oldUpList
regionId:(NSString *)regionId;

/**
* Zone初始化方法
*
Expand Down
23 changes: 21 additions & 2 deletions QiniuSDK/Common/QNFixedZone.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,21 @@ - (QNZonesInfo *)createZonesInfo:(NSArray <NSString *> *)upDomains
- (QNZonesInfo *)createZonesInfo:(NSArray <NSString *> *)upDomains
oldUpDomains:(NSArray <NSString *> *)oldUpDomains
regionId:(NSString *)regionId {
if (!upDomains && upDomains.count == 0) {
return [self createZonesInfo:nil domains:upDomains oldDomains:oldUpDomains regionId:regionId];
}

- (QNZonesInfo *)createZonesInfo:(NSArray <NSString *> *)accDomains
domains:(NSArray <NSString *> *)domains
oldDomains:(NSArray <NSString *> *)oldDomains
regionId:(NSString *)regionId {
if ((!accDomains || accDomains.count == 0) && (!domains || domains.count == 0)) {
return nil;
}

QNZoneInfo *zoneInfo = [QNZoneInfo zoneInfoWithMainHosts:upDomains oldHosts:oldUpDomains regionId:regionId];
QNZoneInfo *zoneInfo = [QNZoneInfo zoneInfoWithAccHosts:accDomains
mainHosts:domains
oldHosts:oldDomains
regionId:regionId];
QNZonesInfo *zonesInfo = [[QNZonesInfo alloc] initWithZonesInfo:@[zoneInfo]];
return zonesInfo;
}
Expand All @@ -167,6 +177,15 @@ - (instancetype)initWithUpDomainList:(NSArray<NSString *> *)upList
}
return self;
}
- (instancetype)initWithAccUpDomainList:(NSArray<NSString *> *)accUpList
upList:(NSArray<NSString *> *)upList
oldUpList:(NSArray<NSString *> *)oldUpList
regionId:(NSString *)regionId {
if (self = [super init]) {
self.zonesInfo = [self createZonesInfo:accUpList domains:upList oldDomains:oldUpList regionId:regionId];
}
return self;
}

- (QNZonesInfo *)getZonesInfoWithToken:(QNUpToken *)token {
return self.zonesInfo;
Expand Down
2 changes: 1 addition & 1 deletion QiniuSDK/Common/QNZoneInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ + (QNZoneInfo *)zoneInfoWithAccHosts:(NSArray <NSString *> *)accHosts

QNZoneInfo *zoneInfo = [QNZoneInfo zoneInfoFromDictionary:@{@"ttl" : @(-1),
@"region" : regionId ?: QNZoneInfoEmptyRegionId,
@"up" : @{@"acc_domains" : mainHosts ?: @[],
@"up" : @{@"acc_domains" : accHosts ?: @[],
@"domains" : mainHosts ?: @[],
@"old" : oldHosts ?: @[]},
}];
Expand Down
2 changes: 2 additions & 0 deletions QiniuSDK/Http/QNResponseInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
@property (nonatomic, readonly) BOOL isHostUnavailable;
/// 在断点续上传过程中,ctx 信息已过期。
@property (nonatomic, readonly) BOOL isCtxExpiedError;
/// 是否是加速配置错误
@property (nonatomic, readonly) BOOL isTransferAccelerationConfigureError;
/// 是否为 七牛响应
@property (nonatomic, readonly, getter=isNotQiniu) BOOL notQiniu;

Expand Down
16 changes: 15 additions & 1 deletion QiniuSDK/Http/QNResponseInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ - (BOOL)couldRegionRetry{
return YES;
}

if ([self isTransferAccelerationConfigureError]) {
return YES;
}

if (self.isCancelled
|| _statusCode == 100
|| (_statusCode > 300 && _statusCode < 500 && _statusCode != 406)
Expand All @@ -276,6 +280,10 @@ - (BOOL)couldHostRetry{
return YES;
}

if ([self isTransferAccelerationConfigureError]) {
return YES;
}

if (self.isCancelled
|| _statusCode == 100
|| (_statusCode > 300 && _statusCode < 500 && _statusCode != 406)
Expand All @@ -301,7 +309,8 @@ - (BOOL)canConnectToHost{

- (BOOL)isHostUnavailable{
// 基本不可恢复,注:会影响下次请求,范围太大可能会造成大量的timeout
if (_statusCode == 502 || _statusCode == 503 || _statusCode == 504 || _statusCode == 599) {
if ([self isTransferAccelerationConfigureError] ||
_statusCode == 502 || _statusCode == 503 || _statusCode == 504 || _statusCode == 599) {
return true;
} else {
return false;
Expand All @@ -312,6 +321,11 @@ - (BOOL)isCtxExpiedError {
return _statusCode == 701 || (_statusCode == 612 && [_message containsString:@"no such uploadId"]);
}

- (BOOL)isTransferAccelerationConfigureError {
NSString *errorDesc = [NSString stringWithFormat:@"%@", self.error];
return [errorDesc containsString:@"transfer acceleration is not configured on this bucket"];
}

- (BOOL)isConnectionBroken {
return _statusCode == kQNNetworkError || _statusCode == NSURLErrorNotConnectedToInternet;
}
Expand Down
4 changes: 4 additions & 0 deletions QiniuSDK/Http/ServerRegion/QNUploadDomainRegion.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@

NS_ASSUME_NONNULL_BEGIN

@class QNConfiguration;

@interface QNUploadDomainRegion : NSObject <QNUploadRegion>

- (instancetype)initWithConfig:(QNConfiguration *)config;

@end

NS_ASSUME_NONNULL_END
35 changes: 18 additions & 17 deletions QiniuSDK/Http/ServerRegion/QNUploadDomainRegion.m
Original file line number Diff line number Diff line change
Expand Up @@ -271,39 +271,40 @@ - (void)updateIpListFormHost:(NSString *)host {

BOOL accelerate = YES;
@synchronized (self) {
if (self.enableAccelerateUpload && responseInfo.error != nil &&
[[NSString stringWithFormat:@"%@", responseInfo.error]
containsString:@"transfer acceleration is not configured on this bucket"]) {
self.enableAccelerateUpload = true;
if (self.enableAccelerateUpload && responseInfo.isTransferAccelerationConfigureError) {
self.enableAccelerateUpload = NO;
}
accelerate = self.enableAccelerateUpload;
}

NSArray *hostList = nil;
NSDictionary *domainInfo = nil;
NSMutableArray *hostList = [NSMutableArray array];
NSMutableDictionary *domainInfo = [NSMutableDictionary dictionary];
if (requestState.isUseOldServer) {
hostList = self.oldDomainHostList;
domainInfo = self.oldDomainDictionary;
if (self.oldDomainHostList.count > 0 && self.oldDomainDictionary.count > 0) {
[hostList addObjectsFromArray:self.oldDomainHostList];
[domainInfo addEntriesFromDictionary:self.oldDomainDictionary];
}
} else {

// 优先使用 acc
if (accelerate &&
self.accDomainHostList.count > 0 &&
self.accDomainDictionary.count > 0) {
hostList = self.accDomainHostList;
domainInfo = self.accDomainDictionary;
} else {
hostList = self.domainHostList;
domainInfo = self.domainDictionary;
[hostList addObjectsFromArray:self.accDomainHostList];
[domainInfo addEntriesFromDictionary:self.accDomainDictionary];
}

if (self.domainHostList.count > 0 &&
self.domainDictionary.count > 0){
[hostList addObjectsFromArray:self.domainHostList];
[domainInfo addEntriesFromDictionary:self.domainDictionary];
}
}

if (hostList.count == 0 || domainInfo.count == 0) {
return nil;
}

if (requestState.isUseOldServer && self.oldDomainHostList.count > 0 && self.oldDomainDictionary.count > 0) {
hostList = self.oldDomainHostList;
domainInfo = self.oldDomainDictionary;
}

QNUploadServer *server = nil;
// 1. 优先使用http3
Expand Down
4 changes: 2 additions & 2 deletions QiniuSDK/Storage/QNBaseUpload.m
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ - (void)run {

if (responseInfo != nil && responseInfo.isOK && zonesInfo) {
if (![self setupRegions:zonesInfo]) {
responseInfo = [QNResponseInfo responseInfoWithInvalidArgument:[NSString stringWithFormat:@"origin response:%@", responseInfo]];
responseInfo = [QNResponseInfo responseInfoWithInvalidArgument:[NSString stringWithFormat:@"setup regions host fail, origin response:%@", responseInfo]];
[self complete:responseInfo response:responseInfo.responseDictionary];
return;
}
Expand Down Expand Up @@ -221,7 +221,7 @@ - (BOOL)setupRegions:(QNZonesInfo *)zonesInfo{
NSMutableArray *defaultRegions = [NSMutableArray array];
NSArray *zoneInfos = zonesInfo.zonesInfo;
for (QNZoneInfo *zoneInfo in zoneInfos) {
QNUploadDomainRegion *region = [[QNUploadDomainRegion alloc] init];
QNUploadDomainRegion *region = [[QNUploadDomainRegion alloc] initWithConfig:self.config];
[region setupRegionData:zoneInfo];
if (region.isValid) {
[defaultRegions addObject:region];
Expand Down
2 changes: 1 addition & 1 deletion QiniuSDK/Storage/QNConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ typedef void (^QNConfigurationBuilderBlock)(QNConfigurationBuilder *builder);
@property(nonatomic, copy) NSArray <NSString *> *dohIpv6Servers;

/**
* Host全局冻结时间 单位:秒 默认:10 推荐范围:[5 ~ 30]
* Host全局冻结时间 单位:秒 默认:60 推荐范围:[30 ~ 120]
* 当某个Host的上传失败后并且可能短时间无法恢复,会冻结该Host
*/
@property(nonatomic, assign)UInt32 globalHostFrozenTime;
Expand Down
2 changes: 1 addition & 1 deletion QiniuSDK/Storage/QNConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ - (void)setupData{
_udpDnsEnable = true;
_defaultUdpDnsIpv4Servers = [self parseBase64Array:@"WyIyMjMuNS41LjUiLCAiMTE0LjExNC4xMTQuMTE0IiwgIjEuMS4xLjEiLCAiOC44LjguOCJd"];

_globalHostFrozenTime = 10;
_globalHostFrozenTime = 60;
_partialHostFrozenTime = 5*60;

_connectCheckEnable = YES;
Expand Down

0 comments on commit f7e8511

Please sign in to comment.