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

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

Issue 118100: Avoid doing concurrent DNS resolves of the same hostname (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Get compiling on mac Created 11 years, 6 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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_H_ 5 #ifndef NET_BASE_HOST_RESOLVER_H_
6 #define NET_BASE_HOST_RESOLVER_H_ 6 #define NET_BASE_HOST_RESOLVER_H_
7 7
8 #include <string> 8 #include <string>
9 #include <vector>
9 10
10 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/lock.h"
11 #include "base/ref_counted.h" 13 #include "base/ref_counted.h"
12 #include "net/base/completion_callback.h" 14 #include "net/base/completion_callback.h"
15 #include "net/base/host_cache.h"
16
17 class MessageLoop;
13 18
14 namespace net { 19 namespace net {
15 20
16 class AddressList; 21 class AddressList;
22 class HostMapper;
17 23
18 // This class represents the task of resolving a hostname (or IP address 24 // This class represents the task of resolving hostnames (or IP address
19 // literal) to an AddressList object. It can only resolve a single hostname at 25 // literal) to an AddressList object.
20 // a time, so if you need to resolve multiple hostnames at the same time, you
21 // will need to allocate a HostResolver object for each hostname.
22 // 26 //
23 // No attempt is made at this level to cache or pin resolution results. For 27 // HostResolver handles multiple requests at a time, so when cancelling a
24 // each request, this API talks directly to the underlying name resolver of 28 // request the Request* handle that was returned by Resolve() needs to be
25 // the local system, which may or may not result in a DNS query. The exact 29 // given. A simpler alternative for consumers that only have 1 outstanding
26 // behavior depends on the system configuration. 30 // request at a time is to create a SingleRequestHostResolver wrapper around
31 // HostResolver (which will automatically cancel the single request when it
32 // goes out of scope).
33 //
34 // For each hostname that is requested, HostResolver creates a
35 // HostResolver::Job. This job gets dispatched to a thread in the global
36 // WorkerPool, where it runs "getaddrinfo(hostname)". If requests for that same
37 // host are made while the job is already outstanding, then they are attached
38 // to the existing job rather than creating a new one. This avoids doing
39 // parallel resolves for the same host.
40 //
41 // The way these classes fit together is illustrated by:
42 //
43 //
44 // +------------- HostResolver ---------------+
45 // | | |
46 // Job Job Job
47 // (for host1) (for host2) (for hostX)
48 // / | | / | | / | |
49 // Request ... Request Request ... Request Request ... Request
50 // (port1) (port2) (port3) (port4) (port5) (portX)
51 //
52 //
53 // When a HostResolver::Job finishes its work in the threadpool, the callbacks
54 // of each waiting request are run on the origin thread.
55 //
56 // Thread safety: This class is not threadsafe, and must only be called
57 // from one thread!
27 // 58 //
28 class HostResolver { 59 class HostResolver {
29 public: 60 public:
30 HostResolver(); 61 // Creates a HostResolver that caches up to |max_cache_entries| for
62 // |cache_duration_ms| milliseconds.
63 //
64 // TODO(eroman): Get rid of the default parameters as it violate google
65 // style. This is temporary to help with refactoring.
66 HostResolver(int max_cache_entries = 100, int cache_duration_ms = 60000);
31 67
32 // If a completion callback is pending when the resolver is destroyed, the 68 // If any completion callbacks are pending when the resolver is destroyed,
33 // host resolution is cancelled, and the completion callback will not be 69 // the host resolutions are cancelled, and the completion callbacks will not
34 // called. 70 // be called.
35 ~HostResolver(); 71 ~HostResolver();
36 72
73 // Opaque type used to cancel a request.
74 class Request;
75
37 // Resolves the given hostname (or IP address literal), filling out the 76 // Resolves the given hostname (or IP address literal), filling out the
38 // |addresses| object upon success. The |port| parameter will be set as the 77 // |addresses| object upon success. The |port| parameter will be set as the
39 // sin(6)_port field of the sockaddr_in{6} struct. Returns OK if successful 78 // sin(6)_port field of the sockaddr_in{6} struct. Returns OK if successful
40 // or an error code upon failure. 79 // or an error code upon failure.
41 // 80 //
42 // When callback is null, the operation completes synchronously. 81 // When callback is null, the operation completes synchronously.
43 // 82 //
44 // When callback is non-null, the operation will be performed asynchronously. 83 // When callback is non-null, the operation will be performed asynchronously.
45 // ERR_IO_PENDING is returned if it has been scheduled successfully. Real 84 // ERR_IO_PENDING is returned if it has been scheduled successfully. Real
46 // result code will be passed to the completion callback. 85 // result code will be passed to the completion callback. If |req| is
86 // non-NULL, then |*req| will be filled with a handle to the async request.
87 // This handle is not valid after the request has completed.
88 int Resolve(const std::string& hostname, int port,
89 AddressList* addresses, CompletionCallback* callback,
90 Request** req);
91
92 // Cancels the specified request. |req| is the handle returned by Resolve().
93 // After a request is cancelled, its completion callback will not be called.
94 void CancelRequest(Request* req);
95
96 private:
97 class Job;
98 typedef std::vector<Request*> RequestsList;
99 typedef base::hash_map<std::string, scoped_refptr<Job> > JobMap;
100
101 // Adds a job to outstanding jobs list.
102 void AddOutstandingJob(Job* job);
103
104 // Returns the outstanding job for |hostname|, or NULL if there is none.
105 Job* FindOutstandingJob(const std::string& hostname);
106
107 // Removes |job| from the outstanding jobs list.
108 void RemoveOutstandingJob(Job* job);
109
110 // Callback for when |job| has completed with |error| and |addrlist|.
111 void OnJobComplete(Job* job, int error, const AddressList& addrlist);
112
113 // Cache of host resolution results.
114 HostCache cache_;
115
116 // Map from hostname to outstanding job.
117 JobMap jobs_;
118
119 // The job that OnJobComplete() is currently processing (needed in case
120 // HostResolver gets deleted from within the callback).
121 scoped_refptr<Job> cur_completing_job_;
122
123 DISALLOW_COPY_AND_ASSIGN(HostResolver);
124 };
125
126 // This class represents the task of resolving a hostname (or IP address
127 // literal) to an AddressList object. It wraps HostResolver to resolve only a
128 // single hostname at a time and cancels this request when going out of scope.
129 class SingleRequestHostResolver {
130 public:
131 explicit SingleRequestHostResolver(HostResolver* resolver);
132
133 // If a completion callback is pending when the resolver is destroyed, the
134 // host resolution is cancelled, and the completion callback will not be
135 // called.
136 ~SingleRequestHostResolver();
137
138 // Resolves the given hostname (or IP address literal), filling out the
139 // |addresses| object upon success. See HostResolver::Resolve() for details.
47 int Resolve(const std::string& hostname, int port, 140 int Resolve(const std::string& hostname, int port,
48 AddressList* addresses, CompletionCallback* callback); 141 AddressList* addresses, CompletionCallback* callback);
49 142
50 private: 143 private:
51 class Request; 144 // Callback for when the request to |resolver_| completes, so we dispatch
52 friend class Request; 145 // to the user's callback.
53 scoped_refptr<Request> request_; 146 void OnResolveCompletion(int result);
54 DISALLOW_COPY_AND_ASSIGN(HostResolver); 147
148 // The actual host resolver that will handle the request.
149 HostResolver* resolver_;
150
151 // The current request (if any).
152 HostResolver::Request* cur_request_;
153 CompletionCallback* cur_request_callback_;
154
155 // Completion callback for when request to |resolver_| completes.
156 net::CompletionCallbackImpl<SingleRequestHostResolver> callback_;
157
158 DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver);
55 }; 159 };
56 160
57 // A helper class used in unit tests to alter hostname mappings. See 161 // A helper class used in unit tests to alter hostname mappings. See
58 // SetHostMapper for details. 162 // SetHostMapper for details.
59 class HostMapper : public base::RefCountedThreadSafe<HostMapper> { 163 class HostMapper : public base::RefCountedThreadSafe<HostMapper> {
60 public: 164 public:
61 virtual ~HostMapper() {} 165 virtual ~HostMapper() {}
62 166
63 // Returns possibly altered hostname, or empty string to simulate 167 // Returns possibly altered hostname, or empty string to simulate
64 // a failed lookup. 168 // a failed lookup.
(...skipping 26 matching lines...) Expand all
91 // 195 //
92 // NOTE: In most cases, you should use ScopedHostMapper instead, which is 196 // NOTE: In most cases, you should use ScopedHostMapper instead, which is
93 // defined in host_resolver_unittest.h 197 // defined in host_resolver_unittest.h
94 // 198 //
95 HostMapper* SetHostMapper(HostMapper* host_mapper); 199 HostMapper* SetHostMapper(HostMapper* host_mapper);
96 #endif 200 #endif
97 201
98 } // namespace net 202 } // namespace net
99 203
100 #endif // NET_BASE_HOST_RESOLVER_H_ 204 #endif // NET_BASE_HOST_RESOLVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698