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

Side by Side Diff: net/dns/dns_session.cc

Issue 10878090: Keep pool of pre-connected DNS sockets (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move to socket buffers and callbacks for dependency injection Created 8 years, 3 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 #include "net/dns/dns_session.h" 5 #include "net/dns/dns_session.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/rand_util.h"
10 #include "base/stl_util.h"
9 #include "base/time.h" 11 #include "base/time.h"
10 #include "net/base/ip_endpoint.h" 12 #include "net/base/ip_endpoint.h"
13 #include "net/base/net_errors.h"
11 #include "net/dns/dns_config_service.h" 14 #include "net/dns/dns_config_service.h"
12 #include "net/socket/client_socket_factory.h" 15 #include "net/socket/client_socket_factory.h"
13 16
14 namespace net { 17 namespace net {
15 18
19 DnsSession::SocketLease::SocketLease(scoped_refptr<DnsSession> session,
20 int server_index,
21 scoped_ptr<DatagramClientSocket> socket)
22 : session_(session), server_index_(server_index), socket_(socket.Pass()) {}
23
24 DnsSession::SocketLease::~SocketLease() {
25 session_->FreeSocket(server_index_, socket_.Pass());
26 }
27
28 #if defined(OS_WIN)
29
30 // Don't request specific (random) ports on Windows, since that will trigger
31 // firewall prompts.
32 static const DatagramSocket::BindType kBindType = DatagramSocket::DEFAULT_BIND;
33 // Keep a buffer of the sockets the OS gives us to give some entropy in source
34 // ports.
35 static const size_t kMaxPoolSize = 256;
36 // TODO(ttuttle): I want 1024, but we get ERR_INSUFFICIENT_RESOURCES if we
37 // try to allocate that many.
38
39 #else
40
41 static const DatagramSocket::BindType kBindType = DatagramSocket::RANDOM_BIND;
42 static const size_t kMaxPoolSize = 0;
43
44 #endif
45
16 DnsSession::DnsSession(const DnsConfig& config, 46 DnsSession::DnsSession(const DnsConfig& config,
17 ClientSocketFactory* factory, 47 ClientSocketFactory* factory,
18 const RandIntCallback& rand_int_callback, 48 const RandIntCallback& rand_int_callback,
19 NetLog* net_log) 49 NetLog* net_log)
20 : config_(config), 50 : config_(config),
21 socket_factory_(factory), 51 socket_factory_(factory),
22 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), 52 rand_int_callback_(rand_int_callback),
23 net_log_(net_log), 53 net_log_(net_log),
54 pools_(0),
24 server_index_(0) { 55 server_index_(0) {
56 CreateDefaultPools(kMaxPoolSize);
57 }
58
59 DnsSession::DnsSession(const DnsConfig& config,
60 ClientSocketFactory* socket_factory,
61 const RandIntCallback& rand_int_callback,
62 int pool_size,
63 NetLog* net_log)
64 : config_(config),
65 socket_factory_(socket_factory),
66 rand_int_callback_(rand_int_callback),
67 net_log_(net_log),
68 pools_(0),
69 server_index_(0) {
70 CreateDefaultPools(pool_size);
71 }
72
73 DnsSession::DnsSession(const DnsConfig& config,
74 ClientSocketFactory* socket_factory,
75 const RandIntCallback& rand_int_callback,
76 const SocketPoolFactoryCallback& pool_factory_callback,
77 NetLog* net_log)
78 : config_(config),
79 socket_factory_(socket_factory),
80 rand_int_callback_(rand_int_callback),
81 net_log_(net_log),
82 pools_(0),
83 server_index_(0) {
84 CreatePools(pool_factory_callback);
85 }
86
87 void DnsSession::CreatePools(
88 const SocketPoolFactoryCallback& pool_factory_callback) {
89 CHECK(pools_.empty());
90
91 for (unsigned i = 0; i < config_.nameservers.size(); i++) {
92 EndPointSocketFactoryCallback socket_factory =
93 base::Bind(&DnsSession::CreateConnectedSocket,
94 this,
95 config_.nameservers[i]);
96 SocketPool* pool = pool_factory_callback.Run(socket_factory);
97 pools_.push_back(pool);
98 }
99 }
100
101 void DnsSession::CreateDefaultPools(int pool_size) {
102 CreatePools(base::Bind(SocketPoolImpl::CreateSocketPool,
103 pool_size,
104 rand_int_callback_));
105 }
106
107 DnsSession::~DnsSession() {
108 STLDeleteElements(&pools_);
25 } 109 }
26 110
27 int DnsSession::NextQueryId() const { 111 int DnsSession::NextQueryId() const {
28 return rand_callback_.Run(); 112 /* NB: This callback is also used for selecting sockets from the pool. */
szym 2012/09/10 19:31:00 I think the RNG used for selecting sockets from th
113 return rand_int_callback_.Run(0, kuint16max);
29 } 114 }
30 115
31 int DnsSession::NextFirstServerIndex() { 116 int DnsSession::NextFirstServerIndex() {
32 int index = server_index_; 117 int index = server_index_;
33 if (config_.rotate) 118 if (config_.rotate)
34 server_index_ = (server_index_ + 1) % config_.nameservers.size(); 119 server_index_ = (server_index_ + 1) % config_.nameservers.size();
35 return index; 120 return index;
36 } 121 }
37 122
38 base::TimeDelta DnsSession::NextTimeout(int attempt) { 123 base::TimeDelta DnsSession::NextTimeout(int attempt) {
39 // The timeout doubles every full round (each nameserver once). 124 // The timeout doubles every full round (each nameserver once).
40 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197 125 // TODO(szym): Adapt timeout to observed RTT. http://crbug.com/110197
41 return config_.timeout * (1 << (attempt / config_.nameservers.size())); 126 return config_.timeout * (1 << (attempt / config_.nameservers.size()));
42 } 127 }
43 128
44 DnsSession::~DnsSession() {} 129 DatagramClientSocket* DnsSession::CreateConnectedSocket(
130 const IPEndPoint& endpoint) {
131 DatagramClientSocket* socket;
132
133 socket = socket_factory_->CreateDatagramClientSocket(
134 kBindType,
135 base::Bind(&base::RandInt),
136 net_log_,
137 NetLog::Source());
138 if (!socket)
139 return NULL;
140
141 int rv = socket->Connect(endpoint);
142 if (rv != OK) {
143 delete socket;
144 return NULL;
145 }
146
147 return socket;
148 }
149
150 DnsSession::SocketPoolImpl::SocketPoolImpl(
151 unsigned pool_size,
152 const RandIntCallback& rand_int_callback,
153 const EndPointSocketFactoryCallback& factory_callback)
154 : pool_(0),
155 pool_size_(pool_size),
156 rand_int_callback_(rand_int_callback),
157 factory_callback_(factory_callback) {
158 FillPool();
159 }
160
161 DnsSession::SocketPoolImpl::~SocketPoolImpl() {
162 STLDeleteElements(&pool_);
163 }
164
165 // static
166 DnsSession::SocketPool* DnsSession::SocketPoolImpl::CreateSocketPool(
167 unsigned pool_size,
168 const RandIntCallback& rand_int_callback,
169 const EndPointSocketFactoryCallback& factory_callback) {
170 SocketPoolImpl* socket_pool = new SocketPoolImpl(pool_size,
171 rand_int_callback,
172 factory_callback);
173 return static_cast<SocketPool*>(socket_pool);
174 }
175
176 scoped_ptr<DatagramClientSocket> DnsSession::SocketPoolImpl::GetSocket() {
177 DatagramClientSocket* socket = NULL;
178
179 if (!pool_.empty()) {
180 unsigned index = rand_int_callback_.Run(0, pool_.size() - 1);
181 socket = pool_[index];
182 pool_[index] = pool_.back();
183 pool_.pop_back();
184 }
185
186 if (!socket)
187 socket = factory_callback_.Run();
188
189 return scoped_ptr<DatagramClientSocket>(socket);
190 }
191
192 void DnsSession::SocketPoolImpl::PutSocket(
193 scoped_ptr<DatagramClientSocket> socket) {
194 // Do nothing, so the socket is destroyed.
195 }
196
197 void DnsSession::SocketPoolImpl::FillPool() {
198 while (pool_.size() < pool_size_) {
199 DatagramClientSocket* socket = factory_callback_.Run();
200 if (!socket)
201 return;
202 pool_.push_back(socket);
203 }
204 }
205
206 // Allocate a socket, already connected to the server address.
207 scoped_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket(int index) {
208 CHECK(index >= 0);
szym 2012/09/10 19:31:00 Consider making |index| unsigned.
Deprecated (see juliatuttle) 2012/09/14 15:28:18 Done.
209 CHECK(index < (int)config_.nameservers.size());
210 DCHECK(pools_.size() == config_.nameservers.size());
211
212 scoped_ptr<DatagramClientSocket> socket(pools_[index]->GetSocket());
213
214 if (socket.get())
szym 2012/09/10 19:31:00 nit: needs braces.
215 return scoped_ptr<SocketLease>(new SocketLease(this, index, socket.Pass()));
216 else
217 return scoped_ptr<SocketLease>(NULL);
218 }
219
220 // Release a socket.
221 void DnsSession::FreeSocket(int index,
222 scoped_ptr<DatagramClientSocket> socket) {
223 CHECK(index >= 0);
224 CHECK(index < (int)config_.nameservers.size());
225 DCHECK(pools_.size() == config_.nameservers.size());
226
227 pools_[index]->PutSocket(socket.Pass());
228 }
229
45 230
46 } // namespace net 231 } // namespace net
47 232
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698