diff options
author | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-12 00:49:38 +0000 |
---|---|---|
committer | ericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-12 00:49:38 +0000 |
commit | 8a00f00ab5d68ffcc998fd04d2ca343af7cdf190 (patch) | |
tree | fd464ba49db4271c76c1cf8f769a22120ad631af /net/base/host_cache.h | |
parent | 77ae132c1bfdd986228b6f1c0d8c63baa441afdf (diff) | |
download | chromium_src-8a00f00ab5d68ffcc998fd04d2ca343af7cdf190.zip chromium_src-8a00f00ab5d68ffcc998fd04d2ca343af7cdf190.tar.gz chromium_src-8a00f00ab5d68ffcc998fd04d2ca343af7cdf190.tar.bz2 |
* Avoid doing concurrent DNS resolves of the same hostname in HostResolver.
* Add a 1 minute cache for host resolves.
* Refactor HostResolver to handle multiple requests.
* Make HostResolver a dependency of URLRequestContext. operate the HostResolver
in async mode for proxy resolver (bridging to IO thread).
TEST=unittests
BUG=13163
Review URL: http://codereview.chromium.org/118100
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18236 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/host_cache.h')
-rw-r--r-- | net/base/host_cache.h | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/net/base/host_cache.h b/net/base/host_cache.h new file mode 100644 index 0000000..2085b7c --- /dev/null +++ b/net/base/host_cache.h @@ -0,0 +1,93 @@ +// Copyright (c) 2009 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. + +#ifndef NET_BASE_HOST_CACHE_H_ +#define NET_BASE_HOST_CACHE_H_ + +#include <string> + +#include "base/hash_tables.h" +#include "base/ref_counted.h" +#include "base/time.h" +#include "net/base/address_list.h" +#include "testing/gtest/include/gtest/gtest_prod.h" + +namespace net { + +// Cache used by HostResolver to map hostnames to their resolved result. +// If the resolve is still in progress, the entry will reference the job +// responsible for populating it. +class HostCache { + public: + // Stores the latest address list that was looked up for a hostname. + struct Entry : public base::RefCounted<Entry> { + Entry(int error, const AddressList& addrlist, base::TimeTicks expiration); + ~Entry(); + + // The resolve results for this entry. + int error; + AddressList addrlist; + + // The time when this entry expires. + base::TimeTicks expiration; + }; + + // Constructs a HostCache whose entries are valid for |cache_duration_ms| + // milliseconds. The cache will store up to |max_entries|. + HostCache(size_t max_entries, size_t cache_duration_ms); + + ~HostCache(); + + // Returns a pointer to the entry for |hostname|, which is valid at time + // |now|. If there is no such entry, returns NULL. + const Entry* Lookup(const std::string& hostname, base::TimeTicks now) const; + + // Overwrites or creates an entry for |hostname|. 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. + Entry* Set(const std::string& hostname, + int error, + const AddressList addrlist, + base::TimeTicks now); + + // Returns true if this HostCache can contain no entries. + bool caching_is_disabled() const { + return max_entries_ == 0; + } + + // Returns the number of entries in the cache. + size_t size() const { + return entries_.size(); + } + + private: + FRIEND_TEST(HostCacheTest, Compact); + FRIEND_TEST(HostCacheTest, NoCache); + + typedef base::hash_map<std::string, scoped_refptr<Entry> > EntryMap; + + // Returns true if this cache entry's result is valid at time |now|. + static bool CanUseEntry(const Entry* entry, const base::TimeTicks now); + + // Prunes entries from the cache to bring it below max entry bound. Entries + // matching |pinned_entry| will NOT be pruned. + void Compact(base::TimeTicks now, const Entry* pinned_entry); + + // Bound on total size of the cache. + size_t max_entries_; + + // Time to live for cache entries in milliseconds. + size_t cache_duration_ms_; + + // Map from hostname (presumably in lowercase canonicalized format) to + // a resolved result entry. + EntryMap entries_; + + DISALLOW_COPY_AND_ASSIGN(HostCache); +}; + +} // namespace net + +#endif // NET_BASE_HOST_CACHE_H_ |