mirror of
https://github.com/valyala/fasthttp.git
synced 2026-06-16 16:17:38 +03:00
add DisableDNSResolution for TCPDialer. Sometimes, users do not need to use DNS resolution because they have already determined that the requested address is a list of IP addresses. (#1702)
Co-authored-by: wangzhengkai.wzk <wangzhengkai.wzk@alibaba-inc.com>
This commit is contained in:
+15
-10
@@ -151,6 +151,8 @@ type TCPDialer struct {
|
||||
// }
|
||||
Resolver Resolver
|
||||
|
||||
// DisableDNSResolution may be used to disable DNS resolution
|
||||
DisableDNSResolution bool
|
||||
// DNSCacheDuration may be used to override the default DNS cache duration (DefaultDNSCacheDuration)
|
||||
DNSCacheDuration time.Duration
|
||||
|
||||
@@ -277,23 +279,26 @@ func (d *TCPDialer) dial(addr string, dualStack bool, timeout time.Duration) (ne
|
||||
d.DNSCacheDuration = DefaultDNSCacheDuration
|
||||
}
|
||||
|
||||
go d.tcpAddrsClean()
|
||||
if !d.DisableDNSResolution {
|
||||
go d.tcpAddrsClean()
|
||||
}
|
||||
})
|
||||
|
||||
deadline := time.Now().Add(timeout)
|
||||
addrs, idx, err := d.getTCPAddrs(addr, dualStack, deadline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
network := "tcp4"
|
||||
if dualStack {
|
||||
network = "tcp"
|
||||
}
|
||||
|
||||
if d.DisableDNSResolution {
|
||||
return d.tryDial(network, addr, deadline, d.concurrencyCh)
|
||||
}
|
||||
addrs, idx, err := d.getTCPAddrs(addr, dualStack, deadline)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var conn net.Conn
|
||||
n := uint32(len(addrs))
|
||||
for n > 0 {
|
||||
conn, err = d.tryDial(network, &addrs[idx%n], deadline, d.concurrencyCh)
|
||||
conn, err = d.tryDial(network, addrs[idx%n].String(), deadline, d.concurrencyCh)
|
||||
if err == nil {
|
||||
return conn, nil
|
||||
}
|
||||
@@ -307,7 +312,7 @@ func (d *TCPDialer) dial(addr string, dualStack bool, timeout time.Duration) (ne
|
||||
}
|
||||
|
||||
func (d *TCPDialer) tryDial(
|
||||
network string, addr *net.TCPAddr, deadline time.Time, concurrencyCh chan struct{},
|
||||
network string, addr string, deadline time.Time, concurrencyCh chan struct{},
|
||||
) (net.Conn, error) {
|
||||
timeout := time.Until(deadline)
|
||||
if timeout <= 0 {
|
||||
@@ -340,7 +345,7 @@ func (d *TCPDialer) tryDial(
|
||||
|
||||
ctx, cancelCtx := context.WithDeadline(context.Background(), deadline)
|
||||
defer cancelCtx()
|
||||
conn, err := dialer.DialContext(ctx, network, addr.String())
|
||||
conn, err := dialer.DialContext(ctx, network, addr)
|
||||
if err != nil && ctx.Err() == context.DeadlineExceeded {
|
||||
return nil, ErrDialTimeout
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user