Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: net/dns/host_resolver_impl.cc

Issue 1903263002: DNS: Expose stale results through HostResolverImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@dns_stale1
Patch Set: rebase Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« net/dns/host_resolver_impl.h ('K') | « net/dns/host_resolver_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "net/dns/host_resolver_impl.h" 5 #include "net/dns/host_resolver_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 if (total_count_ == 0) 485 if (total_count_ == 0)
486 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_); 486 DCHECK_EQ(MINIMUM_PRIORITY, highest_priority_);
487 } 487 }
488 488
489 private: 489 private:
490 RequestPriority highest_priority_; 490 RequestPriority highest_priority_;
491 size_t total_count_; 491 size_t total_count_;
492 size_t counts_[NUM_PRIORITIES]; 492 size_t counts_[NUM_PRIORITIES];
493 }; 493 };
494 494
495 void MakeNotStale(HostCache::EntryStaleness* stale_info) {
496 if (!stale_info)
497 return;
498 stale_info->expired_by = base::TimeDelta::FromSeconds(-1);
499 stale_info->network_changes = 0;
500 stale_info->stale_hits = 0;
501 }
502
495 } // namespace 503 } // namespace
496 504
497 //----------------------------------------------------------------------------- 505 //-----------------------------------------------------------------------------
498 506
499 bool ResolveLocalHostname(base::StringPiece host, 507 bool ResolveLocalHostname(base::StringPiece host,
500 uint16_t port, 508 uint16_t port,
501 AddressList* address_list) { 509 AddressList* address_list) {
502 address_list->clear(); 510 address_list->clear();
503 511
504 bool is_local6; 512 bool is_local6;
(...skipping 1447 matching lines...) Expand 10 before | Expand all | Expand 10 after
1952 1960
1953 IPAddress ip_address; 1961 IPAddress ip_address;
1954 IPAddress* ip_address_ptr = nullptr; 1962 IPAddress* ip_address_ptr = nullptr;
1955 if (ip_address.AssignFromIPLiteral(info.hostname())) 1963 if (ip_address.AssignFromIPLiteral(info.hostname()))
1956 ip_address_ptr = &ip_address; 1964 ip_address_ptr = &ip_address;
1957 1965
1958 // Build a key that identifies the request in the cache and in the 1966 // Build a key that identifies the request in the cache and in the
1959 // outstanding jobs map. 1967 // outstanding jobs map.
1960 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); 1968 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log);
1961 1969
1962 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, source_net_log); 1970 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, nullptr,
1971 source_net_log);
1963 if (rv != ERR_DNS_CACHE_MISS) { 1972 if (rv != ERR_DNS_CACHE_MISS) {
1964 LogFinishRequest(source_net_log, info, rv); 1973 LogFinishRequest(source_net_log, info, rv);
1965 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); 1974 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta());
1966 return rv; 1975 return rv;
1967 } 1976 }
1968 1977
1969 // Next we need to attach our request to a "job". This job is responsible for 1978 // Next we need to attach our request to a "job". This job is responsible for
1970 // calling "getaddrinfo(hostname)" on a worker thread. 1979 // calling "getaddrinfo(hostname)" on a worker thread.
1971 1980
1972 JobMap::iterator jobit = jobs_.find(key); 1981 JobMap::iterator jobit = jobs_.find(key);
(...skipping 27 matching lines...) Expand all
2000 2009
2001 job->AddRequest(std::move(req)); 2010 job->AddRequest(std::move(req));
2002 // Completion happens during Job::CompleteRequests(). 2011 // Completion happens during Job::CompleteRequests().
2003 return ERR_IO_PENDING; 2012 return ERR_IO_PENDING;
2004 } 2013 }
2005 2014
2006 int HostResolverImpl::ResolveHelper(const Key& key, 2015 int HostResolverImpl::ResolveHelper(const Key& key,
2007 const RequestInfo& info, 2016 const RequestInfo& info,
2008 const IPAddress* ip_address, 2017 const IPAddress* ip_address,
2009 AddressList* addresses, 2018 AddressList* addresses,
2019 HostCache::EntryStaleness* stale_info,
2010 const BoundNetLog& source_net_log) { 2020 const BoundNetLog& source_net_log) {
2011 // The result of |getaddrinfo| for empty hosts is inconsistent across systems. 2021 // The result of |getaddrinfo| for empty hosts is inconsistent across systems.
2012 // On Windows it gives the default interface's address, whereas on Linux it 2022 // On Windows it gives the default interface's address, whereas on Linux it
2013 // gives an error. We will make it fail on all platforms for consistency. 2023 // gives an error. We will make it fail on all platforms for consistency.
2014 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) 2024 if (info.hostname().empty() || info.hostname().size() > kMaxHostLength) {
2025 MakeNotStale(stale_info);
2015 return ERR_NAME_NOT_RESOLVED; 2026 return ERR_NAME_NOT_RESOLVED;
2027 }
2016 2028
2017 int net_error = ERR_UNEXPECTED; 2029 int net_error = ERR_UNEXPECTED;
2018 if (ResolveAsIP(key, info, ip_address, &net_error, addresses)) 2030 if (ResolveAsIP(key, info, ip_address, &net_error, addresses)) {
2031 MakeNotStale(stale_info);
2019 return net_error; 2032 return net_error;
2020 if (ServeFromCache(key, info, &net_error, addresses)) { 2033 }
2034 if (ServeFromCache(key, info, &net_error, addresses, stale_info)) {
2021 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT); 2035 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_CACHE_HIT);
2036 // |ServeFromCache()| will set |*stale_info| as needed.
2022 return net_error; 2037 return net_error;
2023 } 2038 }
2024 // TODO(szym): Do not do this if nsswitch.conf instructs not to. 2039 // TODO(szym): Do not do this if nsswitch.conf instructs not to.
2025 // http://crbug.com/117655 2040 // http://crbug.com/117655
2026 if (ServeFromHosts(key, info, addresses)) { 2041 if (ServeFromHosts(key, info, addresses)) {
2027 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT); 2042 source_net_log.AddEvent(NetLog::TYPE_HOST_RESOLVER_IMPL_HOSTS_HIT);
2043 MakeNotStale(stale_info);
2028 return OK; 2044 return OK;
2029 } 2045 }
2030 2046
2031 if (ServeLocalhost(key, info, addresses)) 2047 if (ServeLocalhost(key, info, addresses)) {
2048 MakeNotStale(stale_info);
2032 return OK; 2049 return OK;
2050 }
2033 2051
2034 return ERR_DNS_CACHE_MISS; 2052 return ERR_DNS_CACHE_MISS;
2035 } 2053 }
2036 2054
2037 int HostResolverImpl::ResolveFromCache(const RequestInfo& info, 2055 int HostResolverImpl::ResolveFromCache(const RequestInfo& info,
2038 AddressList* addresses, 2056 AddressList* addresses,
2039 const BoundNetLog& source_net_log) { 2057 const BoundNetLog& source_net_log) {
2058 return ResolveStaleFromCache(info, addresses, nullptr, source_net_log);
2059 }
2060
2061 int HostResolverImpl::ResolveStaleFromCache(
2062 const RequestInfo& info,
2063 AddressList* addresses,
2064 HostCache::EntryStaleness* stale_info,
2065 const BoundNetLog& source_net_log) {
2040 DCHECK(CalledOnValidThread()); 2066 DCHECK(CalledOnValidThread());
2041 DCHECK(addresses); 2067 DCHECK(addresses);
2042 2068
2043 // Update the net log and notify registered observers. 2069 // Update the net log and notify registered observers.
2044 LogStartRequest(source_net_log, info); 2070 LogStartRequest(source_net_log, info);
2045 2071
2046 IPAddress ip_address; 2072 IPAddress ip_address;
2047 IPAddress* ip_address_ptr = nullptr; 2073 IPAddress* ip_address_ptr = nullptr;
2048 if (ip_address.AssignFromIPLiteral(info.hostname())) 2074 if (ip_address.AssignFromIPLiteral(info.hostname()))
2049 ip_address_ptr = &ip_address; 2075 ip_address_ptr = &ip_address;
2050 2076
2051 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log); 2077 Key key = GetEffectiveKeyForRequest(info, ip_address_ptr, source_net_log);
2052 2078
2053 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, source_net_log); 2079 int rv = ResolveHelper(key, info, ip_address_ptr, addresses, stale_info,
2080 source_net_log);
2054 LogFinishRequest(source_net_log, info, rv); 2081 LogFinishRequest(source_net_log, info, rv);
2055 return rv; 2082 return rv;
2056 } 2083 }
2057 2084
2058 void HostResolverImpl::ChangeRequestPriority(RequestHandle req_handle, 2085 void HostResolverImpl::ChangeRequestPriority(RequestHandle req_handle,
2059 RequestPriority priority) { 2086 RequestPriority priority) {
2060 DCHECK(CalledOnValidThread()); 2087 DCHECK(CalledOnValidThread());
2061 Request* req = reinterpret_cast<Request*>(req_handle); 2088 Request* req = reinterpret_cast<Request*>(req_handle);
2062 DCHECK(req); 2089 DCHECK(req);
2063 Job* job = req->job(); 2090 Job* job = req->job();
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
2123 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port()); 2150 *addresses = AddressList::CreateFromIPAddress(*ip_address, info.port());
2124 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME) 2151 if (key.host_resolver_flags & HOST_RESOLVER_CANONNAME)
2125 addresses->SetDefaultCanonicalName(); 2152 addresses->SetDefaultCanonicalName();
2126 } 2153 }
2127 return true; 2154 return true;
2128 } 2155 }
2129 2156
2130 bool HostResolverImpl::ServeFromCache(const Key& key, 2157 bool HostResolverImpl::ServeFromCache(const Key& key,
2131 const RequestInfo& info, 2158 const RequestInfo& info,
2132 int* net_error, 2159 int* net_error,
2133 AddressList* addresses) { 2160 AddressList* addresses,
2161 HostCache::EntryStaleness* stale_info) {
2134 DCHECK(addresses); 2162 DCHECK(addresses);
2135 DCHECK(net_error); 2163 DCHECK(net_error);
2136 if (!info.allow_cached_response() || !cache_.get()) 2164 if (!info.allow_cached_response() || !cache_.get())
2137 return false; 2165 return false;
2138 2166
2139 const HostCache::Entry* cache_entry = cache_->Lookup( 2167 const HostCache::Entry* cache_entry;
2140 key, base::TimeTicks::Now()); 2168 if (stale_info)
2169 cache_entry = cache_->LookupStale(key, base::TimeTicks::Now(), stale_info);
2170 else
2171 cache_entry = cache_->Lookup(key, base::TimeTicks::Now());
2141 if (!cache_entry) 2172 if (!cache_entry)
2142 return false; 2173 return false;
2143 2174
2144 *net_error = cache_entry->error(); 2175 *net_error = cache_entry->error();
2145 if (*net_error == OK) { 2176 if (*net_error == OK) {
2146 if (cache_entry->has_ttl()) 2177 if (cache_entry->has_ttl())
2147 RecordTTL(cache_entry->ttl()); 2178 RecordTTL(cache_entry->ttl());
2148 *addresses = EnsurePortOnAddressList(cache_entry->addresses(), info.port()); 2179 *addresses = EnsurePortOnAddressList(cache_entry->addresses(), info.port());
2149 } 2180 }
2150 return true; 2181 return true;
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after
2482 dns_client_->SetConfig(dns_config); 2513 dns_client_->SetConfig(dns_config);
2483 num_dns_failures_ = 0; 2514 num_dns_failures_ = 0;
2484 if (dns_client_->GetConfig()) 2515 if (dns_client_->GetConfig())
2485 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true); 2516 UMA_HISTOGRAM_BOOLEAN("AsyncDNS.DnsClientEnabled", true);
2486 } 2517 }
2487 2518
2488 AbortDnsTasks(); 2519 AbortDnsTasks();
2489 } 2520 }
2490 2521
2491 } // namespace net 2522 } // namespace net
OLDNEW
« net/dns/host_resolver_impl.h ('K') | « net/dns/host_resolver_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698