diff options
author | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-21 00:56:27 +0000 |
---|---|---|
committer | szym@chromium.org <szym@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-01-21 00:56:27 +0000 |
commit | a27308823dcdc002d05a355404dca725532e1697 (patch) | |
tree | 1c41eb271e7cc98c28cc3ee94d1825c7fcb98bc8 | |
parent | b3dd6cf5edadb959e8cda836aa2d7078cbfdce5c (diff) | |
download | chromium_src-a27308823dcdc002d05a355404dca725532e1697.zip chromium_src-a27308823dcdc002d05a355404dca725532e1697.tar.gz chromium_src-a27308823dcdc002d05a355404dca725532e1697.tar.bz2 |
Adds TTL argument to HostCache::Set.
Re-lands r118489 but with static initializers removed.
BUG=25472, 107880
TEST=net_unittests
R=mmenke
Review URL: http://codereview.chromium.org/9226035
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@118574 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/resources/net_internals/dns_view.html | 4 | ||||
-rw-r--r-- | chrome/browser/resources/net_internals/dns_view.js | 8 | ||||
-rw-r--r-- | chrome/browser/ui/webui/net_internals/net_internals_ui.cc | 6 | ||||
-rw-r--r-- | chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc | 6 | ||||
-rw-r--r-- | chrome/test/data/webui/net_internals/dns_view.js | 4 | ||||
-rw-r--r-- | net/base/host_cache.cc | 34 | ||||
-rw-r--r-- | net/base/host_cache.h | 25 | ||||
-rw-r--r-- | net/base/host_cache_unittest.cc | 111 | ||||
-rw-r--r-- | net/base/host_resolver_impl.cc | 16 | ||||
-rw-r--r-- | net/base/mock_host_resolver.cc | 18 | ||||
-rw-r--r-- | net/dns/async_host_resolver.cc | 23 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings.cc | 9 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_js_bindings_unittest.cc | 7 | ||||
-rw-r--r-- | net/proxy/proxy_resolver_v8.cc | 12 |
14 files changed, 138 insertions, 145 deletions
diff --git a/chrome/browser/resources/net_internals/dns_view.html b/chrome/browser/resources/net_internals/dns_view.html index fb569ce..70d9136 100644 --- a/chrome/browser/resources/net_internals/dns_view.html +++ b/chrome/browser/resources/net_internals/dns_view.html @@ -17,10 +17,6 @@ </h4> <ul> <li>Capacity: <span id=dns-view-cache-capacity></span></li> - <li>Time to live (ms) for success entries: - <span id=dns-view-cache-ttl-success></span></li> - <li>Time to live (ms) for failure entries: - <span id=dns-view-cache-ttl-failure></span></li> </ul> <h4> Current State diff --git a/chrome/browser/resources/net_internals/dns_view.js b/chrome/browser/resources/net_internals/dns_view.js index e5dfa92..93731e3 100644 --- a/chrome/browser/resources/net_internals/dns_view.js +++ b/chrome/browser/resources/net_internals/dns_view.js @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -71,8 +71,6 @@ var DnsView = (function() { // Clear the existing values. $(DnsView.DEFAULT_FAMILY_SPAN_ID).innerHTML = ''; $(DnsView.CAPACITY_SPAN_ID).innerHTML = ''; - $(DnsView.TTL_SUCCESS_SPAN_ID).innerHTML = ''; - $(DnsView.TTL_FAILURE_SPAN_ID).innerHTML = ''; $(DnsView.CACHE_TBODY_ID).innerHTML = ''; $(DnsView.ACTIVE_SPAN_ID).innerHTML = '0'; $(DnsView.EXPIRED_SPAN_ID).innerHTML = '0'; @@ -91,10 +89,6 @@ var DnsView = (function() { // Fill in the basic cache information. var hostResolverCache = hostResolverInfo.cache; $(DnsView.CAPACITY_SPAN_ID).innerText = hostResolverCache.capacity; - $(DnsView.TTL_SUCCESS_SPAN_ID).innerText = - hostResolverCache.ttl_success_ms; - $(DnsView.TTL_FAILURE_SPAN_ID).innerText = - hostResolverCache.ttl_failure_ms; var expiredEntries = 0; // Date the cache was logged. This will be either now, when actively diff --git a/chrome/browser/ui/webui/net_internals/net_internals_ui.cc b/chrome/browser/ui/webui/net_internals/net_internals_ui.cc index 2184be8..a730d91 100644 --- a/chrome/browser/ui/webui/net_internals/net_internals_ui.cc +++ b/chrome/browser/ui/webui/net_internals/net_internals_ui.cc @@ -923,12 +923,6 @@ void NetInternalsMessageHandler::IOThreadImpl::OnGetHostResolverInfo( cache_info_dict->SetInteger( "capacity", static_cast<int>(cache->max_entries())); - cache_info_dict->SetInteger( - "ttl_success_ms", - static_cast<int>(cache->success_entry_ttl().InMilliseconds())); - cache_info_dict->SetInteger( - "ttl_failure_ms", - static_cast<int>(cache->failure_entry_ttl().InMilliseconds())); ListValue* entry_list = new ListValue(); diff --git a/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc b/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc index 261d2c1..59bbbb2 100644 --- a/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc +++ b/chrome/browser/ui/webui/net_internals/net_internals_ui_browsertest.cc @@ -52,8 +52,7 @@ void AddCacheEntryOnIOThread(net::URLRequestContextGetter* context_getter, ASSERT_TRUE(cache); net::HostCache::Key key(hostname, net::ADDRESS_FAMILY_UNSPECIFIED, 0); - base::TimeTicks expires = - base::TimeTicks::Now() + base::TimeDelta::FromDays(expire_days_from_now); + base::TimeDelta ttl = base::TimeDelta::FromDays(expire_days_from_now); net::AddressList address_list; if (net_error == net::OK) { @@ -73,7 +72,8 @@ void AddCacheEntryOnIOThread(net::URLRequestContextGetter* context_getter, cache->Set(net::HostCache::Key(hostname, net::ADDRESS_FAMILY_UNSPECIFIED, 0), net_error, address_list, - expires); + base::TimeTicks::Now(), + ttl); } // Called on IO thread. Adds an entry to the list of known HTTP pipelining diff --git a/chrome/test/data/webui/net_internals/dns_view.js b/chrome/test/data/webui/net_internals/dns_view.js index 12fabb9..aae881e 100644 --- a/chrome/test/data/webui/net_internals/dns_view.js +++ b/chrome/test/data/webui/net_internals/dns_view.js @@ -22,10 +22,6 @@ function checkDisplay(hostResolverInfo) { expectEquals(hostResolverInfo.cache.capacity, parseInt($(DnsView.CAPACITY_SPAN_ID).innerText)); - expectEquals(hostResolverInfo.cache.ttl_success_ms, - parseInt($(DnsView.TTL_SUCCESS_SPAN_ID).innerText)); - expectEquals(hostResolverInfo.cache.ttl_failure_ms, - parseInt($(DnsView.TTL_FAILURE_SPAN_ID).innerText)); var entries = hostResolverInfo.cache.entries; diff --git a/net/base/host_cache.cc b/net/base/host_cache.cc index 03eb23a..731e5f5 100644 --- a/net/base/host_cache.cc +++ b/net/base/host_cache.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -22,12 +22,8 @@ HostCache::Entry::~Entry() { //----------------------------------------------------------------------------- -HostCache::HostCache(size_t max_entries, - base::TimeDelta success_entry_ttl, - base::TimeDelta failure_entry_ttl) - : max_entries_(max_entries), - success_entry_ttl_(success_entry_ttl), - failure_entry_ttl_(failure_entry_ttl) { +HostCache::HostCache(size_t max_entries) + : max_entries_(max_entries) { } HostCache::~HostCache() { @@ -53,13 +49,13 @@ const HostCache::Entry* HostCache::Lookup(const Key& key, HostCache::Entry* HostCache::Set(const Key& key, int error, const AddressList& addrlist, - base::TimeTicks now) { + base::TimeTicks now, + base::TimeDelta ttl) { DCHECK(CalledOnValidThread()); if (caching_is_disabled()) return NULL; - base::TimeTicks expiration = now + - (error == OK ? success_entry_ttl_ : failure_entry_ttl_); + base::TimeTicks expiration = now + ttl; scoped_refptr<Entry>& entry = entries_[key]; if (!entry) { @@ -96,16 +92,6 @@ size_t HostCache::max_entries() const { return max_entries_; } -base::TimeDelta HostCache::success_entry_ttl() const { - DCHECK(CalledOnValidThread()); - return success_entry_ttl_; -} - -base::TimeDelta HostCache::failure_entry_ttl() const { - DCHECK(CalledOnValidThread()); - return failure_entry_ttl_; -} - // Note that this map may contain expired entries. const HostCache::EntryMap& HostCache::entries() const { DCHECK(CalledOnValidThread()); @@ -120,13 +106,7 @@ bool HostCache::CanUseEntry(const Entry* entry, const base::TimeTicks now) { // static HostCache* HostCache::CreateDefaultCache() { static const size_t kMaxHostCacheEntries = 100; - - HostCache* cache = new HostCache( - kMaxHostCacheEntries, - base::TimeDelta::FromMinutes(1), - base::TimeDelta::FromSeconds(0)); // Disable caching of failed DNS. - - return cache; + return new HostCache(kMaxHostCacheEntries); } void HostCache::Compact(base::TimeTicks now, const Entry* pinned_entry) { diff --git a/net/base/host_cache.h b/net/base/host_cache.h index 62c07ab..d7c1590 100644 --- a/net/base/host_cache.h +++ b/net/base/host_cache.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -73,12 +73,8 @@ class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { typedef std::map<Key, scoped_refptr<Entry> > EntryMap; - // Constructs a HostCache that caches successful host resolves for - // |success_entry_ttl| time, and failed host resolves for - // |failure_entry_ttl|. The cache will store up to |max_entries|. - HostCache(size_t max_entries, - base::TimeDelta success_entry_ttl, - base::TimeDelta failure_entry_ttl); + // Constructs a HostCache that stores up to |max_entries|. + explicit HostCache(size_t max_entries); ~HostCache(); @@ -88,12 +84,13 @@ class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { // Overwrites or creates an entry for |key|. Returns the pointer to the // entry, or NULL on failure (fails if caching is disabled). - // (|error|, |addrlist|) is the value to set, and |now| is the current - // timestamp. + // (|error|, |addrlist|) is the value to set, |now| is the current time + // |ttl| is the "time to live". Entry* Set(const Key& key, int error, const AddressList& addrlist, - base::TimeTicks now); + base::TimeTicks now, + base::TimeDelta ttl); // Empties the cache void clear(); @@ -104,10 +101,6 @@ class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { // Following are used by net_internals UI. size_t max_entries() const; - base::TimeDelta success_entry_ttl() const; - - base::TimeDelta failure_entry_ttl() const; - // Note that this map may contain expired entries. const EntryMap& entries() const; @@ -133,10 +126,6 @@ class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { // Bound on total size of the cache. size_t max_entries_; - // Time to live for cache entries. - base::TimeDelta success_entry_ttl_; - base::TimeDelta failure_entry_ttl_; - // Map from hostname (presumably in lowercase canonicalized format) to // a resolved result entry. EntryMap entries_; diff --git a/net/base/host_cache_unittest.cc b/net/base/host_cache_unittest.cc index 4c99688..f00364d 100644 --- a/net/base/host_cache_unittest.cc +++ b/net/base/host_cache_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -14,10 +14,8 @@ namespace net { namespace { -const int kMaxCacheEntries = 10; -const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); -const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); +const int kMaxCacheEntries = 10; // Builds a key for |hostname|, defaulting the address family to unspecified. HostCache::Key Key(const std::string& hostname) { @@ -27,7 +25,9 @@ HostCache::Key Key(const std::string& hostname) { } // namespace TEST(HostCacheTest, Basic) { - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(kMaxCacheEntries); // Start at t=0. base::TimeTicks now; @@ -39,7 +39,7 @@ TEST(HostCacheTest, Basic) { // Add an entry for "foobar.com" at t=0. EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); - cache.Set(Key("foobar.com"), OK, AddressList(), now); + cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL); entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); EXPECT_FALSE(entry1 == NULL); EXPECT_EQ(1U, cache.size()); @@ -49,7 +49,7 @@ TEST(HostCacheTest, Basic) { // Add an entry for "foobar2.com" at t=5. EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL); - cache.Set(Key("foobar2.com"), OK, AddressList(), now); + cache.Set(Key("foobar2.com"), OK, AddressList(), now, kTTL); entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); EXPECT_FALSE(NULL == entry1); EXPECT_EQ(2U, cache.size()); @@ -68,7 +68,7 @@ TEST(HostCacheTest, Basic) { EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); // Update entry1, so it is no longer expired. - cache.Set(Key("foobar.com"), OK, AddressList(), now); + cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL); // Re-uses existing entry storage. EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); EXPECT_EQ(2U, cache.size()); @@ -84,16 +84,20 @@ TEST(HostCacheTest, Basic) { EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now) == NULL); } -// Try caching entries for a failed resolve attempt -- since we set -// the TTL of such entries to 0 it won't work. +// Try caching entries for a failed resolve attempt -- since we set the TTL of +// such entries to 0 it won't store, but it will kick out the previous result. TEST(HostCacheTest, NoCacheNegative) { - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); + const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); + + HostCache cache(kMaxCacheEntries); // Set t=0. base::TimeTicks now; EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), + now, kFailureEntryTTL); EXPECT_EQ(1U, cache.size()); // We disallow use of negative entries. @@ -101,17 +105,18 @@ TEST(HostCacheTest, NoCacheNegative) { // Now overwrite with a valid entry, and then overwrite with negative entry // again -- the valid entry should be kicked out. - cache.Set(Key("foobar.com"), OK, AddressList(), now); + cache.Set(Key("foobar.com"), OK, AddressList(), now, kSuccessEntryTTL); EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now) == NULL); - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), + now, kFailureEntryTTL); EXPECT_TRUE(cache.Lookup(Key("foobar.com"), now) == NULL); } // Try caching entries for a failed resolves for 10 seconds. TEST(HostCacheTest, CacheNegativeEntry) { - HostCache cache(kMaxCacheEntries, - base::TimeDelta::FromSeconds(0), // success entry TTL. - base::TimeDelta::FromSeconds(10)); // failure entry TTL. + const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(kMaxCacheEntries); // Start at t=0. base::TimeTicks now; @@ -123,7 +128,8 @@ TEST(HostCacheTest, CacheNegativeEntry) { // Add an entry for "foobar.com" at t=0. EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), + now, kFailureEntryTTL); entry1 = cache.Lookup(Key("foobar.com"), base::TimeTicks()); EXPECT_FALSE(entry1 == NULL); EXPECT_EQ(1U, cache.size()); @@ -133,7 +139,8 @@ TEST(HostCacheTest, CacheNegativeEntry) { // Add an entry for "foobar2.com" at t=5. EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), base::TimeTicks()) == NULL); - cache.Set(Key("foobar2.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); + cache.Set(Key("foobar2.com"), ERR_NAME_NOT_RESOLVED, AddressList(), + now, kFailureEntryTTL); entry2 = cache.Lookup(Key("foobar2.com"), base::TimeTicks()); EXPECT_FALSE(NULL == entry1); EXPECT_EQ(2U, cache.size()); @@ -152,7 +159,8 @@ TEST(HostCacheTest, CacheNegativeEntry) { EXPECT_EQ(entry2, cache.Lookup(Key("foobar2.com"), now)); // Update entry1, so it is no longer expired. - cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), now); + cache.Set(Key("foobar.com"), ERR_NAME_NOT_RESOLVED, AddressList(), + now, kFailureEntryTTL); // Re-uses existing entry storage. EXPECT_EQ(entry1, cache.Lookup(Key("foobar.com"), now)); EXPECT_EQ(2U, cache.size()); @@ -170,7 +178,9 @@ TEST(HostCacheTest, CacheNegativeEntry) { TEST(HostCacheTest, Compact) { // Initial entries limit is big enough to accomadate everything we add. - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); + const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(0); + HostCache cache(kMaxCacheEntries); EXPECT_EQ(0U, cache.size()); @@ -180,7 +190,7 @@ TEST(HostCacheTest, Compact) { // Add five valid entries at t=10. for (int i = 0; i < 5; ++i) { std::string hostname = base::StringPrintf("valid%d", i); - cache.Set(Key(hostname), OK, AddressList(), now); + cache.Set(Key(hostname), OK, AddressList(), now, kSuccessEntryTTL); } EXPECT_EQ(5U, cache.size()); @@ -188,14 +198,15 @@ TEST(HostCacheTest, Compact) { for (int i = 0; i < 3; ++i) { std::string hostname = base::StringPrintf("expired%d", i); base::TimeTicks t = now - base::TimeDelta::FromSeconds(10); - cache.Set(Key(hostname), OK, AddressList(), t); + cache.Set(Key(hostname), OK, AddressList(), t, kSuccessEntryTTL); } EXPECT_EQ(8U, cache.size()); // Add 2 negative entries at t=10 for (int i = 0; i < 2; ++i) { std::string hostname = base::StringPrintf("negative%d", i); - cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), now); + cache.Set(Key(hostname), ERR_NAME_NOT_RESOLVED, AddressList(), + now, kFailureEntryTTL); } EXPECT_EQ(10U, cache.size()); @@ -236,14 +247,16 @@ TEST(HostCacheTest, Compact) { // Add entries while the cache is at capacity, causing evictions. TEST(HostCacheTest, SetWithCompact) { - HostCache cache(3, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(3); // t=10 - base::TimeTicks now = base::TimeTicks() + kSuccessEntryTTL; + base::TimeTicks now = base::TimeTicks() + kTTL; - cache.Set(Key("host1"), OK, AddressList(), now); - cache.Set(Key("host2"), OK, AddressList(), now); - cache.Set(Key("expired"), OK, AddressList(), now - kSuccessEntryTTL); + cache.Set(Key("host1"), OK, AddressList(), now, kTTL); + cache.Set(Key("host2"), OK, AddressList(), now, kTTL); + cache.Set(Key("expired"), OK, AddressList(), now, kTTL - kTTL); EXPECT_EQ(3U, cache.size()); @@ -253,7 +266,7 @@ TEST(HostCacheTest, SetWithCompact) { EXPECT_TRUE(NULL == cache.Lookup(Key("expired"), now)); // Adding the fourth entry will cause "expired" to be evicted. - cache.Set(Key("host3"), OK, AddressList(), now); + cache.Set(Key("host3"), OK, AddressList(), now, kTTL); EXPECT_EQ(3U, cache.size()); EXPECT_TRUE(cache.Lookup(Key("expired"), now) == NULL); EXPECT_FALSE(cache.Lookup(Key("host1"), now) == NULL); @@ -262,9 +275,9 @@ TEST(HostCacheTest, SetWithCompact) { // Add two more entries. Something should be evicted, however "host5" // should definitely be in there (since it was last inserted). - cache.Set(Key("host4"), OK, AddressList(), now); + cache.Set(Key("host4"), OK, AddressList(), now, kTTL); EXPECT_EQ(3U, cache.size()); - cache.Set(Key("host5"), OK, AddressList(), now); + cache.Set(Key("host5"), OK, AddressList(), now, kTTL); EXPECT_EQ(3U, cache.size()); EXPECT_FALSE(cache.Lookup(Key("host5"), now) == NULL); } @@ -272,7 +285,9 @@ TEST(HostCacheTest, SetWithCompact) { // Tests that the same hostname can be duplicated in the cache, so long as // the address family differs. TEST(HostCacheTest, AddressFamilyIsPartOfKey) { - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(kMaxCacheEntries); // t=0. base::TimeTicks now; @@ -287,14 +302,14 @@ TEST(HostCacheTest, AddressFamilyIsPartOfKey) { // Add an entry for ("foobar.com", UNSPECIFIED) at t=0. EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); - cache.Set(key1, OK, AddressList(), now); + cache.Set(key1, OK, AddressList(), now, kSuccessEntryTTL); entry1 = cache.Lookup(key1, base::TimeTicks()); EXPECT_FALSE(entry1 == NULL); EXPECT_EQ(1U, cache.size()); // Add an entry for ("foobar.com", IPV4_ONLY) at t=0. EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); - cache.Set(key2, OK, AddressList(), now); + cache.Set(key2, OK, AddressList(), now, kSuccessEntryTTL); entry2 = cache.Lookup(key2, base::TimeTicks()); EXPECT_FALSE(entry2 == NULL); EXPECT_EQ(2U, cache.size()); @@ -307,7 +322,9 @@ TEST(HostCacheTest, AddressFamilyIsPartOfKey) { // Tests that the same hostname can be duplicated in the cache, so long as // the HostResolverFlags differ. TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(kMaxCacheEntries); // t=0. base::TimeTicks now; @@ -326,21 +343,21 @@ TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { // Add an entry for ("foobar.com", IPV4, NONE) at t=0. EXPECT_TRUE(cache.Lookup(key1, base::TimeTicks()) == NULL); - cache.Set(key1, OK, AddressList(), now); + cache.Set(key1, OK, AddressList(), now, kTTL); entry1 = cache.Lookup(key1, base::TimeTicks()); EXPECT_FALSE(entry1 == NULL); EXPECT_EQ(1U, cache.size()); // Add an entry for ("foobar.com", IPV4, CANONNAME) at t=0. EXPECT_TRUE(cache.Lookup(key2, base::TimeTicks()) == NULL); - cache.Set(key2, OK, AddressList(), now); + cache.Set(key2, OK, AddressList(), now, kTTL); entry2 = cache.Lookup(key2, base::TimeTicks()); EXPECT_FALSE(entry2 == NULL); EXPECT_EQ(2U, cache.size()); // Add an entry for ("foobar.com", IPV4, LOOPBACK_ONLY) at t=0. EXPECT_TRUE(cache.Lookup(key3, base::TimeTicks()) == NULL); - cache.Set(key3, OK, AddressList(), now); + cache.Set(key3, OK, AddressList(), now, kTTL); entry3 = cache.Lookup(key3, base::TimeTicks()); EXPECT_FALSE(entry3 == NULL); EXPECT_EQ(3U, cache.size()); @@ -354,7 +371,9 @@ TEST(HostCacheTest, HostResolverFlagsArePartOfKey) { TEST(HostCacheTest, NoCache) { // Disable caching. - HostCache cache(0, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(0); EXPECT_TRUE(cache.caching_is_disabled()); // Set t=0. @@ -362,14 +381,16 @@ TEST(HostCacheTest, NoCache) { // Lookup and Set should have no effect. EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); - cache.Set(Key("foobar.com"), OK, AddressList(), now); + cache.Set(Key("foobar.com"), OK, AddressList(), now, kTTL); EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); EXPECT_EQ(0U, cache.size()); } TEST(HostCacheTest, Clear) { - HostCache cache(kMaxCacheEntries, kSuccessEntryTTL, kFailureEntryTTL); + const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10); + + HostCache cache(kMaxCacheEntries); // Set t=0. base::TimeTicks now; @@ -377,9 +398,9 @@ TEST(HostCacheTest, Clear) { EXPECT_EQ(0u, cache.size()); // Add three entries. - cache.Set(Key("foobar1.com"), OK, AddressList(), now); - cache.Set(Key("foobar2.com"), OK, AddressList(), now); - cache.Set(Key("foobar3.com"), OK, AddressList(), now); + cache.Set(Key("foobar1.com"), OK, AddressList(), now, kTTL); + cache.Set(Key("foobar2.com"), OK, AddressList(), now, kTTL); + cache.Set(Key("foobar3.com"), OK, AddressList(), now, kTTL); EXPECT_EQ(3u, cache.size()); diff --git a/net/base/host_resolver_impl.cc b/net/base/host_resolver_impl.cc index 217f1e1..c5fc2e7 100644 --- a/net/base/host_resolver_impl.cc +++ b/net/base/host_resolver_impl.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -50,6 +50,9 @@ namespace { // some platform's resolvers. const size_t kMaxHostLength = 4096; +// Default TTL for successful resolutions with ProcTask. +const unsigned kCacheEntryTTLSeconds = 60; + // Helper to mutate the linked list contained by AddressList to the given // port. Note that in general this is dangerous since the AddressList's // data might be shared (and you should use AddressList::SetPort). @@ -1353,9 +1356,14 @@ void HostResolverImpl::OnJobComplete(Job* job, RemoveOutstandingJob(job); // Write result to the cache. - if (cache_.get()) - cache_->Set(job->key(), net_error, addrlist, base::TimeTicks::Now()); - + if (cache_.get()) { + base::TimeDelta ttl = base::TimeDelta::FromSeconds(0); + if (net_error == OK) + ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); + cache_->Set(job->key(), net_error, addrlist, + base::TimeTicks::Now(), + ttl); + } OnJobCompleteInternal(job, net_error, os_error, addrlist); } diff --git a/net/base/mock_host_resolver.cc b/net/base/mock_host_resolver.cc index 612c600..a41a0ce 100644 --- a/net/base/mock_host_resolver.cc +++ b/net/base/mock_host_resolver.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -22,6 +22,11 @@ namespace net { namespace { +// Cache size for the MockCachingHostResolver. +const unsigned kMaxCacheEntries = 100; +// TTL for the successful resolutions. Failures are not cached. +const unsigned kCacheEntryTTLSeconds = 60; + char* do_strdup(const char* src) { #if defined(OS_WIN) return _strdup(src); @@ -130,10 +135,7 @@ MockHostResolverBase::MockHostResolverBase(bool use_caching) proc_ = rules_; if (use_caching) { - cache_.reset(new HostCache( - 100, // max entries. - base::TimeDelta::FromMinutes(1), - base::TimeDelta::FromSeconds(0))); + cache_.reset(new HostCache(kMaxCacheEntries)); } } @@ -173,7 +175,11 @@ int MockHostResolverBase::ResolveProc(size_t id, HostCache::Key key(info.hostname(), info.address_family(), info.host_resolver_flags()); - cache_->Set(key, rv, addr, base::TimeTicks::Now()); + // Storing a failure with TTL 0 so that it overwrites previous value. + base::TimeDelta ttl; + if (rv == OK) + ttl = base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds); + cache_->Set(key, rv, addr, base::TimeTicks::Now(), ttl); } if (rv == OK) *addresses = CreateAddressListUsingPort(addr, info.port()); diff --git a/net/dns/async_host_resolver.cc b/net/dns/async_host_resolver.cc index 6f6552b..c759888 100644 --- a/net/dns/async_host_resolver.cc +++ b/net/dns/async_host_resolver.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -349,8 +349,9 @@ void AsyncHostResolver::OnDnsRequestComplete( RequestList& requests = rit->second; int port = requests.empty() ? 80 : requests.front()->info().port(); - // Extract AddressList out of DnsResponse. + // Extract AddressList and TTL out of DnsResponse. AddressList addr_list; + uint32 ttl = kuint32max; if (result == OK) { IPAddressList ip_addresses; DnsRecordParser parser = response->Parser(); @@ -362,6 +363,7 @@ void AsyncHostResolver::OnDnsRequestComplete( record.rdata.size() == kIPv6AddressSize)) { ip_addresses.push_back(IPAddressNumber(record.rdata.begin(), record.rdata.end())); + ttl = std::min(ttl, record.ttl); } } if (!ip_addresses.empty()) @@ -380,16 +382,21 @@ void AsyncHostResolver::OnDnsRequestComplete( // case |requests| would be empty. We are knowingly throwing away the // result of a DNS resolution in that case, because (a) if there are no // requests, we do not have info to obtain a key from, (b) DnsTransaction - // does not have info(), adding one into it just temporarily doesn't make - // sense, since HostCache will be replaced with RR cache soon. - // Also, we only cache positive results. All of this will change when RR - // cache is added. - if (result == OK && cache_.get() && !requests.empty()) { + // does not have info(). + // TODO(szym): Should DnsTransaction ignore HostResolverFlags or use defaults? + if ((result == OK || result == ERR_NAME_NOT_RESOLVED) && cache_.get() && + !requests.empty()) { Request* request = requests.front(); HostResolver::RequestInfo info = request->info(); HostCache::Key key( info.hostname(), info.address_family(), info.host_resolver_flags()); - cache_->Set(key, result, addr_list, base::TimeTicks::Now()); + // Store negative results with TTL 0 to flush out the old entry. + cache_->Set(key, + result, + addr_list, + base::TimeTicks::Now(), + (result == OK) ? base::TimeDelta::FromSeconds(ttl) + : base::TimeDelta()); } // Cleanup requests. diff --git a/net/proxy/proxy_resolver_js_bindings.cc b/net/proxy/proxy_resolver_js_bindings.cc index f3f6c15..74ac171 100644 --- a/net/proxy/proxy_resolver_js_bindings.cc +++ b/net/proxy/proxy_resolver_js_bindings.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -23,6 +23,10 @@ namespace net { namespace { +// TTL for the per-request DNS cache. Applies to both successful and failed +// DNS resolutions. +const unsigned kCacheEntryTTLSeconds = 5 * 60; + // Event parameters for a PAC error message (line number + message). class ErrorNetlogParams : public NetLog::EventParameters { public: @@ -263,7 +267,8 @@ class DefaultJSBindings : public ProxyResolverJSBindings { // Save the result back to the per-request DNS cache. if (host_cache) { host_cache->Set(cache_key, result, *address_list, - base::TimeTicks::Now()); + base::TimeTicks::Now(), + base::TimeDelta::FromSeconds(kCacheEntryTTLSeconds)); } return result; diff --git a/net/proxy/proxy_resolver_js_bindings_unittest.cc b/net/proxy/proxy_resolver_js_bindings_unittest.cc index 6ead6ef..dc346e0 100644 --- a/net/proxy/proxy_resolver_js_bindings_unittest.cc +++ b/net/proxy/proxy_resolver_js_bindings_unittest.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -235,9 +235,8 @@ TEST(ProxyResolverJSBindingsTest, PerRequestDNSCache) { // Now setup a per-request context, and try the same experiment -- we // expect the underlying host resolver to receive only 1 request this time, // since it will service the others from the per-request DNS cache. - HostCache cache(50, - base::TimeDelta::FromMinutes(10), - base::TimeDelta::FromMinutes(10)); + const unsigned kMaxCacheEntries = 50; + HostCache cache(kMaxCacheEntries); ProxyResolverRequestContext context(NULL, &cache); bindings->set_current_request_context(&context); diff --git a/net/proxy/proxy_resolver_v8.cc b/net/proxy/proxy_resolver_v8.cc index 92beb80..114bf0f 100644 --- a/net/proxy/proxy_resolver_v8.cc +++ b/net/proxy/proxy_resolver_v8.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -742,12 +742,10 @@ int ProxyResolverV8::GetProxyForURL( // available to any of the javascript "bindings" that are subsequently invoked // from the javascript. // - // In particular, we create a HostCache that is aggressive about caching - // failed DNS resolves. - HostCache host_cache( - 50, - base::TimeDelta::FromMinutes(5), - base::TimeDelta::FromMinutes(5)); + // In particular, we create a HostCache to aggressively cache failed DNS + // resolves. + const unsigned kMaxCacheEntries = 50; + HostCache host_cache(kMaxCacheEntries); ProxyResolverRequestContext request_context(&net_log, &host_cache); |