golang version: 1.22.2
Build system: AlmaLinux release 9.3
When dialContext passes in the address is a domain name. For multiple ips of domain name resolution, I block the network of one ip by iptables drop strategy to simulate network failure.
Check the documentation. Knowing that dialcontext is passing in multiple ips for the domain name, it will divide the timeout set to each ip equally.
The dialcontext example I tested was passed the address www.baidu.com:443 and the timeout was 2 seconds. www.baidu.com resolves to 2 ips on my network. In theory, the timeout for each ip connection can be 1 second.
But in my repeated tests, I found three different results. The network is fast (maybe the first connection ip is not blocked by iptables) and the network is delayed (maybe the first connection is the ip blocked by iptables. The attempt to connect to the second normal ip succeeded). The first two are within my expectations. The third result is that the dialcontext returns a timeout. The result exceeded expectations.
The probability of the third structure is very high. This problem persisted even after the retry mechanism was added.
I traced the network calls with strace. connect calls port 9 instead of 443. I don’t know why.
In the third case, I catch the 443 packet with tcpdump and do not see the tcp syn handshake packet sent by the system.
connect(8, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("223.5.5.5")}, 16) = 0 <0.000027>
getsockname(8, {sa_family=AF_INET, sin_port=htons(60874), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000036>
getpeername(8, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("223.5.5.5")}, [112 => 16]) = 0 <0.000011>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000027>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000032>
connect(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.42")}, 16) = 0 <0.000021>
getsockname(8, {sa_family=AF_INET, sin_port=htons(46937), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000016>
getpeername(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.42")}, [112 => 16]) = 0 <0.000016>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000017>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000013>
connect(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.185")}, 16) = 0 <0.000013>
getsockname(8, {sa_family=AF_INET, sin_port=htons(60023), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000026>
getpeername(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.185")}, [112 => 16]) = 0 <0.000013>
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000031>
connect(8, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("183.2.172.42")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000043>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000044>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000062>
connect(8, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("223.5.5.5")}, 16) = 0 <0.000027>
getsockname(8, {sa_family=AF_INET, sin_port=htons(56527), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000011>
getpeername(8, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("223.5.5.5")}, [112 => 16]) = 0 <0.000008>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000054>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000049>
connect(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.42")}, 16) = 0 <0.000035>
getsockname(8, {sa_family=AF_INET, sin_port=htons(38185), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000012>
getpeername(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.42")}, [112 => 16]) = 0 <0.000028>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000188>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000026>
connect(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.185")}, 16) = 0 <0.000158>
getsockname(8, {sa_family=AF_INET, sin_port=htons(60410), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000055>
getpeername(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.185")}, [112 => 16]) = 0 <0.000019>
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000029>
connect(8, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("183.2.172.42")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000061>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000041>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000013>
connect(8, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("223.5.5.5")}, 16) = 0 <0.000052>
getsockname(8, {sa_family=AF_INET, sin_port=htons(49188), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000009>
getpeername(8, {sa_family=AF_INET, sin_port=htons(53), sin_addr=inet_addr("223.5.5.5")}, [112 => 16]) = 0 <0.000021>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000026>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000047>
connect(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.42")}, 16) = 0 <0.000028>
getsockname(8, {sa_family=AF_INET, sin_port=htons(37913), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000018>
getpeername(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.42")}, [112 => 16]) = 0 <0.000023>
socket(AF_INET, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000016>
setsockopt(8, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0 <0.000018>
connect(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.185")}, 16) = 0 <0.000018>
getsockname(8, {sa_family=AF_INET, sin_port=htons(48424), sin_addr=inet_addr("172.20.0.13")}, [112 => 16]) = 0 <0.000018>
getpeername(8, {sa_family=AF_INET, sin_port=htons(9), sin_addr=inet_addr("183.2.172.185")}, [112 => 16]) = 0 <0.000012>
socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 8 <0.000019>
connect(8, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("183.2.172.42")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000059>
--- SIGINT {si_signo=SIGINT, si_code=SI_KERNEL} ---