diff options
author | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-03 20:58:52 +0000 |
---|---|---|
committer | evan@chromium.org <evan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-03 20:58:52 +0000 |
commit | 42339e9c36f2258b059129101063660cdbaa9e4d (patch) | |
tree | b53c159e3432b3efccbfd55350c775c2a3ea9cdf /net/base/host_resolver.cc | |
parent | 1074caef003be700aad1bde58b82c74c37f3af3b (diff) | |
download | chromium_src-42339e9c36f2258b059129101063660cdbaa9e4d.zip chromium_src-42339e9c36f2258b059129101063660cdbaa9e4d.tar.gz chromium_src-42339e9c36f2258b059129101063660cdbaa9e4d.tar.bz2 |
Use res_ninit for thread safety and a timer for reloading resolv.conf on Linux.
We will only reload resolv.conf once per second per thread when DNS lookups
fail. This should match the behaviour of mozilla.
BUG=12740
Review URL: http://codereview.chromium.org/118061
Patch from Craig Schlenter <craig.schlenter@gmail.com>.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17530 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/host_resolver.cc')
-rw-r--r-- | net/base/host_resolver.cc | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/net/base/host_resolver.cc b/net/base/host_resolver.cc index 23a1c00..f7f8668 100644 --- a/net/base/host_resolver.cc +++ b/net/base/host_resolver.cc @@ -21,6 +21,12 @@ #include "net/base/address_list.h" #include "net/base/net_errors.h" +#if defined(OS_LINUX) +#include "base/singleton.h" +#include "base/thread_local_storage.h" +#include "base/time.h" +#endif + #if defined(OS_WIN) #include "net/base/winsock_init.h" #endif @@ -40,6 +46,70 @@ HostMapper* SetHostMapper(HostMapper* value) { return value; } +#if defined(OS_LINUX) +// On Linux changes to /etc/resolv.conf can go unnoticed thus resulting in +// DNS queries failing either because nameservers are unknown on startup +// or because nameserver info has changed as a result of e.g. connecting to +// a new network. Some distributions patch glibc to stat /etc/resolv.conf +// to try to automatically detect such changes but these patches are not +// universal and even patched systems such as Jaunty appear to need calls +// to res_ninit to reload the nameserver information in different threads. +// +// We adopt the Mozilla solution here which is to call res_ninit when +// lookups fail and to rate limit the reloading to once per second per +// thread. + +// Keep a timer per calling thread to rate limit the calling of res_ninit. +class DnsReloadTimer { + public: + DnsReloadTimer() { + tls_index_.Initialize(SlotReturnFunction); + } + + ~DnsReloadTimer() { } + + // Check if the timer for the calling thread has expired. When no + // timer exists for the calling thread, create one. + bool Expired() { + const base::TimeDelta kRetryTime = base::TimeDelta::FromSeconds(1); + base::TimeTicks now = base::TimeTicks::Now(); + base::TimeTicks* timer_ptr = + static_cast<base::TimeTicks*>(tls_index_.Get()); + + if (!timer_ptr) { + timer_ptr = new base::TimeTicks(); + *timer_ptr = base::TimeTicks::Now(); + tls_index_.Set(timer_ptr); + // Return true to reload dns info on the first call for each thread. + return true; + } else if (now - *timer_ptr > kRetryTime) { + *timer_ptr = now; + return true; + } else { + return false; + } + } + + // Free the allocated timer. + static void SlotReturnFunction(void* data) { + base::TimeTicks* tls_data = static_cast<base::TimeTicks*>(data); + delete tls_data; + } + + private: + // We use thread local storage to identify which base::TimeTicks to + // interact with. + static ThreadLocalStorage::Slot tls_index_ ; + + DISALLOW_COPY_AND_ASSIGN(DnsReloadTimer); +}; + +// A TLS slot to the TimeTicks for the current thread. +// static +ThreadLocalStorage::Slot DnsReloadTimer::tls_index_(base::LINKER_INITIALIZED); + +#endif // defined(OS_LINUX) + static int HostResolverProc( const std::string& host, const std::string& port, struct addrinfo** out) { struct addrinfo hints = {0}; @@ -76,9 +146,10 @@ static int HostResolverProc( int err = getaddrinfo(host.c_str(), port.c_str(), &hints, out); #if defined(OS_LINUX) + net::DnsReloadTimer* dns_timer = Singleton<net::DnsReloadTimer>::get(); // If we fail, re-initialise the resolver just in case there have been any // changes to /etc/resolv.conf and retry. See http://crbug.com/11380 for info. - if (err && !res_init()) + if (err && dns_timer->Expired() && !res_ninit(&_res)) err = getaddrinfo(host.c_str(), port.c_str(), &hints, out); #endif |