diff options
author | Ertuğrul Emre Ertekin <ee@ertek.in> | 2021-06-18 14:37:07 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-18 13:37:07 +0200 |
commit | c12a06108b3a1d6b0a47d0635e66ebe0efc6ac9f (patch) | |
tree | 80f7d1f8c00f028fd8a47c3b428db8ad7e908aa1 /tcpdialer.go | |
parent | Run go test on github actions (#1047) (diff) | |
download | fasthttp-c12a06108b3a1d6b0a47d0635e66ebe0efc6ac9f.tar.gz fasthttp-c12a06108b3a1d6b0a47d0635e66ebe0efc6ac9f.tar.bz2 fasthttp-c12a06108b3a1d6b0a47d0635e66ebe0efc6ac9f.zip |
TCPDialer :: DNSCacheDuration option (#1046)
* DefaultDNSCacheDuration :: Changed to `var`
* Revert "DefaultDNSCacheDuration :: Changed to `var`"
This reverts commit d836eace9f709b5993d73830aa12d5a2f548ce0d.
* TCPDialer :: DNSCacheDuration option
* TCPDialer :: DNSCacheDuration option - comment added
* TCPDialer :: DNSCacheDuration option - comment fixed
Diffstat (limited to 'tcpdialer.go')
-rw-r--r-- | tcpdialer.go | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/tcpdialer.go b/tcpdialer.go index 0023cf6..2317aca 100644 --- a/tcpdialer.go +++ b/tcpdialer.go @@ -15,7 +15,7 @@ import ( // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -42,7 +42,7 @@ func Dial(addr string) (net.Conn, error) { // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -67,7 +67,7 @@ func DialTimeout(addr string, timeout time.Duration) (net.Conn, error) { // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -96,7 +96,7 @@ func DialDualStack(addr string) (net.Conn, error) { // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -153,6 +153,9 @@ type TCPDialer struct { // } Resolver Resolver + // DNSCacheDuration may be used to override the default DNS cache duration (DefaultDNSCacheDuration) + DNSCacheDuration time.Duration + tcpAddrsLock sync.Mutex tcpAddrsMap map[string]*tcpAddrEntry @@ -166,7 +169,7 @@ type TCPDialer struct { // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -193,7 +196,7 @@ func (d *TCPDialer) Dial(addr string) (net.Conn, error) { // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -218,7 +221,7 @@ func (d *TCPDialer) DialTimeout(addr string, timeout time.Duration) (net.Conn, e // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -247,7 +250,7 @@ func (d *TCPDialer) DialDualStack(addr string) (net.Conn, error) { // This function has the following additional features comparing to net.Dial: // // * It reduces load on DNS resolver by caching resolved TCP addressed -// for DefaultDNSCacheDuration. +// for DNSCacheDuration. // * It dials all the resolved TCP addresses in round-robin manner until // connection is established. This may be useful if certain addresses // are temporarily unreachable. @@ -272,6 +275,11 @@ func (d *TCPDialer) dial(addr string, dualStack bool, timeout time.Duration) (ne if d.Concurrency > 0 { d.concurrencyCh = make(chan struct{}, d.Concurrency) } + + if d.DNSCacheDuration == 0 { + d.DNSCacheDuration = DefaultDNSCacheDuration + } + d.tcpAddrsMap = make(map[string]*tcpAddrEntry) go d.tcpAddrsClean() }) @@ -361,7 +369,7 @@ type tcpAddrEntry struct { const DefaultDNSCacheDuration = time.Minute func (d *TCPDialer) tcpAddrsClean() { - expireDuration := 2 * DefaultDNSCacheDuration + expireDuration := 2 * d.DNSCacheDuration for { time.Sleep(time.Second) t := time.Now() @@ -379,7 +387,7 @@ func (d *TCPDialer) tcpAddrsClean() { func (d *TCPDialer) getTCPAddrs(addr string, dualStack bool) ([]net.TCPAddr, uint32, error) { d.tcpAddrsLock.Lock() e := d.tcpAddrsMap[addr] - if e != nil && !e.pending && time.Since(e.resolveTime) > DefaultDNSCacheDuration { + if e != nil && !e.pending && time.Since(e.resolveTime) > d.DNSCacheDuration { e.pending = true e = nil } |