-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #384 from YangSen-qn/develop
UC Query Add SingleFlight & connect check
- Loading branch information
Showing
40 changed files
with
796 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// QNConnectChecker.h | ||
// QiniuSDK_Mac | ||
// | ||
// Created by yangsen on 2021/1/8. | ||
// Copyright © 2021 Qiniu. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface QNConnectChecker : NSObject | ||
|
||
+ (BOOL)check; | ||
|
||
+ (void)check:(void(^)(BOOL isConnected))complete; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// | ||
// QNConnectChecker.m | ||
// QiniuSDK_Mac | ||
// | ||
// Created by yangsen on 2021/1/8. | ||
// Copyright © 2021 Qiniu. All rights reserved. | ||
// | ||
|
||
#import "QNDefine.h" | ||
#import "QNLogUtil.h" | ||
#import "QNConfiguration.h" | ||
#import "QNSingleFlight.h" | ||
#import "QNConnectChecker.h" | ||
#import "QNUploadSystemClient.h" | ||
|
||
@interface QNConnectChecker() | ||
|
||
@end | ||
@implementation QNConnectChecker | ||
|
||
+ (QNSingleFlight *)singleFlight { | ||
static QNSingleFlight *singleFlight = nil; | ||
static dispatch_once_t onceToken; | ||
dispatch_once(&onceToken, ^{ | ||
singleFlight = [[QNSingleFlight alloc] init]; | ||
}); | ||
return singleFlight; | ||
} | ||
|
||
+ (BOOL)check { | ||
__block BOOL isConnected = false; | ||
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); | ||
[self check:^(BOOL isConnectedP) { | ||
isConnected = isConnectedP; | ||
dispatch_semaphore_signal(semaphore); | ||
}]; | ||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); | ||
return isConnected; | ||
} | ||
|
||
+ (void)check:(void (^)(BOOL isConnected))complete { | ||
QNSingleFlight *singleFlight = [self singleFlight]; | ||
|
||
kQNWeakSelf; | ||
[singleFlight perform:@"connect_check" action:^(QNSingleFlightComplete _Nonnull singleFlightComplete) { | ||
kQNStrongSelf; | ||
|
||
[self checkAllHosts:^(BOOL isConnected) { | ||
singleFlightComplete(@(isConnected), nil); | ||
}]; | ||
|
||
} complete:^(id _Nullable value, NSError * _Nullable error) { | ||
if (complete) { | ||
complete([(NSNumber *)value boolValue]); | ||
} | ||
}]; | ||
} | ||
|
||
|
||
+ (void)checkAllHosts:(void (^)(BOOL isConnected))complete { | ||
|
||
__block int completeCount = 0; | ||
__block BOOL isCompleted = false; | ||
__block BOOL isConnected = false; | ||
kQNWeakSelf; | ||
NSArray *allHosts = [kQNGlobalConfiguration.connectCheckURLStrings copy]; | ||
for (NSString *host in allHosts) { | ||
[self checkHost:host complete:^(BOOL isHostConnected) { | ||
kQNStrongSelf; | ||
|
||
@synchronized (self) { | ||
completeCount += 1; | ||
} | ||
if (isHostConnected) { | ||
isConnected = YES; | ||
} | ||
if (isHostConnected || completeCount == allHosts.count) { | ||
@synchronized (self) { | ||
if (isCompleted) { | ||
QNLogInfo(@"== check all hosts has completed totalCount:%d completeCount:%d", allHosts.count, completeCount); | ||
return; | ||
} else { | ||
QNLogInfo(@"== check all hosts completed totalCount:%d completeCount:%d", allHosts.count, completeCount); | ||
isCompleted = true; | ||
} | ||
} | ||
complete(isConnected); | ||
} else { | ||
QNLogInfo(@"== check all hosts not completed totalCount:%d completeCount:%d", allHosts.count, completeCount); | ||
} | ||
}]; | ||
} | ||
} | ||
|
||
+ (void)checkHost:(NSString *)host complete:(void (^)(BOOL isConnected))complete { | ||
|
||
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; | ||
request.URL = [NSURL URLWithString:host]; | ||
request.HTTPMethod = @"HEAD"; | ||
request.timeoutInterval = kQNGlobalConfiguration.connectCheckTimeout; | ||
|
||
QNUploadSystemClient *client = [[QNUploadSystemClient alloc] init]; | ||
[client request:request connectionProxy:nil progress:nil complete:^(NSURLResponse * response, QNUploadSingleRequestMetrics * metrics, NSData * _Nullable data, NSError * error) { | ||
if (response && [(NSHTTPURLResponse *)response statusCode] > 99) { | ||
QNLogInfo(@"== checkHost:%@ result: true", host); | ||
complete(true); | ||
} else { | ||
QNLogInfo(@"== checkHost:%@ result: false", host); | ||
complete(false); | ||
} | ||
}]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.