Skip to content

Commit

Permalink
feat: 优化多线程下数组取值逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Vastness committed Feb 8, 2022
1 parent 2d8cce4 commit 609aca7
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions MSDKDns/CacheManager/MSDKDnsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -734,20 +734,21 @@ - (void)switchDnsServer {
}
self.waitToSwitch = YES;
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
self.serverIndex += 1;
if (!self.firstFailTime) {
self.firstFailTime = [NSDate date];
// 一定时间后自动切回主ip
__weak __typeof__(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,[[MSDKDnsParamsManager shareInstance] msdkDnsGetMinutesBeforeSwitchToMain] * 60 * NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{
if (weakSelf.firstFailTime && [[NSDate date] timeIntervalSinceDate:weakSelf.firstFailTime] >= [[MSDKDnsParamsManager shareInstance] msdkDnsGetMinutesBeforeSwitchToMain] * 60) {
MSDKDNSLOG(@"auto reset server index, use main ip now.");
weakSelf.serverIndex = 0;
weakSelf.firstFailTime = nil;
}
});
}
if (self.serverIndex >= [[[MSDKDnsParamsManager shareInstance] msdkDnsGetServerIps] count]) {
if (self.serverIndex < [[[MSDKDnsParamsManager shareInstance] msdkDnsGetServerIps] count]) {
self.serverIndex += 1;
if (!self.firstFailTime) {
self.firstFailTime = [NSDate date];
// 一定时间后自动切回主ip
__weak __typeof__(self) weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW,[[MSDKDnsParamsManager shareInstance] msdkDnsGetMinutesBeforeSwitchToMain] * 60 * NSEC_PER_SEC), [MSDKDnsInfoTool msdkdns_queue], ^{
if (weakSelf.firstFailTime && [[NSDate date] timeIntervalSinceDate:weakSelf.firstFailTime] >= [[MSDKDnsParamsManager shareInstance] msdkDnsGetMinutesBeforeSwitchToMain] * 60) {
MSDKDNSLOG(@"auto reset server index, use main ip now.");
weakSelf.serverIndex = 0;
weakSelf.firstFailTime = nil;
}
});
}
} else {
self.serverIndex = 0;
self.firstFailTime = nil;
}
Expand Down

0 comments on commit 609aca7

Please sign in to comment.