OLD | NEW |
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/base/host_resolver_impl.h" | 5 #include "net/base/host_resolver_impl.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <Winsock2.h> | 8 #include <Winsock2.h> |
9 #elif defined(OS_POSIX) | 9 #elif defined(OS_POSIX) |
10 #include <netdb.h> | 10 #include <netdb.h> |
(...skipping 1641 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1652 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); | 1652 RecordTotalTime(HaveDnsConfig(), info.is_speculative(), base::TimeDelta()); |
1653 return rv; | 1653 return rv; |
1654 } | 1654 } |
1655 | 1655 |
1656 // Next we need to attach our request to a "job". This job is responsible for | 1656 // Next we need to attach our request to a "job". This job is responsible for |
1657 // calling "getaddrinfo(hostname)" on a worker thread. | 1657 // calling "getaddrinfo(hostname)" on a worker thread. |
1658 | 1658 |
1659 JobMap::iterator jobit = jobs_.find(key); | 1659 JobMap::iterator jobit = jobs_.find(key); |
1660 Job* job; | 1660 Job* job; |
1661 if (jobit == jobs_.end()) { | 1661 if (jobit == jobs_.end()) { |
| 1662 // If we couldn't find the desired address family, check to see if the |
| 1663 // other family is in the cache or another job, which indicates waste, |
| 1664 // and we should fix crbug.com/139811. |
| 1665 { |
| 1666 bool ipv4 = key.address_family == ADDRESS_FAMILY_IPV4; |
| 1667 Key other_family_key = key; |
| 1668 other_family_key.address_family = ipv4 ? |
| 1669 ADDRESS_FAMILY_UNSPECIFIED : ADDRESS_FAMILY_IPV4; |
| 1670 bool found_other_family_cache = false; |
| 1671 bool found_other_family_job = false; |
| 1672 if (default_address_family_ == ADDRESS_FAMILY_UNSPECIFIED) { |
| 1673 found_other_family_cache = cache_.get() && |
| 1674 cache_->Lookup(other_family_key, base::TimeTicks::Now()) != NULL; |
| 1675 if (!found_other_family_cache) |
| 1676 found_other_family_job = jobs_.count(other_family_key) > 0; |
| 1677 } |
| 1678 enum { // Used in UMA_HISTOGRAM_ENUMERATION. |
| 1679 AF_WASTE_IPV4_ONLY, |
| 1680 AF_WASTE_CACHE_IPV4, |
| 1681 AF_WASTE_CACHE_UNSPEC, |
| 1682 AF_WASTE_JOB_IPV4, |
| 1683 AF_WASTE_JOB_UNSPEC, |
| 1684 AF_WASTE_NONE_IPV4, |
| 1685 AF_WASTE_NONE_UNSPEC, |
| 1686 AF_WASTE_MAX, // Bounding value. |
| 1687 } category = AF_WASTE_MAX; |
| 1688 if (default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { |
| 1689 category = AF_WASTE_IPV4_ONLY; |
| 1690 } else if (found_other_family_cache) { |
| 1691 category = ipv4 ? AF_WASTE_CACHE_IPV4 : AF_WASTE_CACHE_UNSPEC; |
| 1692 } else if (found_other_family_job) { |
| 1693 category = ipv4 ? AF_WASTE_JOB_IPV4 : AF_WASTE_JOB_UNSPEC; |
| 1694 } else { |
| 1695 category = ipv4 ? AF_WASTE_NONE_IPV4 : AF_WASTE_NONE_UNSPEC; |
| 1696 } |
| 1697 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveUnspecWaste", category, |
| 1698 AF_WASTE_MAX); |
| 1699 } |
| 1700 |
1662 // Create new Job. | 1701 // Create new Job. |
1663 job = new Job(this, key, info.priority(), request_net_log); | 1702 job = new Job(this, key, info.priority(), request_net_log); |
1664 job->Schedule(); | 1703 job->Schedule(); |
1665 | 1704 |
1666 // Check for queue overflow. | 1705 // Check for queue overflow. |
1667 if (dispatcher_.num_queued_jobs() > max_queued_jobs_) { | 1706 if (dispatcher_.num_queued_jobs() > max_queued_jobs_) { |
1668 Job* evicted = static_cast<Job*>(dispatcher_.EvictOldestLowest()); | 1707 Job* evicted = static_cast<Job*>(dispatcher_.EvictOldestLowest()); |
1669 DCHECK(evicted); | 1708 DCHECK(evicted); |
1670 evicted->OnEvicted(); // Deletes |evicted|. | 1709 evicted->OnEvicted(); // Deletes |evicted|. |
1671 if (evicted == job) { | 1710 if (evicted == job) { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1813 | 1852 |
1814 bool HostResolverImpl::ServeFromCache(const Key& key, | 1853 bool HostResolverImpl::ServeFromCache(const Key& key, |
1815 const RequestInfo& info, | 1854 const RequestInfo& info, |
1816 int* net_error, | 1855 int* net_error, |
1817 AddressList* addresses) { | 1856 AddressList* addresses) { |
1818 DCHECK(addresses); | 1857 DCHECK(addresses); |
1819 DCHECK(net_error); | 1858 DCHECK(net_error); |
1820 if (!info.allow_cached_response() || !cache_.get()) | 1859 if (!info.allow_cached_response() || !cache_.get()) |
1821 return false; | 1860 return false; |
1822 | 1861 |
1823 base::TimeTicks current_time = base::TimeTicks::Now(); | 1862 const HostCache::Entry* cache_entry = cache_->Lookup( |
1824 const HostCache::Entry* cache_entry = cache_->Lookup(key, current_time); | 1863 key, base::TimeTicks::Now()); |
1825 | |
1826 { | |
1827 bool found = cache_entry != NULL; | |
1828 bool ipv4 = key.address_family == ADDRESS_FAMILY_IPV4; | |
1829 // If we couldn't find the desired address family, check to see if the | |
1830 // other family is in the cache, which indicates waste, and we should fix | |
1831 // crbug.com/139811. | |
1832 bool found_other_family = false; | |
1833 if (!found && default_address_family_ == ADDRESS_FAMILY_UNSPECIFIED) { | |
1834 Key other_family_key = key; | |
1835 other_family_key.address_family = ipv4 ? | |
1836 ADDRESS_FAMILY_UNSPECIFIED : ADDRESS_FAMILY_IPV4; | |
1837 found_other_family = | |
1838 cache_->Lookup(other_family_key, current_time) != NULL; | |
1839 } | |
1840 enum { // Used in HISTOGRAM_ENUMERATION. | |
1841 CACHE_IPV4_ONLY_FOUND, | |
1842 CACHE_IPV4_ONLY_MISS, | |
1843 CACHE_FOUND_IPV4, | |
1844 CACHE_FOUND_UNSPEC, | |
1845 CACHE_WASTE_IPV4, | |
1846 CACHE_WASTE_UNSPEC, | |
1847 CACHE_MISS_IPV4, | |
1848 CACHE_MISS_UNSPEC, | |
1849 CACHE_MAX, // Bounding value. | |
1850 } category = CACHE_MAX; | |
1851 if (default_address_family_ != ADDRESS_FAMILY_UNSPECIFIED) { | |
1852 category = found ? CACHE_IPV4_ONLY_FOUND : CACHE_IPV4_ONLY_MISS; | |
1853 } else if (found) { | |
1854 category = ipv4 ? CACHE_FOUND_IPV4 : CACHE_FOUND_UNSPEC; | |
1855 } else if (found_other_family) { | |
1856 category = ipv4 ? CACHE_WASTE_IPV4 : CACHE_WASTE_UNSPEC; | |
1857 } else { | |
1858 category = ipv4 ? CACHE_MISS_IPV4 : CACHE_MISS_UNSPEC; | |
1859 } | |
1860 UMA_HISTOGRAM_ENUMERATION("DNS.ResolveCacheCategory", category, CACHE_MAX); | |
1861 } | |
1862 | |
1863 if (!cache_entry) | 1864 if (!cache_entry) |
1864 return false; | 1865 return false; |
1865 | 1866 |
1866 *net_error = cache_entry->error; | 1867 *net_error = cache_entry->error; |
1867 if (*net_error == OK) { | 1868 if (*net_error == OK) { |
1868 *addresses = cache_entry->addrlist; | 1869 *addresses = cache_entry->addrlist; |
1869 EnsurePortOnAddressList(info.port(), addresses); | 1870 EnsurePortOnAddressList(info.port(), addresses); |
1870 } | 1871 } |
1871 return true; | 1872 return true; |
1872 } | 1873 } |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2051 // |this| may be deleted inside AbortAllInProgressJobs(). | 2052 // |this| may be deleted inside AbortAllInProgressJobs(). |
2052 if (self) | 2053 if (self) |
2053 TryServingAllJobsFromHosts(); | 2054 TryServingAllJobsFromHosts(); |
2054 } | 2055 } |
2055 | 2056 |
2056 bool HostResolverImpl::HaveDnsConfig() const { | 2057 bool HostResolverImpl::HaveDnsConfig() const { |
2057 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL); | 2058 return (dns_client_.get() != NULL) && (dns_client_->GetConfig() != NULL); |
2058 } | 2059 } |
2059 | 2060 |
2060 } // namespace net | 2061 } // namespace net |
OLD | NEW |