Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCPDialer :: DNSCacheDuration option #1046

Merged
merged 5 commits into from
Jun 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions tcpdialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -153,6 +153,9 @@ type TCPDialer struct {
// }
Resolver Resolver

// DNSCacheDuration may be used to override the default DNS cache duration (DefaultDNSCacheDuration)
DNSCacheDuration time.Duration
eeertekin marked this conversation as resolved.
Show resolved Hide resolved

tcpAddrsLock sync.Mutex
tcpAddrsMap map[string]*tcpAddrEntry

Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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()
})
Expand Down Expand Up @@ -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()
Expand All @@ -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
}
Expand Down