Chromium Code Reviews| 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 #ifndef NET_DNS_HOST_CACHE_H_ | 5 #ifndef NET_DNS_HOST_CACHE_H_ |
| 6 #define NET_DNS_HOST_CACHE_H_ | 6 #define NET_DNS_HOST_CACHE_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <functional> | 10 #include <functional> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <tuple> | 13 #include <tuple> |
| 14 | 14 |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
| 18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 19 #include "net/base/address_family.h" | 19 #include "net/base/address_family.h" |
| 20 #include "net/base/address_list.h" | 20 #include "net/base/address_list.h" |
| 21 #include "net/base/expiring_cache.h" | 21 #include "net/base/expiring_cache.h" |
| 22 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 | 25 |
| 26 // Cache used by HostResolver to map hostnames to their resolved result. | 26 // Cache used by HostResolver to map hostnames to their resolved result. |
| 27 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { | 27 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { |
| 28 public: | 28 public: |
| 29 struct NET_EXPORT EntryStaleness { | |
| 30 // Time since the entry's TTL has expired. Negative if not expired. | |
| 31 base::TimeDelta expired_by; | |
| 32 | |
| 33 // Number of network changes since this result was cached. | |
| 34 int network_changes; | |
| 35 | |
| 36 // Number of hits to the cache entry while stale (expired or past-network). | |
| 37 int stale_hits; | |
| 38 | |
| 39 bool is_stale() const { | |
| 40 return network_changes > 0 || expired_by >= base::TimeDelta(); | |
| 41 } | |
| 42 }; | |
| 43 | |
| 29 // Stores the latest address list that was looked up for a hostname. | 44 // Stores the latest address list that was looked up for a hostname. |
| 30 struct NET_EXPORT Entry { | 45 struct NET_EXPORT Entry { |
| 46 public: | |
| 31 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); | 47 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); |
| 32 // Use when |ttl| is unknown. | 48 // Use when |ttl| is unknown. |
| 33 Entry(int error, const AddressList& addrlist); | 49 Entry(int error, const AddressList& addrlist); |
| 34 ~Entry(); | 50 ~Entry(); |
| 35 | 51 |
| 36 bool has_ttl() const { return ttl >= base::TimeDelta(); } | 52 bool has_ttl() const { return ttl >= base::TimeDelta(); } |
| 37 | 53 |
| 38 // The resolve results for this entry. | 54 // The resolve results for this entry. |
| 39 int error; | 55 int error; |
| 40 AddressList addrlist; | 56 AddressList addrlist; |
| 41 // TTL obtained from the nameserver. Negative if unknown. | 57 // TTL obtained from the nameserver. Negative if unknown. |
| 42 base::TimeDelta ttl; | 58 base::TimeDelta ttl; |
|
Randy Smith (Not in Mondays)
2016/05/05 20:41:51
How would you feel about turning Entry into a clas
Julia Tuttle
2016/05/06 18:32:43
Done.
| |
| 59 | |
| 60 private: | |
| 61 friend class HostCache; | |
| 62 | |
| 63 Entry(const Entry& entry, | |
| 64 base::TimeTicks now, | |
| 65 base::TimeDelta ttl, | |
| 66 int network_changes); | |
| 67 | |
| 68 base::TimeTicks expires; | |
| 69 // Copied from the cache's network_changes_ when the entry is set; can | |
| 70 // later be compared to it to see if the entry was received on the current | |
| 71 // network. | |
| 72 int network_changes; | |
| 73 int total_hits; | |
| 74 int stale_hits; | |
| 75 | |
| 76 bool IsStale(base::TimeTicks now, int network_changes) const; | |
| 77 void CountHit(bool hit_is_stale); | |
| 78 void GetStaleness(base::TimeTicks now, | |
| 79 int network_changes, | |
| 80 EntryStaleness* out) const; | |
|
Randy Smith (Not in Mondays)
2016/05/05 20:41:51
I believe the style guide says that within each se
Julia Tuttle
2016/05/06 18:32:43
Done.
| |
| 43 }; | 81 }; |
| 44 | 82 |
| 45 struct Key { | 83 struct Key { |
| 46 Key(const std::string& hostname, AddressFamily address_family, | 84 Key(const std::string& hostname, AddressFamily address_family, |
| 47 HostResolverFlags host_resolver_flags) | 85 HostResolverFlags host_resolver_flags) |
| 48 : hostname(hostname), | 86 : hostname(hostname), |
| 49 address_family(address_family), | 87 address_family(address_family), |
| 50 host_resolver_flags(host_resolver_flags) {} | 88 host_resolver_flags(host_resolver_flags) {} |
| 51 | 89 |
| 52 bool operator<(const Key& other) const { | 90 bool operator<(const Key& other) const { |
| 53 // The order of comparisons of |Key| fields is arbitrary, thus | 91 // The order of comparisons of |Key| fields is arbitrary, thus |
| 54 // |address_family| and |host_resolver_flags| are compared before | 92 // |address_family| and |host_resolver_flags| are compared before |
| 55 // |hostname| under assumption that integer comparisons are faster than | 93 // |hostname| under assumption that integer comparisons are faster than |
| 56 // string comparisons. | 94 // string comparisons. |
| 57 return std::tie(address_family, host_resolver_flags, hostname) < | 95 return std::tie(address_family, host_resolver_flags, hostname) < |
| 58 std::tie(other.address_family, other.host_resolver_flags, | 96 std::tie(other.address_family, other.host_resolver_flags, |
| 59 other.hostname); | 97 other.hostname); |
| 60 } | 98 } |
| 61 | 99 |
| 62 std::string hostname; | 100 std::string hostname; |
| 63 AddressFamily address_family; | 101 AddressFamily address_family; |
| 64 HostResolverFlags host_resolver_flags; | 102 HostResolverFlags host_resolver_flags; |
| 65 }; | 103 }; |
| 66 | 104 |
| 67 struct EvictionHandler { | |
| 68 void Handle(const Key& key, | |
| 69 const Entry& entry, | |
| 70 const base::TimeTicks& expiration, | |
| 71 const base::TimeTicks& now, | |
| 72 bool onGet) const; | |
| 73 }; | |
| 74 | |
| 75 typedef ExpiringCache<Key, Entry, base::TimeTicks, | |
| 76 std::less<base::TimeTicks>, | |
| 77 EvictionHandler> EntryMap; | |
| 78 | |
| 79 // Constructs a HostCache that stores up to |max_entries|. | 105 // Constructs a HostCache that stores up to |max_entries|. |
| 80 explicit HostCache(size_t max_entries); | 106 explicit HostCache(size_t max_entries); |
| 81 | 107 |
| 82 ~HostCache(); | 108 ~HostCache(); |
| 83 | 109 |
| 84 // Returns a pointer to the entry for |key|, which is valid at time | 110 // Returns a pointer to the entry for |key|, which is valid at time |
| 85 // |now|. If there is no such entry, returns NULL. | 111 // |now|. If there is no such entry, returns NULL. |
| 86 const Entry* Lookup(const Key& key, base::TimeTicks now); | 112 const Entry* Lookup(const Key& key, base::TimeTicks now); |
| 87 | 113 |
| 114 // Returns a pointer to the entry for |key|, whether it is valid or stale at | |
| 115 // time |now|. Fills in |stale_out| with information about how stale it is. | |
| 116 // If there is no entry for |key| at all, returns NULL. | |
| 117 const Entry* LookupStale(const Key& key, | |
| 118 base::TimeTicks now, | |
| 119 EntryStaleness* stale_out); | |
| 120 | |
| 88 // Overwrites or creates an entry for |key|. | 121 // Overwrites or creates an entry for |key|. |
| 89 // |entry| is the value to set, |now| is the current time | 122 // |entry| is the value to set, |now| is the current time |
| 90 // |ttl| is the "time to live". | 123 // |ttl| is the "time to live". |
| 91 void Set(const Key& key, | 124 void Set(const Key& key, |
| 92 const Entry& entry, | 125 const Entry& entry, |
| 93 base::TimeTicks now, | 126 base::TimeTicks now, |
| 94 base::TimeDelta ttl); | 127 base::TimeDelta ttl); |
| 95 | 128 |
| 129 // Marks all entries as stale on account of a network change. | |
| 130 void OnNetworkChange(); | |
| 131 | |
| 96 // Empties the cache | 132 // Empties the cache |
| 97 void clear(); | 133 void clear(); |
| 98 | 134 |
| 99 // Returns the number of entries in the cache. | 135 // Returns the number of entries in the cache. |
| 100 size_t size() const; | 136 size_t size() const; |
| 101 | 137 |
| 102 // Following are used by net_internals UI. | 138 // Following are used by net_internals UI. |
| 103 size_t max_entries() const; | 139 size_t max_entries() const; |
| 104 | 140 |
| 105 const EntryMap& entries() const; | 141 std::unique_ptr<base::Value> GetEntriesAsValue() const; |
| 106 | 142 |
| 107 // Creates a default cache. | 143 // Creates a default cache. |
| 108 static std::unique_ptr<HostCache> CreateDefaultCache(); | 144 static std::unique_ptr<HostCache> CreateDefaultCache(); |
| 109 | 145 |
| 110 private: | 146 private: |
| 111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); | 147 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); |
| 112 | 148 |
| 149 typedef std::map<Key, Entry> EntryMap; | |
|
Randy Smith (Not in Mondays)
2016/05/05 20:41:51
I believe that "using EntryMap = std::map<Key, Ent
Julia Tuttle
2016/05/06 18:32:43
Done.
| |
| 150 enum SetOutcome : int; | |
| 151 enum LookupOutcome : int; | |
| 152 enum EraseReason : int; | |
| 153 | |
| 154 Entry* LookupInternal(const Key& key); | |
| 155 | |
| 156 void RecordSet(SetOutcome outcome, | |
| 157 base::TimeTicks now, | |
| 158 const Entry* old_entry, | |
| 159 const Entry& new_entry); | |
| 160 void RecordUpdateStale(const Entry& old_entry, | |
| 161 const Entry& new_entry, | |
| 162 base::TimeDelta expired_by, | |
| 163 int network_changes, | |
| 164 int stale_hits); | |
| 165 void RecordLookup(LookupOutcome outcome, | |
| 166 base::TimeTicks now, | |
| 167 const Entry* entry); | |
| 168 void RecordErase(EraseReason reason, base::TimeTicks now, const Entry& entry); | |
| 169 void RecordEraseAll(EraseReason reason, base::TimeTicks now); | |
| 170 | |
| 113 // Returns true if this HostCache can contain no entries. | 171 // Returns true if this HostCache can contain no entries. |
| 114 bool caching_is_disabled() const { | 172 bool caching_is_disabled() const { return max_entries_ == 0; } |
| 115 return entries_.max_entries() == 0; | 173 |
| 116 } | 174 void EvictOneEntry(base::TimeTicks now); |
| 117 | 175 |
| 118 // Map from hostname (presumably in lowercase canonicalized format) to | 176 // Map from hostname (presumably in lowercase canonicalized format) to |
| 119 // a resolved result entry. | 177 // a resolved result entry. |
| 120 EntryMap entries_; | 178 EntryMap entries_; |
| 179 size_t max_entries_; | |
| 180 int network_changes_; | |
| 121 | 181 |
| 122 DISALLOW_COPY_AND_ASSIGN(HostCache); | 182 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 123 }; | 183 }; |
| 124 | 184 |
| 125 } // namespace net | 185 } // namespace net |
| 126 | 186 |
| 127 #endif // NET_DNS_HOST_CACHE_H_ | 187 #endif // NET_DNS_HOST_CACHE_H_ |
| OLD | NEW |