Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(90)

Side by Side Diff: net/dns/host_cache.h

Issue 1908543002: DNS: Retain stale entries in HostCache and return when requested (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make a few more changes (discussed IRL) Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/dns/dns_util.cc ('k') | net/dns/host_cache.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #include "net/dns/dns_util.h"
23 24
24 namespace net { 25 namespace net {
25 26
26 // Cache used by HostResolver to map hostnames to their resolved result. 27 // Cache used by HostResolver to map hostnames to their resolved result.
27 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) { 28 class NET_EXPORT HostCache : NON_EXPORTED_BASE(public base::NonThreadSafe) {
28 public: 29 public:
29 // Stores the latest address list that was looked up for a hostname.
30 struct NET_EXPORT Entry {
31 Entry(int error, const AddressList& addrlist, base::TimeDelta ttl);
32 // Use when |ttl| is unknown.
33 Entry(int error, const AddressList& addrlist);
34 ~Entry();
35
36 bool has_ttl() const { return ttl >= base::TimeDelta(); }
37
38 // The resolve results for this entry.
39 int error;
40 AddressList addrlist;
41 // TTL obtained from the nameserver. Negative if unknown.
42 base::TimeDelta ttl;
43 };
44
45 struct Key { 30 struct Key {
46 Key(const std::string& hostname, AddressFamily address_family, 31 Key(const std::string& hostname, AddressFamily address_family,
47 HostResolverFlags host_resolver_flags) 32 HostResolverFlags host_resolver_flags)
48 : hostname(hostname), 33 : hostname(hostname),
49 address_family(address_family), 34 address_family(address_family),
50 host_resolver_flags(host_resolver_flags) {} 35 host_resolver_flags(host_resolver_flags) {}
51 36
52 bool operator<(const Key& other) const { 37 bool operator<(const Key& other) const {
53 // The order of comparisons of |Key| fields is arbitrary, thus 38 // The order of comparisons of |Key| fields is arbitrary, thus
54 // |address_family| and |host_resolver_flags| are compared before 39 // |address_family| and |host_resolver_flags| are compared before
55 // |hostname| under assumption that integer comparisons are faster than 40 // |hostname| under assumption that integer comparisons are faster than
56 // string comparisons. 41 // string comparisons.
57 return std::tie(address_family, host_resolver_flags, hostname) < 42 return std::tie(address_family, host_resolver_flags, hostname) <
58 std::tie(other.address_family, other.host_resolver_flags, 43 std::tie(other.address_family, other.host_resolver_flags,
59 other.hostname); 44 other.hostname);
60 } 45 }
61 46
62 std::string hostname; 47 std::string hostname;
63 AddressFamily address_family; 48 AddressFamily address_family;
64 HostResolverFlags host_resolver_flags; 49 HostResolverFlags host_resolver_flags;
65 }; 50 };
66 51
67 struct EvictionHandler { 52 struct NET_EXPORT EntryStaleness {
68 void Handle(const Key& key, 53 // Time since the entry's TTL has expired. Negative if not expired.
69 const Entry& entry, 54 base::TimeDelta expired_by;
70 const base::TimeTicks& expiration, 55
71 const base::TimeTicks& now, 56 // Number of network changes since this result was cached.
72 bool onGet) const; 57 int network_changes;
58
59 // Number of hits to the cache entry while stale (expired or past-network).
60 int stale_hits;
61
62 bool is_stale() const {
63 return network_changes > 0 || expired_by >= base::TimeDelta();
64 }
73 }; 65 };
74 66
75 typedef ExpiringCache<Key, Entry, base::TimeTicks, 67 // Stores the latest address list that was looked up for a hostname.
76 std::less<base::TimeTicks>, 68 class NET_EXPORT Entry {
77 EvictionHandler> EntryMap; 69 public:
70 Entry(int error, const AddressList& addresses, base::TimeDelta ttl);
71 // Use when |ttl| is unknown.
72 Entry(int error, const AddressList& addresses);
73 ~Entry();
74
75 int error() const { return error_; }
76 const AddressList& addresses() const { return addresses_; }
77 bool has_ttl() const { return ttl_ >= base::TimeDelta(); }
78 base::TimeDelta ttl() const { return ttl_; }
79
80 base::TimeTicks expires() const { return expires_; }
81
82 // The resolve results for this entry.
83 int error_;
84 AddressList addresses_;
85 // TTL obtained from the nameserver. Negative if unknown.
86 base::TimeDelta ttl_;
Randy Smith (Not in Mondays) 2016/05/11 19:37:54 I thought you were going to turn these into access
87
88 private:
89 friend class HostCache;
90
91 Entry(const Entry& entry,
92 base::TimeTicks now,
93 base::TimeDelta ttl,
94 int network_changes);
95
96 int network_changes() const { return network_changes_; }
97 int total_hits() const { return total_hits_; }
98 int stale_hits() const { return stale_hits_; }
99
100 bool IsStale(base::TimeTicks now, int network_changes) const;
101 void CountHit(bool hit_is_stale);
102 void GetStaleness(base::TimeTicks now,
103 int network_changes,
104 EntryStaleness* out) const;
105
106 base::TimeTicks expires_;
107 // Copied from the cache's network_changes_ when the entry is set; can0
108 // later be compared to it to see if the entry was received on the current
109 // network.
110 int network_changes_;
111 int total_hits_;
112 int stale_hits_;
113 };
114
115 using EntryMap = std::map<Key, Entry>;
78 116
79 // Constructs a HostCache that stores up to |max_entries|. 117 // Constructs a HostCache that stores up to |max_entries|.
80 explicit HostCache(size_t max_entries); 118 explicit HostCache(size_t max_entries);
81 119
82 ~HostCache(); 120 ~HostCache();
83 121
84 // Returns a pointer to the entry for |key|, which is valid at time 122 // Returns a pointer to the entry for |key|, which is valid at time
85 // |now|. If there is no such entry, returns NULL. 123 // |now|. If there is no such entry, returns NULL.
86 const Entry* Lookup(const Key& key, base::TimeTicks now); 124 const Entry* Lookup(const Key& key, base::TimeTicks now);
87 125
126 // Returns a pointer to the entry for |key|, whether it is valid or stale at
127 // time |now|. Fills in |stale_out| with information about how stale it is.
128 // If there is no entry for |key| at all, returns NULL.
129 const Entry* LookupStale(const Key& key,
130 base::TimeTicks now,
131 EntryStaleness* stale_out);
132
88 // Overwrites or creates an entry for |key|. 133 // Overwrites or creates an entry for |key|.
89 // |entry| is the value to set, |now| is the current time 134 // |entry| is the value to set, |now| is the current time
90 // |ttl| is the "time to live". 135 // |ttl| is the "time to live".
91 void Set(const Key& key, 136 void Set(const Key& key,
92 const Entry& entry, 137 const Entry& entry,
93 base::TimeTicks now, 138 base::TimeTicks now,
94 base::TimeDelta ttl); 139 base::TimeDelta ttl);
95 140
141 // Marks all entries as stale on account of a network change.
142 void OnNetworkChange();
143
96 // Empties the cache 144 // Empties the cache
97 void clear(); 145 void clear();
98 146
99 // Returns the number of entries in the cache. 147 // Returns the number of entries in the cache.
100 size_t size() const; 148 size_t size() const;
101 149
102 // Following are used by net_internals UI. 150 // Following are used by net_internals UI.
103 size_t max_entries() const; 151 size_t max_entries() const;
104 152
105 const EntryMap& entries() const; 153 const EntryMap& entries() const { return entries_; }
106 154
107 // Creates a default cache. 155 // Creates a default cache.
108 static std::unique_ptr<HostCache> CreateDefaultCache(); 156 static std::unique_ptr<HostCache> CreateDefaultCache();
109 157
110 private: 158 private:
111 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache); 159 FRIEND_TEST_ALL_PREFIXES(HostCacheTest, NoCache);
112 160
161 enum SetOutcome : int;
162 enum LookupOutcome : int;
163 enum EraseReason : int;
164
165 Entry* LookupInternal(const Key& key);
166
167 void RecordSet(SetOutcome outcome,
168 base::TimeTicks now,
169 const Entry* old_entry,
170 const Entry& new_entry);
171 void RecordUpdateStale(AddressListDeltaType delta,
172 const EntryStaleness& stale);
173 void RecordLookup(LookupOutcome outcome,
174 base::TimeTicks now,
175 const Entry* entry);
176 void RecordErase(EraseReason reason, base::TimeTicks now, const Entry& entry);
177 void RecordEraseAll(EraseReason reason, base::TimeTicks now);
178
113 // Returns true if this HostCache can contain no entries. 179 // Returns true if this HostCache can contain no entries.
114 bool caching_is_disabled() const { 180 bool caching_is_disabled() const { return max_entries_ == 0; }
115 return entries_.max_entries() == 0; 181
116 } 182 void EvictOneEntry(base::TimeTicks now);
117 183
118 // Map from hostname (presumably in lowercase canonicalized format) to 184 // Map from hostname (presumably in lowercase canonicalized format) to
119 // a resolved result entry. 185 // a resolved result entry.
120 EntryMap entries_; 186 EntryMap entries_;
187 size_t max_entries_;
188 int network_changes_;
121 189
122 DISALLOW_COPY_AND_ASSIGN(HostCache); 190 DISALLOW_COPY_AND_ASSIGN(HostCache);
123 }; 191 };
124 192
125 } // namespace net 193 } // namespace net
126 194
127 #endif // NET_DNS_HOST_CACHE_H_ 195 #endif // NET_DNS_HOST_CACHE_H_
OLDNEW
« no previous file with comments | « net/dns/dns_util.cc ('k') | net/dns/host_cache.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698