@@ -29,6 +29,7 @@ @interface MSDKDnsManager ()
29
29
@property (nonatomic , assign , readwrite ) HttpDnsSdkStatus sdkStatus;
30
30
@property (nonatomic , strong , readwrite ) NSArray * dnsServers;
31
31
@property (strong , nonatomic ) NSMutableURLRequest *request;
32
+ @property (strong , nonatomic , readwrite ) NSMutableDictionary * cacheDomainCountDict;
32
33
33
34
@end
34
35
@@ -43,6 +44,10 @@ - (void)dealloc {
43
44
[self .serviceArray removeAllObjects ];
44
45
[self setServiceArray: nil ];
45
46
}
47
+ if (_cacheDomainCountDict) {
48
+ [self .cacheDomainCountDict removeAllObjects ];
49
+ [self setCacheDomainCountDict: nil ];
50
+ }
46
51
}
47
52
48
53
#pragma mark - init
@@ -64,6 +69,7 @@ - (instancetype) init {
64
69
_serviceArray = [[NSMutableArray alloc ] init ];
65
70
_sdkStatus = net_undetected;
66
71
_dnsServers = [self defaultServers ];
72
+ _cacheDomainCountDict = [[NSMutableDictionary alloc ] init ];
67
73
}
68
74
return self;
69
75
}
@@ -200,6 +206,55 @@ - (NSDictionary *)getHostsByNamesEnableExpired:(NSArray *)domains verbose:(BOOL)
200
206
NSDictionary * result = verbose?
201
207
[self fullResultDictionaryEnableExpired: domains fromCache: cacheDomainDict toEmpty: toEmptyDomains] :
202
208
[self resultDictionaryEnableExpired: domains fromCache: cacheDomainDict toEmpty: toEmptyDomains];
209
+ // 当开启乐观DNS之后,如果结果为0则上报errorCode=3,要所有结果都为0
210
+ BOOL needReport = NO ;
211
+ if (verbose) {
212
+ for (int i = 0 ; i < [domains count ]; i++) {
213
+ NSString *domain = [domains objectAtIndex: i];
214
+ NSDictionary *domainData = result[domain];
215
+ if (domainData && domainData.count > 0 ) {
216
+ needReport = NO ;
217
+ break ;
218
+ } else {
219
+ needReport = YES ;
220
+ }
221
+ }
222
+ } else {
223
+ for (int i = 0 ; i < [domains count ]; i++) {
224
+ NSString *domain = [domains objectAtIndex: i];
225
+ NSArray *domainResArray = result[domain];
226
+ if (domainResArray && domainResArray.count > 0 ) {
227
+ if ([domainResArray[0 ] isEqualToString: @" 0" ] && [domainResArray[1 ] isEqualToString: @" 0" ]) {
228
+ needReport = YES ;
229
+ } else {
230
+ needReport = NO ;
231
+ break ;
232
+ }
233
+ } else {
234
+ needReport = YES ;
235
+ }
236
+ }
237
+ }
238
+ if (needReport && domains) {
239
+ for (int i = 0 ; i < [domains count ]; i++) {
240
+ NSString *domain = [domains objectAtIndex: i];
241
+ [[AttaReport sharedInstance ] reportEvent: @{
242
+ MSDKDns_ErrorCode: MSDKDns_NoData,
243
+ @" eventName" : MSDKDnsEventHttpDnsCached,
244
+ @" dnsIp" : [[MSDKDnsManager shareInstance ] currentDnsServer ],
245
+ @" req_dn" : domain,
246
+ @" req_type" : @" a" ,
247
+ @" req_timeout" : @0 ,
248
+ @" req_ttl" : @0 ,
249
+ @" req_query" : @0 ,
250
+ @" req_ip" : @" " ,
251
+ @" spend" : @0 ,
252
+ @" statusCode" : @0 ,
253
+ @" count" : @1 ,
254
+ @" isCache" : @1 ,
255
+ }];
256
+ }
257
+ }
203
258
return result;
204
259
}
205
260
@@ -566,14 +621,16 @@ - (void)clearCacheForDomain:(NSString *)domain {
566
621
567
622
- (void )clearCacheForDomains : (NSArray *)domains {
568
623
for (int i = 0 ; i < [domains count ]; i++) {
569
- NSString * domain = [domains objectAtIndex: i];
570
- [self clearCacheForDomain: domain];
624
+ if ([[domains objectAtIndex: i] isKindOfClass: [NSString class ]]) {
625
+ NSString * domain = [domains objectAtIndex: i];
626
+ [self clearCacheForDomain: domain];
627
+ }
571
628
}
572
629
}
573
630
574
631
- (void )clearAllCache {
575
632
dispatch_async ([MSDKDnsInfoTool msdkdns_queue ], ^{
576
- MSDKDNSLOG (@" MSDKDns clearCache " );
633
+ MSDKDNSLOG (@" MSDKDns cleared all caches! " );
577
634
if (self.domainDict ) {
578
635
[self .domainDict removeAllObjects ];
579
636
self.domainDict = nil ;
@@ -583,26 +640,43 @@ - (void)clearAllCache {
583
640
584
641
#pragma mark - uploadReport
585
642
- (void )hitCacheAttaUploadReport : (NSString *)domain {
586
- static int count = 0 ;
587
- count ++;
588
- if ([[MSDKDnsParamsManager shareInstance ] msdkDnsGetEnableReport ] && [[AttaReport sharedInstance ] shoulReportDnsSpend ]) {
589
- [[AttaReport sharedInstance ] reportEvent: @{
590
- MSDKDns_ErrorCode: MSDKDns_Success,
591
- @" eventName" : MSDKDnsEventHttpDnsCached,
592
- @" dnsIp" : [[MSDKDnsManager shareInstance ] currentDnsServer ],
593
- @" req_dn" : domain,
594
- @" req_type" : @" a" ,
595
- @" req_timeout" : @0 ,
596
- @" req_ttl" : @0 ,
597
- @" req_query" : @0 ,
598
- @" req_ip" : @" " ,
599
- @" spend" : @0 ,
600
- @" statusCode" : @0 ,
601
- @" count" :[NSString stringWithFormat: @" %d " , count],
602
- @" isCache" : @1 ,
603
- }];
604
- count = 0 ;
605
- }
643
+ // 检查控制台解析监控上报开关是否开启
644
+ if ([[MSDKDnsParamsManager shareInstance ] msdkDnsGetEnableReport ]) {
645
+ if (self.cacheDomainCountDict ) {
646
+ NSNumber *num = self.cacheDomainCountDict [domain];
647
+ if (num) {
648
+ int numInt = num.intValue + 1 ;
649
+ [self .cacheDomainCountDict setValue: [NSNumber numberWithInt: numInt] forKey: domain];
650
+ } else {
651
+ [self .cacheDomainCountDict setValue: [NSNumber numberWithInt: 1 ] forKey: domain];
652
+ }
653
+
654
+ if ([[AttaReport sharedInstance ] shoulReportDnsSpend ]) {
655
+ NSArray *dictKey = [self .cacheDomainCountDict allKeys ];
656
+ NSInteger length = [dictKey count ];
657
+ for (int i = 0 ; i < length; i++) {
658
+ id domainKey = [dictKey objectAtIndex: i];
659
+ NSNumber *cacheCount = [self .cacheDomainCountDict objectForKey: domainKey];
660
+ [[AttaReport sharedInstance ] reportEvent: @{
661
+ MSDKDns_ErrorCode: MSDKDns_Success,
662
+ @" eventName" : MSDKDnsEventHttpDnsCached,
663
+ @" dnsIp" : [[MSDKDnsManager shareInstance ] currentDnsServer ],
664
+ @" req_dn" : domainKey,
665
+ @" req_type" : @" a" ,
666
+ @" req_timeout" : @0 ,
667
+ @" req_ttl" : @0 ,
668
+ @" req_query" : @0 ,
669
+ @" req_ip" : @" " ,
670
+ @" spend" : @0 ,
671
+ @" statusCode" : @0 ,
672
+ @" count" : cacheCount,
673
+ @" isCache" : @1 ,
674
+ }];
675
+ }
676
+ [self .cacheDomainCountDict removeAllObjects ];
677
+ }
678
+ }
679
+ }
606
680
}
607
681
608
682
- (void )uploadReport : (BOOL )isFromCache Domain : (NSString *)domain NetStack : (msdkdns::MSDKDNS_TLocalIPStack)netStack {
@@ -1205,7 +1279,7 @@ - (void)switchToMainServer {
1205
1279
1206
1280
- (void )resetDnsServers : (NSArray *)servers {
1207
1281
self.waitToSwitch = YES ;
1208
- dispatch_async ([MSDKDnsInfoTool msdkdns_queue ], ^{
1282
+ dispatch_barrier_async ([MSDKDnsInfoTool msdkdns_resolver_queue ], ^{
1209
1283
NSMutableArray *array = [[NSMutableArray alloc ] init ];
1210
1284
if (servers && [servers count ] > 0 ) {
1211
1285
[array addObjectsFromArray: servers];
0 commit comments