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

Side by Side Diff: net/base/host_resolver_impl.h

Issue 9369045: [net] HostResolverImpl + DnsTransaction + DnsConfigService = Asynchronous DNS ready for experiments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Ready for test-drive. Created 8 years, 10 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 | Annotate | Revision Log
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_BASE_HOST_RESOLVER_IMPL_H_ 5 #ifndef NET_BASE_HOST_RESOLVER_IMPL_H_
6 #define NET_BASE_HOST_RESOLVER_IMPL_H_ 6 #define NET_BASE_HOST_RESOLVER_IMPL_H_
7 #pragma once 7 #pragma once
8 8
9 #include <map> 9 #include <map>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/gtest_prod_util.h" 13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
16 #include "base/threading/non_thread_safe.h" 16 #include "base/threading/non_thread_safe.h"
17 #include "base/time.h" 17 #include "base/time.h"
18 #include "net/base/capturing_net_log.h" 18 #include "net/base/capturing_net_log.h"
19 #include "net/base/host_cache.h" 19 #include "net/base/host_cache.h"
20 #include "net/base/host_resolver.h" 20 #include "net/base/host_resolver.h"
21 #include "net/base/host_resolver_proc.h" 21 #include "net/base/host_resolver_proc.h"
22 #include "net/base/net_export.h" 22 #include "net/base/net_export.h"
23 #include "net/base/net_log.h" 23 #include "net/base/net_log.h"
24 #include "net/base/network_change_notifier.h" 24 #include "net/base/network_change_notifier.h"
25 #include "net/base/prioritized_dispatcher.h" 25 #include "net/base/prioritized_dispatcher.h"
26 #include "net/dns/dns_config_service.h"
26 27
27 namespace net { 28 namespace net {
28 29
30 class DnsTransactionFactory;
31
29 // For each hostname that is requested, HostResolver creates a 32 // For each hostname that is requested, HostResolver creates a
30 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcJob 33 // HostResolverImpl::Job. When this job gets dispatched it creates a ProcTask
31 // which runs the given HostResolverProc on a WorkerPool thread. If requests for 34 // which runs the given HostResolverProc on a WorkerPool thread. If requests for
32 // that same host are made during the job's lifetime, they are attached to the 35 // that same host are made during the job's lifetime, they are attached to the
33 // existing job rather than creating a new one. This avoids doing parallel 36 // existing job rather than creating a new one. This avoids doing parallel
34 // resolves for the same host. 37 // resolves for the same host.
35 // 38 //
36 // The way these classes fit together is illustrated by: 39 // The way these classes fit together is illustrated by:
37 // 40 //
38 // 41 //
39 // +----------- HostResolverImpl -------------+ 42 // +----------- HostResolverImpl -------------+
40 // | | | 43 // | | |
41 // Job Job Job 44 // Job Job Job
42 // (for host1, fam1) (for host2, fam2) (for hostx, famx) 45 // (for host1, fam1) (for host2, fam2) (for hostx, famx)
43 // / | | / | | / | | 46 // / | | / | | / | |
44 // Request ... Request Request ... Request Request ... Request 47 // Request ... Request Request ... Request Request ... Request
45 // (port1) (port2) (port3) (port4) (port5) (portX) 48 // (port1) (port2) (port3) (port4) (port5) (portX)
46 // 49 //
47 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request 50 // When a HostResolverImpl::Job finishes, the callbacks of each waiting request
48 // are run on the origin thread. 51 // are run on the origin thread.
49 // 52 //
50 // Thread safety: This class is not threadsafe, and must only be called 53 // Thread safety: This class is not threadsafe, and must only be called
51 // from one thread! 54 // from one thread!
52 // 55 //
53 // The HostResolverImpl enforces limits on the maximum number of concurrent 56 // The HostResolverImpl enforces limits on the maximum number of concurrent
54 // threads using PrioritizedDispatcher::Limits. 57 // threads using PrioritizedDispatcher::Limits.
55 // 58 //
56 // Jobs are ordered in the queue based on their priority and order of arrival. 59 // Jobs are ordered in the queue based on their priority and order of arrival.
57 // 60 //
61 // TODO(szym): Change DnsConfigService::Observer to Callback.
58 class NET_EXPORT HostResolverImpl 62 class NET_EXPORT HostResolverImpl
59 : public HostResolver, 63 : public HostResolver,
60 NON_EXPORTED_BASE(public base::NonThreadSafe), 64 NON_EXPORTED_BASE(public base::NonThreadSafe),
61 public NetworkChangeNotifier::IPAddressObserver, 65 public NetworkChangeNotifier::IPAddressObserver,
62 public NetworkChangeNotifier::DNSObserver, 66 public NetworkChangeNotifier::DNSObserver,
67 public DnsConfigService::Observer,
cbentzel 2012/02/10 19:51:08 NON_EXPORTED_BASE needed.
szym 2012/02/10 21:49:36 In the long run, I don't think we should be keepin
cbentzel 2012/02/10 22:18:49 Agree. But it doesn't compile without this on a co
szym 2012/02/11 00:01:37 Oh yes, absolutely, that is fixed in the next patc
63 public base::SupportsWeakPtr<HostResolverImpl> { 68 public base::SupportsWeakPtr<HostResolverImpl> {
64 public: 69 public:
65 // Parameters for ProcTask which resolves hostnames using HostResolveProc. 70 // Parameters for ProcTask which resolves hostnames using HostResolveProc.
66 // 71 //
67 // |resolver_proc| is used to perform the actual resolves; it must be 72 // |resolver_proc| is used to perform the actual resolves; it must be
68 // thread-safe since it is run from multiple worker threads. If 73 // thread-safe since it is run from multiple worker threads. If
69 // |resolver_proc| is NULL then the default host resolver procedure is 74 // |resolver_proc| is NULL then the default host resolver procedure is
70 // used (which is SystemHostResolverProc except if overridden). 75 // used (which is SystemHostResolverProc except if overridden).
71 // 76 //
72 // For each attempt, we could start another attempt if host is not resolved 77 // For each attempt, we could start another attempt if host is not resolved
(...skipping 25 matching lines...) Expand all
98 uint32 retry_factor; 103 uint32 retry_factor;
99 }; 104 };
100 105
101 // Creates a HostResolver that first uses the local cache |cache|, and then 106 // Creates a HostResolver that first uses the local cache |cache|, and then
102 // falls back to |proc_params.resolver_proc|. 107 // falls back to |proc_params.resolver_proc|.
103 // 108 //
104 // If |cache| is NULL, then no caching is used. Otherwise we take 109 // If |cache| is NULL, then no caching is used. Otherwise we take
105 // ownership of the |cache| pointer, and will free it during destruction. 110 // ownership of the |cache| pointer, and will free it during destruction.
106 // 111 //
107 // |job_limits| specifies the maximum number of jobs that the resolver will 112 // |job_limits| specifies the maximum number of jobs that the resolver will
108 // run at once (not counting potential duplicate attempts). 113 // run at once. This upper-bounds the total number of outstanding
114 // DNS transactions (not counting retransmissions and retries).
115 //
116 // |proc_job_limits| specifies the maximum number of calls to
117 // SystemHostResolverProc the resolver will make on WorkerPool (not counting
118 // retries).
119 //
120 // |dns_config_service| will be used to obtain DnsConfig for
121 // DnsTransactionFactory.
109 // 122 //
110 // |net_log| must remain valid for the life of the HostResolverImpl. 123 // |net_log| must remain valid for the life of the HostResolverImpl.
111 // TODO(szym): change to scoped_ptr<HostCache>. 124 // TODO(szym): change to scoped_ptr<HostCache>.
112 HostResolverImpl(HostCache* cache, 125 HostResolverImpl(HostCache* cache,
113 const PrioritizedDispatcher::Limits& job_limits, 126 const PrioritizedDispatcher::Limits& job_limits,
127 const PrioritizedDispatcher::Limits& proc_limits,
114 const ProcTaskParams& proc_params, 128 const ProcTaskParams& proc_params,
129 scoped_ptr<DnsConfigService> dns_config_service,
115 NetLog* net_log); 130 NetLog* net_log);
116 131
117 // If any completion callbacks are pending when the resolver is destroyed, 132 // If any completion callbacks are pending when the resolver is destroyed,
118 // the host resolutions are cancelled, and the completion callbacks will not 133 // the host resolutions are cancelled, and the completion callbacks will not
119 // be called. 134 // be called.
120 virtual ~HostResolverImpl(); 135 virtual ~HostResolverImpl();
121 136
122 // Configures maximum number of Jobs in the queue. Exposed for testing. 137 // Configures maximum number of Jobs in the queue. Exposed for testing.
123 // Only allowed when the queue is empty. 138 // Only allowed when the queue is empty.
124 void SetMaxQueuedJobs(size_t value); 139 void SetMaxQueuedJobs(size_t value);
(...skipping 10 matching lines...) Expand all
135 virtual void CancelRequest(RequestHandle req) OVERRIDE; 150 virtual void CancelRequest(RequestHandle req) OVERRIDE;
136 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; 151 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE;
137 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; 152 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE;
138 virtual void ProbeIPv6Support() OVERRIDE; 153 virtual void ProbeIPv6Support() OVERRIDE;
139 virtual HostCache* GetHostCache() OVERRIDE; 154 virtual HostCache* GetHostCache() OVERRIDE;
140 155
141 private: 156 private:
142 class Job; 157 class Job;
143 class ProcTask; 158 class ProcTask;
144 class IPv6ProbeJob; 159 class IPv6ProbeJob;
160 class DnsTask;
145 class Request; 161 class Request;
146 typedef HostCache::Key Key; 162 typedef HostCache::Key Key;
147 typedef std::map<Key, Job*> JobMap; 163 typedef std::map<Key, Job*> JobMap;
148 typedef std::vector<Request*> RequestsList; 164 typedef std::vector<Request*> RequestsList;
149 165
150 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP 166 // Helper used by |Resolve()| and |ResolveFromCache()|. Performs IP
151 // literal and cache lookup, returns OK if successful, 167 // literal and cache lookup, returns OK if successful,
152 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is 168 // ERR_NAME_NOT_RESOLVED if either hostname is invalid or IP literal is
153 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache. 169 // incompatible, ERR_DNS_CACHE_MISS if entry was not found in cache.
154 int ResolveHelper(const Key& key, 170 int ResolveHelper(const Key& key,
(...skipping 23 matching lines...) Expand all
178 // Callback from IPv6 probe activity. 194 // Callback from IPv6 probe activity.
179 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family); 195 void IPv6ProbeSetDefaultAddressFamily(AddressFamily address_family);
180 196
181 // Returns the (hostname, address_family) key to use for |info|, choosing an 197 // Returns the (hostname, address_family) key to use for |info|, choosing an
182 // "effective" address family by inheriting the resolver's default address 198 // "effective" address family by inheriting the resolver's default address
183 // family when the request leaves it unspecified. 199 // family when the request leaves it unspecified.
184 Key GetEffectiveKeyForRequest(const RequestInfo& info) const; 200 Key GetEffectiveKeyForRequest(const RequestInfo& info) const;
185 201
186 // Called by |job| when it has finished running. Records the result in cache 202 // Called by |job| when it has finished running. Records the result in cache
187 // if necessary and dispatches another job if possible. 203 // if necessary and dispatches another job if possible.
188 void OnJobFinished(Job* job, const AddressList& addrlist); 204 void OnJobFinished(Job* job,
205 const AddressList& addrlist,
cbentzel 2012/02/10 19:51:08 Should ttl be part of the AddressList?
szym 2012/02/10 21:49:36 An excellent question. Makes sense for HostCache a
206 base::TimeDelta ttl);
189 207
190 // Removes |job| from |jobs_|. 208 // Removes |job| from |jobs_|.
191 void RemoveJob(Job* job); 209 void RemoveJob(Job* job);
192 210
193 // Aborts all in progress jobs and notifies their requests. 211 // Aborts all in progress jobs and notifies their requests.
194 // Might start new jobs. 212 // Might start new jobs.
195 void AbortAllInProgressJobs(); 213 void AbortAllInProgressJobs();
196 214
197 // NetworkChangeNotifier::IPAddressObserver methods: 215 // NetworkChangeNotifier::IPAddressObserver interface.
cbentzel 2012/02/10 19:51:08 Nit: the examples on http://dev.chromium.org/devel
198 virtual void OnIPAddressChanged() OVERRIDE; 216 virtual void OnIPAddressChanged() OVERRIDE;
199 217
200 // NetworkChangeNotifier::OnDNSChanged methods: 218 // NetworkChangeNotifier::DNSObserver interface.
201 virtual void OnDNSChanged() OVERRIDE; 219 virtual void OnDNSChanged() OVERRIDE;
202 220
221 // DnsConfigService::Observer interface.
222 virtual void OnConfigChanged(const DnsConfig& dns_config) OVERRIDE;
223
203 // Cache of host resolution results. 224 // Cache of host resolution results.
204 scoped_ptr<HostCache> cache_; 225 scoped_ptr<HostCache> cache_;
205 226
206 // Map from HostCache::Key to a Job. 227 // Map from HostCache::Key to a Job.
207 JobMap jobs_; 228 JobMap jobs_;
208 229
209 // Starts Jobs according to their priority and the configured limits. 230 // Starts Jobs according to their priority and the configured limits.
210 PrioritizedDispatcher dispatcher_; 231 PrioritizedDispatcher dispatcher_;
211 232
233 // Starts ProcTask if DnsTask fails. Enforces tighter limits than
234 // |dispatcher_|.
235 PrioritizedDispatcher proc_dispatcher_;
236
212 // Limit on the maximum number of jobs queued in |dispatcher_|. 237 // Limit on the maximum number of jobs queued in |dispatcher_|.
213 size_t max_queued_jobs_; 238 size_t max_queued_jobs_;
214 239
215 // Parameters for ProcTask. 240 // Parameters for ProcTask.
216 ProcTaskParams proc_params_; 241 ProcTaskParams proc_params_;
217 242
243 scoped_ptr<DnsTransactionFactory> dns_transaction_factory_;
244
218 // Address family to use when the request doesn't specify one. 245 // Address family to use when the request doesn't specify one.
219 AddressFamily default_address_family_; 246 AddressFamily default_address_family_;
220 247
248 scoped_ptr<DnsConfigService> dns_config_service_;
249
221 // Indicate if probing is done after each network change event to set address 250 // Indicate if probing is done after each network change event to set address
222 // family. 251 // family.
223 // When false, explicit setting of address family is used. 252 // When false, explicit setting of address family is used.
224 bool ipv6_probe_monitoring_; 253 bool ipv6_probe_monitoring_;
225 254
226 // The last un-cancelled IPv6ProbeJob (if any). 255 // The last un-cancelled IPv6ProbeJob (if any).
227 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_; 256 scoped_refptr<IPv6ProbeJob> ipv6_probe_job_;
228 257
229 // Any resolver flags that should be added to a request by default. 258 // Any resolver flags that should be added to a request by default.
230 HostResolverFlags additional_resolver_flags_; 259 HostResolverFlags additional_resolver_flags_;
231 260
232 NetLog* net_log_; 261 NetLog* net_log_;
233 262
234 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl); 263 DISALLOW_COPY_AND_ASSIGN(HostResolverImpl);
235 }; 264 };
236 265
237 } // namespace net 266 } // namespace net
238 267
239 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_ 268 #endif // NET_BASE_HOST_RESOLVER_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698