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> |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 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 // Stores the latest address list that was looked up for a hostname. | 29 // Stores the latest address list that was looked up for a hostname. |
| 30 struct NET_EXPORT Entry { | 30 struct NET_EXPORT Entry { |
| 31 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); | 31 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl); |
| 32 // Use when |ttl| is unknown. | 32 // Use when |ttl| is unknown. |
| 33 Entry(int error, const AddressList& addrlist); | 33 Entry(int error, const AddressList& addrlist); |
| 34 | |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
The comments and code seem crowded together enough
Julia Tuttle
2016/04/29 17:33:52
Fixed.
| |
| 34 ~Entry(); | 35 ~Entry(); |
| 35 | 36 |
| 36 bool has_ttl() const { return ttl >= base::TimeDelta(); } | 37 bool has_ttl() const { return ttl >= base::TimeDelta(); } |
| 37 | 38 |
| 38 // The resolve results for this entry. | 39 // The resolve results for this entry. |
| 39 int error; | 40 int error; |
| 40 AddressList addrlist; | 41 AddressList addrlist; |
| 41 // TTL obtained from the nameserver. Negative if unknown. | 42 // TTL obtained from the nameserver. Negative if unknown. |
| 42 base::TimeDelta ttl; | 43 base::TimeDelta ttl; |
| 43 }; | 44 }; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 57 return std::tie(address_family, host_resolver_flags, hostname) < | 58 return std::tie(address_family, host_resolver_flags, hostname) < |
| 58 std::tie(other.address_family, other.host_resolver_flags, | 59 std::tie(other.address_family, other.host_resolver_flags, |
| 59 other.hostname); | 60 other.hostname); |
| 60 } | 61 } |
| 61 | 62 |
| 62 std::string hostname; | 63 std::string hostname; |
| 63 AddressFamily address_family; | 64 AddressFamily address_family; |
| 64 HostResolverFlags host_resolver_flags; | 65 HostResolverFlags host_resolver_flags; |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 struct EvictionHandler { | 68 struct NET_EXPORT StaleEntryInfo { |
| 68 void Handle(const Key& key, | 69 // Time since the entry's TTL has expired. Negative if not expired. |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
If it might not be expired, it's not really StaleE
Julia Tuttle
2016/04/29 17:33:52
EntryStaleness.
| |
| 69 const Entry& entry, | 70 base::TimeDelta expired_by; |
| 70 const base::TimeTicks& expiration, | 71 // Number of network changes since this result was cached, or 0u if none. |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
nit, suggestion: I find the comma following phrase
Julia Tuttle
2016/04/29 17:33:52
Removed.
| |
| 71 const base::TimeTicks& now, | 72 unsigned network_changes; |
| 72 bool onGet) const; | 73 // Number of hits to the cache entry while stale (expired or past-network). |
| 74 unsigned stale_hits; | |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
nit: The style guide forbids using built-in types
Julia Tuttle
2016/04/29 17:33:52
Done. (Does it not allow size_t either?)
Randy Smith (Not in Mondays)
2016/05/04 18:15:25
size_t isn't a built-in type :-}. But yes, size_t
| |
| 75 | |
| 76 bool is_stale() const { | |
| 77 return expired_by >= base::TimeDelta() || network_changes > 0; | |
|
Ryan Sleevi
2016/04/28 01:44:25
nit: network_changes > 0 first (to short-circuit t
Julia Tuttle
2016/04/29 17:33:52
Done.
| |
| 78 } | |
| 73 }; | 79 }; |
| 74 | 80 |
| 75 typedef ExpiringCache<Key, Entry, base::TimeTicks, | 81 // TODO: This should be private, but Histogram* functions in the .cc need to |
| 76 std::less<base::TimeTicks>, | 82 // poke at it. At least it's not NET_EXPORTed. |
|
Ryan Sleevi
2016/04/28 01:44:25
Is the TODO meaning that you're planning to addres
Julia Tuttle
2016/04/29 17:33:52
I'm removing the TODO.
I need the struct here bec
| |
| 77 EvictionHandler> EntryMap; | 83 struct EntryInternal : public Entry { |
| 84 EntryInternal(const Entry& entry, | |
| 85 base::TimeTicks now, | |
| 86 base::TimeDelta ttl, | |
| 87 unsigned network_changes); | |
| 88 | |
| 89 base::TimeTicks added; | |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
nit: Document meanings of member variables? The
Julia Tuttle
2016/04/29 17:33:52
Done; I think the others are self-evident.
| |
| 90 base::TimeTicks expires; | |
| 91 unsigned network_changes; | |
| 92 unsigned total_hits; | |
| 93 unsigned stale_hits; | |
| 94 | |
| 95 bool CheckStale(base::TimeTicks now, | |
| 96 unsigned network_changes, | |
| 97 StaleEntryInfo* out) const; | |
| 98 }; | |
| 99 | |
| 100 // Ditto. | |
| 101 typedef std::map<Key, EntryInternal> EntryMap; | |
| 78 | 102 |
| 79 // Constructs a HostCache that stores up to |max_entries|. | 103 // Constructs a HostCache that stores up to |max_entries|. |
| 80 explicit HostCache(size_t max_entries); | 104 explicit HostCache(size_t max_entries); |
| 81 | 105 |
| 82 ~HostCache(); | 106 ~HostCache(); |
| 83 | 107 |
| 84 // Returns a pointer to the entry for |key|, which is valid at time | 108 // Returns a pointer to the entry for |key|, which is valid at time |
| 85 // |now|. If there is no such entry, returns NULL. | 109 // |now|. If there is no such entry, returns NULL. |
| 86 const Entry* Lookup(const Key& key, base::TimeTicks now); | 110 const Entry* Lookup(const Key& key, base::TimeTicks now); |
| 87 | 111 |
| 112 const Entry* LookupStale(const Key& key, | |
|
Ryan Sleevi
2016/04/28 01:44:25
Document?
Julia Tuttle
2016/04/29 17:33:52
Done.
| |
| 113 base::TimeTicks now, | |
| 114 StaleEntryInfo* stale_out); | |
| 115 | |
| 88 // Overwrites or creates an entry for |key|. | 116 // Overwrites or creates an entry for |key|. |
| 89 // |entry| is the value to set, |now| is the current time | 117 // |entry| is the value to set, |now| is the current time |
| 90 // |ttl| is the "time to live". | 118 // |ttl| is the "time to live". |
| 91 void Set(const Key& key, | 119 void Set(const Key& key, |
| 92 const Entry& entry, | 120 const Entry& entry, |
| 93 base::TimeTicks now, | 121 base::TimeTicks now, |
| 94 base::TimeDelta ttl); | 122 base::TimeDelta ttl); |
| 95 | 123 |
| 124 // Marks all entries as stale on account of a network change. | |
| 125 // (Does not actually remove them from memory.) | |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
nit, suggestion: I think the fact that the interfa
Julia Tuttle
2016/04/29 17:33:52
Removed.
| |
| 126 void OnNetworkChange(); | |
| 127 | |
| 96 // Empties the cache | 128 // Empties the cache |
| 97 void clear(); | 129 void clear(); |
| 98 | 130 |
| 99 // Returns the number of entries in the cache. | 131 // Returns the number of entries in the cache. |
| 100 size_t size() const; | 132 size_t size() const; |
| 101 | 133 |
| 102 // Following are used by net_internals UI. | 134 // Following are used by net_internals UI. |
| 103 size_t max_entries() const; | 135 size_t max_entries() const; |
| 104 | 136 |
| 105 const EntryMap& entries() const; | 137 std::unique_ptr<base::Value> GetEntriesAsValue() const; |
|
Ryan Sleevi
2016/04/28 01:44:24
Document?
This feels a little weird because it's
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
I agree. I'm willing to accept this interface, bu
Julia Tuttle
2016/04/29 17:33:52
Would appending "ForNetLogOnly" do it?
I wanted t
Randy Smith (Not in Mondays)
2016/05/04 18:15:25
So there's a balancing act here. I'm ok with the
Randy Smith (Not in Mondays)
2016/05/05 20:41:51
Ping? (I asked what the private information you'r
| |
| 106 | 138 |
| 107 // Creates a default cache. | 139 // Creates a default cache. |
| 108 static std::unique_ptr<HostCache> CreateDefaultCache(); | 140 static std::unique_ptr<HostCache> CreateDefaultCache(); |
| 109 | 141 |
| 110 private: | 142 private: |
| 111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); | 143 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); |
| 112 | 144 |
| 113 // Returns true if this HostCache can contain no entries. | 145 // Returns true if this HostCache can contain no entries. |
| 114 bool caching_is_disabled() const { | 146 bool caching_is_disabled() const { return max_entries_ == 0; } |
| 115 return entries_.max_entries() == 0; | 147 |
| 116 } | 148 void Evict(base::TimeTicks now); |
|
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
Document? When I first encountered this, I presum
Julia Tuttle
2016/04/29 17:33:52
Renamed to EvictOneEntry.
| |
| 149 | |
| 150 EntryMap::iterator SelectEntryToEvict(); | |
|
Ryan Sleevi
2016/04/28 01:44:25
Document?
Randy Smith (Not in Mondays)
2016/04/28 16:24:16
Why does this exist? It's only called from Evict(
Julia Tuttle
2016/04/29 17:33:52
I was imagining a more complex algorithm, but ther
| |
| 117 | 151 |
| 118 // Map from hostname (presumably in lowercase canonicalized format) to | 152 // Map from hostname (presumably in lowercase canonicalized format) to |
| 119 // a resolved result entry. | 153 // a resolved result entry. |
| 120 EntryMap entries_; | 154 EntryMap entries_; |
| 155 size_t max_entries_; | |
| 156 unsigned network_changes_; | |
| 121 | 157 |
| 122 DISALLOW_COPY_AND_ASSIGN(HostCache); | 158 DISALLOW_COPY_AND_ASSIGN(HostCache); |
| 123 }; | 159 }; |
| 124 | 160 |
| 125 } // namespace net | 161 } // namespace net |
| 126 | 162 |
| 127 #endif // NET_DNS_HOST_CACHE_H_ | 163 #endif // NET_DNS_HOST_CACHE_H_ |
| OLD | NEW |