带阿里标识的版本
This commit is contained in:
@@ -71,8 +71,8 @@ typedef struct {
|
||||
+ (void)initialize {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
_persistentCacheConcurrentQueue = dispatch_queue_create("com.alibaba.sdk.httpdns.persistentCacheOperationQueue", DISPATCH_QUEUE_CONCURRENT);
|
||||
_asyncResolveHostQueue = dispatch_queue_create("com.alibaba.sdk.httpdns.asyncResolveHostQueue", DISPATCH_QUEUE_CONCURRENT);
|
||||
_persistentCacheConcurrentQueue = dispatch_queue_create("com.Trust.sdk.httpdns.persistentCacheOperationQueue", DISPATCH_QUEUE_CONCURRENT);
|
||||
_asyncResolveHostQueue = dispatch_queue_create("com.Trust.sdk.httpdns.asyncResolveHostQueue", DISPATCH_QUEUE_CONCURRENT);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -113,7 +113,7 @@ typedef struct {
|
||||
// 先清理过期时间超过阈值的缓存结果
|
||||
[self->_httpdnsDB cleanRecordAlreadExpiredAt:[[NSDate date] timeIntervalSince1970] - duration];
|
||||
|
||||
// 再读取持久化缓存中的历史记录,加载到内存缓存里
|
||||
// 再读取持久化缓存中的历史记录,加载到内存缓存<EFBFBD><EFBFBD>?
|
||||
[self loadCacheFromDbToMemory];
|
||||
});
|
||||
}
|
||||
@@ -139,7 +139,7 @@ typedef struct {
|
||||
return;
|
||||
}
|
||||
|
||||
// 分批处理,每批最多5个域名
|
||||
// 分批处理,每批最<EFBFBD><EFBFBD>?个域<EFBFBD><EFBFBD>?
|
||||
NSUInteger totalCount = hosts.count;
|
||||
for (NSUInteger i = 0; i < totalCount; i += HTTPDNS_PRE_RESOLVE_BATCH_SIZE) {
|
||||
NSUInteger length = MIN(HTTPDNS_PRE_RESOLVE_BATCH_SIZE, totalCount - i);
|
||||
@@ -190,21 +190,21 @@ typedef struct {
|
||||
if (isCachedResultUsable) {
|
||||
if (isResolvingRequired) {
|
||||
// 缓存结果可用,但是需要请求,因为缓存结果已经过期
|
||||
// 这种情况异步去解析就可以了
|
||||
// 这种情况异步去解析就可以<EFBFBD><EFBFBD>?
|
||||
[self determineResolvingHostNonBlocking:request];
|
||||
}
|
||||
// 缓存是以cacheKey为准,这里返回前,要把host替换成用户请求的这个
|
||||
result.hostName = host;
|
||||
HttpdnsLogDebug("Reuse available cache for cacheKey: %@, result: %@", cacheKey, result);
|
||||
// 因为缓存结果可用,可以立即返回
|
||||
// 因为缓存结果可用,可以立即返<EFBFBD><EFBFBD>?
|
||||
return result;
|
||||
}
|
||||
|
||||
if (request.isBlockingRequest) {
|
||||
// 缓存结果不可用,且是同步请求,需要等待结果
|
||||
// 缓存结果不可用,且是同步请求,需要等待结<EFBFBD><EFBFBD>?
|
||||
return [self determineResolveHostBlocking:request];
|
||||
} else {
|
||||
// 缓存结果不可用,且是异步请求,不需要等待结果
|
||||
// 缓存结果不可用,且是异步请求,不需要等待结<EFBFBD><EFBFBD>?
|
||||
[self determineResolvingHostNonBlocking:request];
|
||||
return nil;
|
||||
}
|
||||
@@ -264,14 +264,14 @@ typedef struct {
|
||||
|
||||
if ([hostObject isExpiredUnderQueryIpType:queryType]) {
|
||||
if (self.atomicExpiredIPEnabled || [hostObject isLoadFromDB]) {
|
||||
// 只有开启了允许过期缓存,和开启持久化缓存情况下启动后加载到内存中的缓存,才可以直接复用过期结果
|
||||
// 只有开启了允许过期缓存,和开启持久化缓存情况下启动后加载到内存中的缓存,才可以直接复用过期结<EFBFBD><EFBFBD>?
|
||||
HttpdnsLogDebug("The ips is expired, but we accept it, host: %@, queryType: %ld, expiredIpEnabled: %d, isLoadFromDB: %d",
|
||||
hostObject.hostName, queryType, self.atomicExpiredIPEnabled, [hostObject isLoadFromDB]);
|
||||
// 复用过期结果,同时也需要发起新的解析请求
|
||||
// 复用过期结果,同时也需要发起新的解析请<EFBFBD><EFBFBD>?
|
||||
return (HostObjectExamingResult){YES, YES};
|
||||
}
|
||||
|
||||
// 只要过期了就肯定需要请求
|
||||
// 只要过期了就肯定需要请<EFBFBD><EFBFBD>?
|
||||
return (HostObjectExamingResult){NO, YES};
|
||||
}
|
||||
|
||||
@@ -310,7 +310,7 @@ typedef struct {
|
||||
return nil;
|
||||
}
|
||||
|
||||
// 这个路径里,host只会有一个,所以直接取第一个处理就行
|
||||
// 这个路径里,host只会有一个,所以直接取第一个处理就<EFBFBD><EFBFBD>?
|
||||
result = resultArray.firstObject;
|
||||
} else {
|
||||
if (!self.degradeToLocalDNSEnabled) {
|
||||
@@ -332,7 +332,7 @@ typedef struct {
|
||||
|
||||
// merge之后,返回的应当是存储在缓存中的实际对象,而非请求过程中构造出来的对象
|
||||
HttpdnsHostObject *lookupResult = [self mergeLookupResultToManager:result host:host cacheKey:cacheKey underQueryIpType:queryIPType];
|
||||
// 返回一个快照,避免进行中的一些缓存调整影响返回去的结果
|
||||
// 返回一个快照,避免进行中的一些缓存调整影响返回去的结<EFBFBD><EFBFBD>?
|
||||
return [lookupResult copy];
|
||||
}
|
||||
|
||||
@@ -483,7 +483,7 @@ typedef struct {
|
||||
HttpdnsNetworkStatus currentStatus = [[HttpdnsReachability sharedInstance] currentReachabilityStatus];
|
||||
NSString *currentStatusString = [[HttpdnsReachability sharedInstance] currentReachabilityString];
|
||||
|
||||
// 重新检测协议栈代价小,所以只要网络切换就发起检测
|
||||
// 重新检测协议栈代价小,所以只要网络切换就发起检<EFBFBD><EFBFBD>?
|
||||
// 但考虑到网络切换后不稳定,还是延迟1秒才发起
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
|
||||
[[HttpdnsIpStackDetector sharedInstance] redetectIpStack];
|
||||
@@ -493,23 +493,23 @@ typedef struct {
|
||||
BOOL statusChanged = (_lastNetworkStatus != currentStatus);
|
||||
|
||||
// 仅在以下情况下响应网络变化去尝试更新缓存:
|
||||
// - 距离上次处理事件至少过去了较长时间,或
|
||||
// - 网络状态发生变化且至少过去了较短时间
|
||||
// - 距离上次处理事件至少过去了较长时间,<EFBFBD><EFBFBD>?
|
||||
// - 网络状态发生变化且至少过去了较短时<EFBFBD><EFBFBD>?
|
||||
NSTimeInterval elapsedTime = currentTimestamp - _lastUpdateTimestamp;
|
||||
if (elapsedTime >= 5 || (statusChanged && elapsedTime >= 1)) {
|
||||
HttpdnsLogDebug("Processing network change: oldStatus: %ld, newStatus: %ld(%@), elapsedTime=%.2f seconds",
|
||||
_lastNetworkStatus, currentStatus, currentStatusString, elapsedTime);
|
||||
|
||||
// 更新调度
|
||||
// 网络在切换过程中可能不稳定,所以发送请求前等待2秒
|
||||
// 网络在切换过程中可能不稳定,所以发送请求前等待2<EFBFBD><EFBFBD>?
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
|
||||
HttpdnsScheduleCenter *scheduleCenter = self.ownerService.scheduleCenter;
|
||||
[scheduleCenter asyncUpdateRegionScheduleConfig];
|
||||
});
|
||||
|
||||
// 复杂逻辑(仅注释关键点,避免冗余):
|
||||
// 网络切换后只预解析“cacheKey 等于 hostName”的条目,
|
||||
// 这些条目代表标准域名缓存;SDNS 等使用自定义 cacheKey 的记录不在此批次处理。
|
||||
// 网络切换后只预解析“cacheKey 等于 hostName”的条目<EFBFBD><EFBFBD>?
|
||||
// 这些条目代表标准域名缓存;SDNS 等使用自定义 cacheKey 的记录不在此批次处理<EFBFBD><EFBFBD>?
|
||||
NSArray *allKeys = [_hostObjectInMemoryCache allCacheKeys];
|
||||
NSMutableArray<NSString *> *hostArray = [NSMutableArray array];
|
||||
for (NSString *key in allKeys) {
|
||||
@@ -524,10 +524,10 @@ typedef struct {
|
||||
}
|
||||
}
|
||||
|
||||
// 预解析
|
||||
// 网络在切换过程中可能不稳定,所以在清理缓存和发送请求前等待3秒
|
||||
// 预解<EFBFBD><EFBFBD>?
|
||||
// 网络在切换过程中可能不稳定,所以在清理缓存和发送请求前等待3<EFBFBD><EFBFBD>?
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_global_queue(0, 0), ^{
|
||||
// 仅清理“hostName 键”的缓存,保留 SDNS 等自定义 cacheKey 的记录
|
||||
// 仅清理“hostName 键”的缓存,保<EFBFBD><EFBFBD>?SDNS 等自定义 cacheKey 的记<EFBFBD><EFBFBD>?
|
||||
for (NSString *host in hostArray) {
|
||||
[self->_hostObjectInMemoryCache removeHostObjectByCacheKey:host];
|
||||
}
|
||||
@@ -538,7 +538,7 @@ typedef struct {
|
||||
}
|
||||
});
|
||||
|
||||
// 更新时间戳和状态
|
||||
// 更新时间戳和状<EFBFBD><EFBFBD>?
|
||||
_lastNetworkStatus = currentStatus;
|
||||
_lastUpdateTimestamp = currentTimestamp;
|
||||
} else {
|
||||
@@ -552,7 +552,7 @@ typedef struct {
|
||||
|
||||
- (dispatch_queue_t)cacheQueue {
|
||||
if (!_cacheQueue) {
|
||||
_cacheQueue = dispatch_queue_create("com.alibaba.sdk.httpdns.cacheDisableStatusQueue", DISPATCH_QUEUE_SERIAL);
|
||||
_cacheQueue = dispatch_queue_create("com.Trust.sdk.httpdns.cacheDisableStatusQueue", DISPATCH_QUEUE_SERIAL);
|
||||
}
|
||||
return _cacheQueue;
|
||||
}
|
||||
@@ -573,7 +573,7 @@ typedef struct {
|
||||
|
||||
HttpdnsHostObject *hostObject = [HttpdnsHostObject fromDBRecord:hostRecord];
|
||||
|
||||
// 从持久层加载到内存的缓存,需要做个标记,App启动后从缓存使用结果时,根据标记做特殊处理
|
||||
// 从持久层加载到内存的缓存,需要做个标记,App启动后从缓存使用结果时,根据标记做特殊处<EFBFBD><EFBFBD>?
|
||||
[hostObject setIsLoadFromDB:YES];
|
||||
|
||||
[self->_hostObjectInMemoryCache setHostObject:hostObject forCacheKey:cacheKey];
|
||||
@@ -596,7 +596,7 @@ typedef struct {
|
||||
}
|
||||
}
|
||||
|
||||
// 清空数据库数据
|
||||
// 清空数据库数<EFBFBD><EFBFBD>?
|
||||
dispatch_async(_persistentCacheConcurrentQueue, ^{
|
||||
[self->_httpdnsDB deleteByHostNameArr:hostArray];
|
||||
});
|
||||
@@ -605,7 +605,7 @@ typedef struct {
|
||||
- (void)cleanMemoryAndPersistentCacheOfAllHosts {
|
||||
[_hostObjectInMemoryCache removeAllHostObjects];
|
||||
|
||||
// 清空数据库数据
|
||||
// 清空数据库数<EFBFBD><EFBFBD>?
|
||||
dispatch_async(_persistentCacheConcurrentQueue, ^{
|
||||
[self->_httpdnsDB deleteAll];
|
||||
});
|
||||
@@ -622,7 +622,7 @@ typedef struct {
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark - 以下函数仅用于测试目的
|
||||
#pragma mark - 以下函数仅用于测试目<EFBFBD><EFBFBD>?
|
||||
|
||||
- (NSString *)showMemoryCache {
|
||||
NSString *cacheDes;
|
||||
|
||||
Reference in New Issue
Block a user