Skip to content

Commit b0c465b

Browse files
author
yangsen
committed
auto zone: support set default zones
1 parent 69df1df commit b0c465b

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

QiniuSDK/Common/QNAutoZone.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010

1111
NS_ASSUME_NONNULL_BEGIN
1212

13+
@class QNFixedZone;
1314
@interface QNAutoZone : QNZone
1415

1516
+ (instancetype)zoneWithUcHosts:(NSArray *)ucHosts;
1617

1718
+ (void)clearCache;
1819

20+
/**
21+
* 当 查询失败时,会使用 zones 进行上传,默认不配置。
22+
*/
23+
- (void)setDefaultZones:(NSArray <QNFixedZone *> *)zones;
24+
1925
@end
2026

2127
NS_ASSUME_NONNULL_END

QiniuSDK/Common/QNAutoZone.m

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ @implementation QNUCQuerySingleFlightValue
9797
@interface QNAutoZone()
9898

9999
@property(nonatomic, strong)NSArray *ucHosts;
100+
@property(nonatomic, strong)QNFixedZone *defaultZone;
100101
@property(nonatomic, strong)NSMutableArray <QNRequestTransaction *> *transactions;
101102

102103
@end
@@ -128,6 +129,10 @@ - (instancetype)init{
128129
return self;
129130
}
130131

132+
- (void)setDefaultZones:(NSArray <QNFixedZone *> *)zones {
133+
self.defaultZone = [QNFixedZone combineZones:zones];
134+
}
135+
131136
- (QNZonesInfo *)getZonesInfoWithToken:(QNUpToken * _Nullable)token
132137
actionType:(QNActionType)actionType {
133138

@@ -196,9 +201,17 @@ - (void)preQuery:(QNUpToken *)token actionType:(QNActionType)actionType on:(QNPr
196201
if (responseInfo.isConnectionBroken) {
197202
ret(kQNNetworkError, responseInfo, metrics);
198203
} else {
199-
QNZonesInfo *zonesInfo = [[QNFixedZone localsZoneInfo] getZonesInfoWithToken:token];
200-
if ([zonesInfo isValid]) {
201-
[[QNAutoZoneCache share] cache:zonesInfo forKey:cacheKey];
204+
QNZonesInfo *info = nil;
205+
if (self.defaultZone) {
206+
QNZonesInfo * infoP = [self.defaultZone getZonesInfoWithToken:token actionType:actionType];
207+
if (infoP && [infoP isValid]) {
208+
[infoP toTemporary];
209+
info = infoP;
210+
}
211+
}
212+
213+
if (info) {
214+
[[QNAutoZoneCache share] cache:info forKey:cacheKey];
202215
ret(0, responseInfo, metrics);
203216
} else {
204217
ret(-1, responseInfo, metrics);

QiniuSDK/Common/QNFixedZone.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ NS_ASSUME_NONNULL_BEGIN
8383
*/
8484
+ (QNFixedZone *)localsZoneInfo;
8585

86+
/**
87+
* 合并区域
88+
*/
89+
+ (QNFixedZone *)combineZones:(NSArray<QNFixedZone *> *)zones;
90+
8691
@end
8792

8893
NS_ASSUME_NONNULL_END

QiniuSDK/Common/QNFixedZone.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,17 @@ + (QNFixedZone *)localsZoneInfo{
103103
[QNFixedZone zoneNa0],
104104
[QNFixedZone zoneApNorthEast1],
105105
[QNFixedZone zoneAs0]];
106+
QNFixedZone *zone = [self combineZones:zones];
107+
if (zone) {
108+
[zone.zonesInfo toTemporary];
109+
}
110+
return zone;
111+
}
112+
113+
+ (QNFixedZone *)combineZones:(NSArray<QNFixedZone *> *)zones {
114+
if (zones == nil || zones.count == 0) {
115+
return nil;
116+
}
106117

107118
NSMutableArray <QNZoneInfo *> *zoneInfoArray = [NSMutableArray array];
108119
for (QNFixedZone *zone in zones) {

0 commit comments

Comments
 (0)