Skip to content

Commit

Permalink
feat:优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
erichuyuehu committed Aug 11, 2022
1 parent a4f8c80 commit 8beac8d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 75 deletions.
36 changes: 18 additions & 18 deletions MSDKDns.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@
54EA82232760890B005F68A9 /* AttaReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 54EA82212760890B005F68A9 /* AttaReport.m */; };
54EA822427608A55005F68A9 /* AttaReport.h in Headers */ = {isa = PBXBuildFile; fileRef = 54EA82202760890B005F68A9 /* AttaReport.h */; };
54EA822527608A58005F68A9 /* AttaReport.m in Sources */ = {isa = PBXBuildFile; fileRef = 54EA82212760890B005F68A9 /* AttaReport.m */; };
5FE4BAB4289A4E4700C7A944 /* HttpdnsSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FE4BAB1289A4E4700C7A944 /* HttpdnsSpeedTester.h */; };
5FE4BAB5289A4E4700C7A944 /* HttpdnsSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FE4BAB1289A4E4700C7A944 /* HttpdnsSpeedTester.h */; };
5FE4BAB6289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FE4BAB2289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h */; };
5FE4BAB7289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5FE4BAB2289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h */; };
5FE4BAB8289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FE4BAB3289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m */; };
5FE4BAB9289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m in Sources */ = {isa = PBXBuildFile; fileRef = 5FE4BAB3289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m */; };
5F5DAF4E28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */; };
5F5DAF4F28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */; };
5F5DAF5028A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */; };
5F5DAF5128A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */ = {isa = PBXBuildFile; fileRef = 5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */; };
5F5DAF5228A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */; };
5F5DAF5328A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */ = {isa = PBXBuildFile; fileRef = 5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */; };
C8EBE7D3256664C500BEFEEC /* aes.h in Headers */ = {isa = PBXBuildFile; fileRef = C8EBE7D1256664C400BEFEEC /* aes.h */; };
C8EBE7D4256664C500BEFEEC /* aes.mm in Sources */ = {isa = PBXBuildFile; fileRef = C8EBE7D2256664C500BEFEEC /* aes.mm */; };
C8EBE7F4256668A300BEFEEC /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = C8EBE7DA2566675800BEFEEC /* libc++.tbd */; };
Expand Down Expand Up @@ -110,9 +110,9 @@
54EA81F1275478D1005F68A9 /* httpdnsIps.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = httpdnsIps.h; sourceTree = "<group>"; };
54EA82202760890B005F68A9 /* AttaReport.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AttaReport.h; sourceTree = "<group>"; };
54EA82212760890B005F68A9 /* AttaReport.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AttaReport.m; sourceTree = "<group>"; };
5FE4BAB1289A4E4700C7A944 /* HttpdnsSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpdnsSpeedTester.h; sourceTree = "<group>"; };
5FE4BAB2289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HttpdnsTCPSpeedTester.h; sourceTree = "<group>"; };
5FE4BAB3289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HttpdnsTCPSpeedTester.m; sourceTree = "<group>"; };
5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsTCPSpeedTester.h; sourceTree = "<group>"; };
5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSDKDnsTCPSpeedTester.m; sourceTree = "<group>"; };
5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSDKDnsSpeedTester.h; sourceTree = "<group>"; };
C8EBE7D1256664C400BEFEEC /* aes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = aes.h; sourceTree = "<group>"; };
C8EBE7D2256664C500BEFEEC /* aes.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = aes.mm; sourceTree = "<group>"; };
C8EBE7DA2566675800BEFEEC /* libc++.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = "libc++.tbd"; path = "Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/lib/libc++.tbd"; sourceTree = DEVELOPER_DIR; };
Expand Down Expand Up @@ -174,9 +174,9 @@
4497F8BC1B46306200D51391 /* MSDKDnsPrivate.h */,
504F54381ECAF89F001BD7A9 /* MSDKDnsHttpMessageTools.h */,
504F54391ECAF89F001BD7A9 /* MSDKDnsHttpMessageTools.m */,
5FE4BAB1289A4E4700C7A944 /* HttpdnsSpeedTester.h */,
5FE4BAB2289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h */,
5FE4BAB3289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m */,
5F5DAF4D28A525E300BF5B79 /* MSDKDnsSpeedTester.h */,
5F5DAF4B28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h */,
5F5DAF4C28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m */,
C8EBE7D1256664C400BEFEEC /* aes.h */,
C8EBE7D2256664C500BEFEEC /* aes.mm */,
54EA821F2760890B005F68A9 /* Reporter */,
Expand Down Expand Up @@ -272,18 +272,18 @@
C8EBE7D3256664C500BEFEEC /* aes.h in Headers */,
445B36631CBD095C00BD4345 /* MSDKDnsManager.h in Headers */,
4497F8B81B4628F000D51391 /* MSDKDnsLog.h in Headers */,
5FE4BAB4289A4E4700C7A944 /* HttpdnsSpeedTester.h in Headers */,
4455D15E1B3A5B90005BF126 /* MSDKDns.h in Headers */,
448EE4E61B329899004A2131 /* LocalDnsResolver.h in Headers */,
DD5935581DDC56B200BF9348 /* HttpsDnsResolver.h in Headers */,
5F5DAF4E28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */,
448EE4E81B329899004A2131 /* MSDKDnsResolver.h in Headers */,
54EA82222760890B005F68A9 /* AttaReport.h in Headers */,
5F5DAF5228A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */,
44BFE2371CA58C3A00D7FE87 /* MSDKDnsInfoTool.h in Headers */,
502422EC2140073F0094403C /* MSDKDnsParamsManager.h in Headers */,
4497F8BD1B46306200D51391 /* MSDKDnsPrivate.h in Headers */,
444044F91B3133A30010F5D5 /* MSDKDnsService.h in Headers */,
501001F0215E1F1D003288A5 /* msdkdns_local_ip_stack.h in Headers */,
5FE4BAB6289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -297,18 +297,18 @@
2FBE52E825E77AFE0012A0DF /* aes.h in Headers */,
DD43F4A9231CC36D0000A89F /* MSDKDnsManager.h in Headers */,
DD43F4AA231CC36D0000A89F /* MSDKDnsLog.h in Headers */,
5FE4BAB5289A4E4700C7A944 /* HttpdnsSpeedTester.h in Headers */,
DD43F4AB231CC36D0000A89F /* MSDKDns.h in Headers */,
DD43F4AC231CC36D0000A89F /* LocalDnsResolver.h in Headers */,
DD43F4AD231CC36D0000A89F /* HttpsDnsResolver.h in Headers */,
5F5DAF4F28A525E300BF5B79 /* MSDKDnsTCPSpeedTester.h in Headers */,
DD43F4AE231CC36D0000A89F /* MSDKDnsResolver.h in Headers */,
54EA822427608A55005F68A9 /* AttaReport.h in Headers */,
5F5DAF5328A525E300BF5B79 /* MSDKDnsSpeedTester.h in Headers */,
DD43F4AF231CC36D0000A89F /* MSDKDnsInfoTool.h in Headers */,
DD43F4B0231CC36D0000A89F /* MSDKDnsParamsManager.h in Headers */,
DD43F4B1231CC36D0000A89F /* MSDKDnsPrivate.h in Headers */,
DD43F4B2231CC36D0000A89F /* MSDKDnsService.h in Headers */,
DD43F4B3231CC36D0000A89F /* msdkdns_local_ip_stack.h in Headers */,
5FE4BAB7289A4E4700C7A944 /* HttpdnsTCPSpeedTester.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -499,8 +499,8 @@
DD5935561DDC56B200BF9348 /* HttpsDnsResolver.m in Sources */,
445B36651CBD095C00BD4345 /* MSDKDnsManager.m in Sources */,
444044FA1B3133A30010F5D5 /* MSDKDnsService.m in Sources */,
5F5DAF5028A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */,
501001EE215E1F1D003288A5 /* msdkdns_local_ip_stack.cpp in Sources */,
5FE4BAB8289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m in Sources */,
44BFE24A1CA59D9800D7FE87 /* MSDKDnsReachability.m in Sources */,
C8EBE7D4256664C500BEFEEC /* aes.mm in Sources */,
445B366B1CBD1D4700BD4345 /* MSDKDnsNetworkManager.m in Sources */,
Expand All @@ -522,8 +522,8 @@
DD43F498231CC36D0000A89F /* HttpsDnsResolver.m in Sources */,
DD43F499231CC36D0000A89F /* MSDKDnsManager.m in Sources */,
DD43F49A231CC36D0000A89F /* MSDKDnsService.m in Sources */,
5F5DAF5128A525E300BF5B79 /* MSDKDnsTCPSpeedTester.m in Sources */,
2FBE52E925E77B000012A0DF /* aes.mm in Sources */,
5FE4BAB9289A4E4700C7A944 /* HttpdnsTCPSpeedTester.m in Sources */,
DD43F49B231CC36D0000A89F /* msdkdns_local_ip_stack.cpp in Sources */,
DD43F49C231CC36D0000A89F /* MSDKDnsReachability.m in Sources */,
DD43F49D231CC36D0000A89F /* MSDKDnsNetworkManager.m in Sources */,
Expand Down
13 changes: 0 additions & 13 deletions MSDKDns/HttpdnsTCPSpeedTester.h

This file was deleted.

8 changes: 4 additions & 4 deletions MSDKDns/MSDKDnsService.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#import "MSDKDnsManager.h"
#import "MSDKDnsNetworkManager.h"
#import "MSDKDnsParamsManager.h"
#import "HttpdnsTCPSpeedTester.h"
#import "MSDKDnsTCPSpeedTester.h"
#import "AttaReport.h"

@interface MSDKDnsService () <MSDKDnsResolverDelegate>
Expand Down Expand Up @@ -310,15 +310,15 @@ - (void)aysncUpdateIPRankingWithResult:(NSArray *)IPStrings forHost:(NSString *)
}

- (void)syncUpdateIPRankingWithResult:(NSArray *)IPStrings forHost:(NSString *)host {
NSArray *sortedIps = [[HttpdnsTCPSpeedTester new] ipRankingWithIPs:IPStrings host:host];
NSArray *sortedIps = [[MSDKDnsTCPSpeedTester new] ipRankingWithIPs:IPStrings host:host];
[self updateHostManagerDictWithIPs:sortedIps host:host];
}

- (void)updateHostManagerDictWithIPs:(NSArray *)IPs host:(NSString *)host {
if(!IPs){
return;
}
@synchronized(self) {
dispatch_async([MSDKDnsInfoTool msdkdns_queue], ^{
NSDictionary * tempDict = [[[MSDKDnsManager shareInstance] domainDict] objectForKey:host];
NSMutableDictionary *cacheDict;

Expand Down Expand Up @@ -351,7 +351,7 @@ - (void)updateHostManagerDictWithIPs:(NSArray *)IPs host:(NSString *)host {
[[MSDKDnsManager shareInstance] cacheDomainInfo:cacheDict Domain:host];
}
}
}
});
}

- (void)resolver:(MSDKDnsResolver *)resolver getDomainError:(NSString *)error retry:(BOOL)retry {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Tencent. All rights reserved.
*/

@protocol HttpdnsSpeedTester <NSObject>
@protocol MSDKDnsSpeedTester <NSObject>

- (float)testSpeedOf:(NSString *)ip port:(int16_t)port;
- (NSArray<NSString *> *)ipRankingWithIPs:(NSArray<NSString *> *)IPs host:(NSString *)host;
Expand Down
13 changes: 13 additions & 0 deletions MSDKDns/MSDKDnsTCPSpeedTester.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) Tencent. All rights reserved.
*/

#import <Foundation/Foundation.h>
#import "MSDKDnsSpeedTester.h"

#define MSDKDns_SOCKET_CONNECT_TIMEOUT 10 //单位秒
#define MSDKDns_SOCKET_CONNECT_TIMEOUT_RTT 600000//10分钟 单位毫秒

@interface MSDKDnsTCPSpeedTester : NSObject <MSDKDnsSpeedTester>

@end
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Tencent. All rights reserved.
*/

#import "HttpdnsTCPSpeedTester.h"
#import "MSDKDnsTCPSpeedTester.h"
#import "MSDKDnsParamsManager.h"
#import "MSDKDnsLog.h"
#import <sys/socket.h>
Expand All @@ -15,22 +15,11 @@
static NSString *const testSpeedKey = @"testSpeed";
static NSString *const ipKey = @"ip";

@implementation HttpdnsTCPSpeedTester
@implementation MSDKDnsTCPSpeedTester

/*!
* 如果用户对域名提供多个端口,取任意一个端口。
假设:同一个域名,不同端口到达速度一致。
让优选逻辑,尽量少de
15s 100s
- IP池在2个到5个范围内,才进行测速逻辑。
- 只在ttl未过期内测试。
- ~~只取内存缓存,与持久化缓存逻辑不产生交集。持久化优先级更高。~~ 无法区分持久化,持久化缓存也可能参与排序。
- 测速逻辑公开,作为最佳实践。
- 只在 IPv4 逻辑下测试,IPv6 环境不测。
- 开启IPv6解析结果时,不测试。
- 测速逻辑不能增加用户计费请求次数。
- 预加载也参与IP优选,网络请求成功就异步排序。
/**
*
- IP池在2个到9个范围内,才进行测速逻辑。
-
*/
- (NSArray<NSString *> *)ipRankingWithIPs:(NSArray<NSString *> *)IPs host:(NSString *)host {
Expand Down Expand Up @@ -61,7 +50,7 @@ @implementation HttpdnsTCPSpeedTester
float testSpeed = [self testSpeedOf:ip port:port];
MSDKDNSLOG(@"%@:%hd speed is %f",ip,port,testSpeed);
if (testSpeed == 0) {
testSpeed = HTTPDNS_SOCKET_CONNECT_TIMEOUT_RTT;
testSpeed = MSDKDns_SOCKET_CONNECT_TIMEOUT_RTT;
}
NSMutableDictionary *IPSpeed = [NSMutableDictionary dictionaryWithCapacity:2];
[IPSpeed setObject:@(testSpeed) forKey:testSpeedKey];
Expand All @@ -88,14 +77,7 @@ @implementation HttpdnsTCPSpeedTester
}

/**
* 本测速函数,使用linux socket connect 和select函数实现的。 基于以下原理
* 1. 即使套接口是非阻塞的。如果连接的服务器在同一台主机上,那么在调用connect 建立连接时,连接通常会立即建立成功,我们必须处理这种情况。
* 2. 源自Berkeley的实现(和Posix.1g)有两条与select 和非阻塞IO相关的规则:
* A. 当连接建立成功时,套接口描述符变成可写;
* B. 当连接出错时,套接口描述符变成既可读又可写。
* @param ip 用于测速对Ip,应该是IPv4格式。
*
* @return 测速结果,单位时毫秒,HTTPDNS_SOCKET_CONNECT_TIMEOUT_RTT 代表超时。
* @return 测速结果,单位时毫秒,MSDKDns_SOCKET_CONNECT_TIMEOUT_RTT 代表超时。
*/
- (float)testSpeedOf:(NSString *)ip port:(int16_t)port {
NSString *oldIp = ip;
Expand All @@ -105,10 +87,8 @@ - (float)testSpeedOf:(NSString *)ip port:(int16_t)port {
int s = 0;
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
// MARK: - 设置端口,这里需要根据需要自定义,默认是80端口。
saddr.sin_port = htons(port);
saddr.sin_addr.s_addr = inet_addr([ip UTF8String]);
//saddr.sin_addr.s_addr = inet_addr("1.1.1.123");
if( (s=socket(AF_INET, SOCK_STREAM, 0)) < 0) {
MSDKDNSLOG(@"ERROR:%s:%d, create socket failed.",__FUNCTION__,__LINE__);
return 0;
Expand All @@ -118,9 +98,6 @@ - (float)testSpeedOf:(NSString *)ip port:(int16_t)port {
//为了设置connect超时 把socket设置称为非阻塞
int flags = fcntl(s, F_GETFL,0);
fcntl(s,F_SETFL, flags | O_NONBLOCK);
//对于阻塞式套接字,调用connect函数将激发TCP的三次握手过程,而且仅在连接建立成功或者出错时才返回;
//对于非阻塞式套接字,如果调用connect函数会之间返回-1(表示出错),且错误为EINPROGRESS,表示连接建立,建立启动但是尚未完成;
//如果返回0,则表示连接已经建立,这通常是在服务器和客户在同一台主机上时发生。
int i = connect(s,(struct sockaddr*)&saddr, sizeof(saddr));
if(i == 0) {
//建立连接成功,返回rtt时间。 因为connect是非阻塞,所以这个时间就是一个函数执行的时间,毫秒级,没必要再测速了。
Expand All @@ -130,7 +107,7 @@ - (float)testSpeedOf:(NSString *)ip port:(int16_t)port {
struct timeval tv;
int valopt;
socklen_t lon;
tv.tv_sec = HTTPDNS_SOCKET_CONNECT_TIMEOUT;
tv.tv_sec = MSDKDns_SOCKET_CONNECT_TIMEOUT;
tv.tv_usec = 0;

fd_set myset;
Expand All @@ -148,7 +125,7 @@ - (float)testSpeedOf:(NSString *)ip port:(int16_t)port {

if (j == 0) {
MSDKDNSLOG(@"INFO:%s:%d, test rtt of (%@) timeout.",__FUNCTION__,__LINE__, oldIp);
rtt = HTTPDNS_SOCKET_CONNECT_TIMEOUT_RTT;
rtt = MSDKDns_SOCKET_CONNECT_TIMEOUT_RTT;
close(s);
return rtt;
}
Expand All @@ -159,20 +136,13 @@ - (float)testSpeedOf:(NSString *)ip port:(int16_t)port {
close(s);
return rtt;
}

/**
对于select和非阻塞connect,注意两点:
[1] 当连接成功建立时,描述符变成可写; [2] 当连接建立遇到错误时,描述符变为即可读,也可写,遇到这种情况,可调用getsockopt函数。
**/
lon = sizeof(int);
//valopt 表示错误信息。
// MARK: - 测试核心逻辑,连接后,获取错误信息,如果没有错误信息就是访问成功
/*!
* //getsockopt函数可获取影响套接字的选项,比如SOCKET的出错信息
* (get socket option)
*/
getsockopt(s, SOL_SOCKET, SO_ERROR, (void*)(&valopt), &lon);
//如果有错误信息:
if (valopt) {
MSDKDNSLOG(@"ERROR:%s:%d, select function error.",__FUNCTION__,__LINE__);
rtt = 0;
Expand Down

0 comments on commit 8beac8d

Please sign in to comment.