blob: 75081a21cd8079af6b84f82c318922d97a869bc1 [file] [log] [blame]
[email protected]ff579d42009-06-24 15:47:021// 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
3// found in the LICENSE file.
4
5#include "net/socket/client_socket_pool_base.h"
6
7#include "base/compiler_specific.h"
[email protected]fd4fe0b2010-02-08 23:02:158#include "base/format_macros.h"
[email protected]ff579d42009-06-24 15:47:029#include "base/message_loop.h"
10#include "base/stl_util-inl.h"
[email protected]fd4fe0b2010-02-08 23:02:1511#include "base/string_util.h"
[email protected]ff579d42009-06-24 15:47:0212#include "base/time.h"
[email protected]fd7b7c92009-08-20 19:38:3013#include "net/base/load_log.h"
[email protected]ff579d42009-06-24 15:47:0214#include "net/base/net_errors.h"
15#include "net/socket/client_socket_handle.h"
16
17using base::TimeDelta;
18
19namespace {
20
21// The timeout value, in seconds, used to clean up idle sockets that can't be
22// reused.
23//
24// Note: It's important to close idle sockets that have received data as soon
25// as possible because the received data may cause BSOD on Windows XP under
26// some conditions. See http://crbug.com/4606.
27const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
28
[email protected]60c4c412009-11-06 19:59:3629// The maximum size of the ConnectJob's LoadLog.
30const int kMaxNumLoadLogEntries = 50;
31
[email protected]ff579d42009-06-24 15:47:0232} // namespace
33
34namespace net {
35
[email protected]2ab05b52009-07-01 23:57:5836ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3437 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3038 Delegate* delegate,
39 LoadLog* load_log)
[email protected]2ab05b52009-07-01 23:57:5840 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3441 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5842 delegate_(delegate),
[email protected]fd7b7c92009-08-20 19:38:3043 load_log_(load_log) {
[email protected]2ab05b52009-07-01 23:57:5844 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5845 DCHECK(delegate);
46}
47
[email protected]fd7b7c92009-08-20 19:38:3048ConnectJob::~ConnectJob() {
49 if (delegate_) {
50 // If the delegate was not NULLed, then NotifyDelegateOfCompletion has
51 // not been called yet (hence we are cancelling).
52 LoadLog::AddEvent(load_log_, LoadLog::TYPE_CANCELLED);
53 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
54 }
55}
[email protected]2ab05b52009-07-01 23:57:5856
[email protected]974ebd62009-08-03 23:14:3457int ConnectJob::Connect() {
58 if (timeout_duration_ != base::TimeDelta())
59 timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3060
61 LoadLog::BeginEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
62
63 int rv = ConnectInternal();
64
65 if (rv != ERR_IO_PENDING) {
66 delegate_ = NULL;
67 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
68 }
69
70 return rv;
71}
72
73void ConnectJob::NotifyDelegateOfCompletion(int rv) {
74 // The delegate will delete |this|.
75 Delegate *delegate = delegate_;
76 delegate_ = NULL;
77
78 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
79
80 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:3481}
82
83void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:4084 // Make sure the socket is NULL before calling into |delegate|.
85 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:3086
87 LoadLog::AddEvent(load_log_,
88 LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
89
90 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:3491}
92
[email protected]d80a4322009-08-14 07:07:4993namespace internal {
94
[email protected]fd4fe0b2010-02-08 23:02:1595ClientSocketPoolBaseHelper::Request::Request(
96 ClientSocketHandle* handle,
97 CompletionCallback* callback,
98 RequestPriority priority,
99 LoadLog* load_log)
100 : handle_(handle), callback_(callback), priority_(priority),
101 load_log_(load_log) {}
102
103ClientSocketPoolBaseHelper::Request::~Request() {}
104
[email protected]d80a4322009-08-14 07:07:49105ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53106 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02107 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16108 base::TimeDelta unused_idle_socket_timeout,
109 base::TimeDelta used_idle_socket_timeout,
[email protected]100d5fb92009-12-21 21:08:35110 ConnectJobFactory* connect_job_factory,
[email protected]d13c3272010-02-04 00:24:51111 NetworkChangeNotifier* network_change_notifier)
[email protected]ff579d42009-06-24 15:47:02112 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53113 connecting_socket_count_(0),
114 handed_out_socket_count_(0),
115 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02116 max_sockets_per_group_(max_sockets_per_group),
[email protected]9bf28db2009-08-29 01:35:16117 unused_idle_socket_timeout_(unused_idle_socket_timeout),
118 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]211d2172009-07-22 15:48:53119 may_have_stalled_group_(false),
[email protected]100d5fb92009-12-21 21:08:35120 connect_job_factory_(connect_job_factory),
121 network_change_notifier_(network_change_notifier) {
[email protected]211d2172009-07-22 15:48:53122 DCHECK_LE(0, max_sockets_per_group);
123 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]100d5fb92009-12-21 21:08:35124
125 if (network_change_notifier_)
126 network_change_notifier_->AddObserver(this);
[email protected]211d2172009-07-22 15:48:53127}
[email protected]ff579d42009-06-24 15:47:02128
[email protected]d80a4322009-08-14 07:07:49129ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]4d3b05d2010-01-27 21:27:29130 CancelAllConnectJobs();
131
[email protected]ff579d42009-06-24 15:47:02132 // Clean up any idle sockets. Assert that we have no remaining active
133 // sockets or pending requests. They should have all been cleaned up prior
134 // to the manager being destroyed.
135 CloseIdleSockets();
136 DCHECK(group_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29137 DCHECK_EQ(0, connecting_socket_count_);
[email protected]100d5fb92009-12-21 21:08:35138
139 if (network_change_notifier_)
140 network_change_notifier_->RemoveObserver(this);
[email protected]ff579d42009-06-24 15:47:02141}
142
143// InsertRequestIntoQueue inserts the request into the queue based on
144// priority. Highest priorities are closest to the front. Older requests are
145// prioritized over requests of equal priority.
146//
147// static
[email protected]d80a4322009-08-14 07:07:49148void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
149 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02150 RequestQueue::iterator it = pending_requests->begin();
[email protected]ac790b42009-12-02 04:31:31151 while (it != pending_requests->end() && r->priority() >= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02152 ++it;
153 pending_requests->insert(it, r);
154}
155
[email protected]fd7b7c92009-08-20 19:38:30156// static
157const ClientSocketPoolBaseHelper::Request*
158ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
159 RequestQueue::iterator it, RequestQueue* pending_requests) {
160 const Request* req = *it;
[email protected]fd7b7c92009-08-20 19:38:30161 pending_requests->erase(it);
162 return req;
163}
164
[email protected]d80a4322009-08-14 07:07:49165int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02166 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49167 const Request* request) {
[email protected]fd4fe0b2010-02-08 23:02:15168 LoadLog::BeginEvent(request->load_log(), LoadLog::TYPE_SOCKET_POOL);
169 Group& group = group_map_[group_name];
170 int rv = RequestSocketInternal(group_name, request);
171 if (rv != ERR_IO_PENDING)
172 LoadLog::EndEvent(request->load_log(), LoadLog::TYPE_SOCKET_POOL);
173 else
174 InsertRequestIntoQueue(request, &group.pending_requests);
175 return rv;
176}
177
178int ClientSocketPoolBaseHelper::RequestSocketInternal(
179 const std::string& group_name,
180 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49181 DCHECK_GE(request->priority(), 0);
182 CompletionCallback* const callback = request->callback();
183 CHECK(callback);
184 ClientSocketHandle* const handle = request->handle();
185 CHECK(handle);
[email protected]ff579d42009-06-24 15:47:02186 Group& group = group_map_[group_name];
187
[email protected]ff579d42009-06-24 15:47:02188 // Can we make another active socket now?
[email protected]211d2172009-07-22 15:48:53189 if (ReachedMaxSocketsLimit() ||
190 !group.HasAvailableSocketSlot(max_sockets_per_group_)) {
191 if (ReachedMaxSocketsLimit()) {
192 // We could check if we really have a stalled group here, but it requires
193 // a scan of all groups, so just flip a flag here, and do the check later.
194 may_have_stalled_group_ = true;
[email protected]fd7b7c92009-08-20 19:38:30195
[email protected]fd4fe0b2010-02-08 23:02:15196 LoadLog::AddEvent(
197 request->load_log(),
[email protected]fd7b7c92009-08-20 19:38:30198 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
199 } else {
[email protected]fd4fe0b2010-02-08 23:02:15200 LoadLog::AddEvent(
201 request->load_log(),
[email protected]fd7b7c92009-08-20 19:38:30202 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]211d2172009-07-22 15:48:53203 }
[email protected]ff579d42009-06-24 15:47:02204 return ERR_IO_PENDING;
205 }
206
[email protected]5edbf8d2010-01-13 18:44:11207 // Try to reuse a socket.
[email protected]ff579d42009-06-24 15:47:02208 while (!group.idle_sockets.empty()) {
209 IdleSocket idle_socket = group.idle_sockets.back();
210 group.idle_sockets.pop_back();
211 DecrementIdleCount();
212 if (idle_socket.socket->IsConnectedAndIdle()) {
213 // We found one we can reuse!
[email protected]f9d285c2009-08-17 19:54:29214 base::TimeDelta idle_time =
215 base::TimeTicks::Now() - idle_socket.start_time;
[email protected]d80a4322009-08-14 07:07:49216 HandOutSocket(
[email protected]fd4fe0b2010-02-08 23:02:15217 idle_socket.socket, idle_socket.used, handle, idle_time, &group,
218 request->load_log());
[email protected]ff579d42009-06-24 15:47:02219 return OK;
220 }
221 delete idle_socket.socket;
222 }
223
[email protected]5edbf8d2010-01-13 18:44:11224 // See if we already have enough connect jobs or sockets that will be released
225 // soon.
[email protected]4d3b05d2010-01-27 21:27:29226 if (group.HasReleasingSockets()) {
[email protected]5edbf8d2010-01-13 18:44:11227 return ERR_IO_PENDING;
228 }
229
[email protected]ff579d42009-06-24 15:47:02230 // We couldn't find a socket to reuse, so allocate and connect a new one.
[email protected]4d3b05d2010-01-27 21:27:29231 scoped_refptr<LoadLog> job_load_log = new LoadLog(kMaxNumLoadLogEntries);
[email protected]fd7b7c92009-08-20 19:38:30232
[email protected]2ab05b52009-07-01 23:57:58233 scoped_ptr<ConnectJob> connect_job(
[email protected]fd7b7c92009-08-20 19:38:30234 connect_job_factory_->NewConnectJob(group_name, *request, this,
[email protected]5edbf8d2010-01-13 18:44:11235 job_load_log));
[email protected]ff579d42009-06-24 15:47:02236
[email protected]2ab05b52009-07-01 23:57:58237 int rv = connect_job->Connect();
[email protected]fd7b7c92009-08-20 19:38:30238
[email protected]4d3b05d2010-01-27 21:27:29239 if (rv != ERR_IO_PENDING && request->load_log())
[email protected]fd7b7c92009-08-20 19:38:30240 request->load_log()->Append(job_load_log);
241
[email protected]2ab05b52009-07-01 23:57:58242 if (rv == OK) {
243 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]fd4fe0b2010-02-08 23:02:15244 handle, base::TimeDelta(), &group, request->load_log());
[email protected]2ab05b52009-07-01 23:57:58245 } else if (rv == ERR_IO_PENDING) {
[email protected]211d2172009-07-22 15:48:53246 connecting_socket_count_++;
247
[email protected]5fc08e32009-07-15 17:09:57248 ConnectJob* job = connect_job.release();
[email protected]5fc08e32009-07-15 17:09:57249 group.jobs.insert(job);
[email protected]2b7523d2009-07-29 20:29:23250 } else if (group.IsEmpty()) {
251 group_map_.erase(group_name);
[email protected]2ab05b52009-07-01 23:57:58252 }
[email protected]ff579d42009-06-24 15:47:02253
[email protected]2ab05b52009-07-01 23:57:58254 return rv;
[email protected]ff579d42009-06-24 15:47:02255}
256
[email protected]d80a4322009-08-14 07:07:49257void ClientSocketPoolBaseHelper::CancelRequest(
258 const std::string& group_name, const ClientSocketHandle* handle) {
[email protected]ff579d42009-06-24 15:47:02259 CHECK(ContainsKey(group_map_, group_name));
260
261 Group& group = group_map_[group_name];
262
[email protected]ff579d42009-06-24 15:47:02263 // Search pending_requests for matching handle.
264 RequestQueue::iterator it = group.pending_requests.begin();
265 for (; it != group.pending_requests.end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49266 if ((*it)->handle() == handle) {
[email protected]fd7b7c92009-08-20 19:38:30267 const Request* req = RemoveRequestFromQueue(it, &group.pending_requests);
268 LoadLog::AddEvent(req->load_log(), LoadLog::TYPE_CANCELLED);
269 LoadLog::EndEvent(req->load_log(), LoadLog::TYPE_SOCKET_POOL);
270 delete req;
[email protected]4d3b05d2010-01-27 21:27:29271 if (group.jobs.size() > group.pending_requests.size() + 1) {
[email protected]974ebd62009-08-03 23:14:34272 // TODO(willchan): Cancel the job in the earliest LoadState.
[email protected]4d3b05d2010-01-27 21:27:29273 RemoveConnectJob(*group.jobs.begin(), &group);
[email protected]974ebd62009-08-03 23:14:34274 OnAvailableSocketSlot(group_name, &group);
275 }
[email protected]ff579d42009-06-24 15:47:02276 return;
277 }
278 }
[email protected]ff579d42009-06-24 15:47:02279}
280
[email protected]d80a4322009-08-14 07:07:49281void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
282 ClientSocket* socket) {
[email protected]5edbf8d2010-01-13 18:44:11283 Group& group = group_map_[group_name];
284 group.num_releasing_sockets++;
285 DCHECK_LE(group.num_releasing_sockets, group.active_socket_count);
[email protected]ff579d42009-06-24 15:47:02286 // Run this asynchronously to allow the caller to finish before we let
287 // another to begin doing work. This also avoids nasty recursion issues.
288 // NOTE: We cannot refer to the handle argument after this method returns.
289 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]d80a4322009-08-14 07:07:49290 this, &ClientSocketPoolBaseHelper::DoReleaseSocket, group_name, socket));
[email protected]ff579d42009-06-24 15:47:02291}
292
[email protected]d80a4322009-08-14 07:07:49293void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02294 CleanupIdleSockets(true);
295}
296
[email protected]d80a4322009-08-14 07:07:49297int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02298 const std::string& group_name) const {
299 GroupMap::const_iterator i = group_map_.find(group_name);
300 CHECK(i != group_map_.end());
301
302 return i->second.idle_sockets.size();
303}
304
[email protected]d80a4322009-08-14 07:07:49305LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02306 const std::string& group_name,
307 const ClientSocketHandle* handle) const {
308 if (!ContainsKey(group_map_, group_name)) {
309 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
310 << " for handle: " << handle;
311 return LOAD_STATE_IDLE;
312 }
313
314 // Can't use operator[] since it is non-const.
315 const Group& group = group_map_.find(group_name)->second;
316
[email protected]ff579d42009-06-24 15:47:02317 // Search pending_requests for matching handle.
318 RequestQueue::const_iterator it = group.pending_requests.begin();
[email protected]5fc08e32009-07-15 17:09:57319 for (size_t i = 0; it != group.pending_requests.end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49320 if ((*it)->handle() == handle) {
[email protected]4d3b05d2010-01-27 21:27:29321 if (i < group.jobs.size()) {
[email protected]5fc08e32009-07-15 17:09:57322 LoadState max_state = LOAD_STATE_IDLE;
323 for (ConnectJobSet::const_iterator job_it = group.jobs.begin();
324 job_it != group.jobs.end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21325 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57326 }
327 return max_state;
328 } else {
329 // TODO(wtc): Add a state for being on the wait list.
330 // See http://www.crbug.com/5077.
331 return LOAD_STATE_IDLE;
332 }
[email protected]ff579d42009-06-24 15:47:02333 }
334 }
335
336 NOTREACHED();
337 return LOAD_STATE_IDLE;
338}
339
[email protected]d80a4322009-08-14 07:07:49340bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16341 base::TimeTicks now,
342 base::TimeDelta timeout) const {
343 bool timed_out = (now - start_time) >= timeout;
[email protected]5fc08e32009-07-15 17:09:57344 return timed_out ||
345 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
[email protected]ff579d42009-06-24 15:47:02346}
347
[email protected]d80a4322009-08-14 07:07:49348void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02349 if (idle_socket_count_ == 0)
350 return;
351
352 // Current time value. Retrieving it once at the function start rather than
353 // inside the inner loop, since it shouldn't change by any meaningful amount.
354 base::TimeTicks now = base::TimeTicks::Now();
355
356 GroupMap::iterator i = group_map_.begin();
357 while (i != group_map_.end()) {
358 Group& group = i->second;
359
360 std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
361 while (j != group.idle_sockets.end()) {
[email protected]9bf28db2009-08-29 01:35:16362 base::TimeDelta timeout =
363 j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_;
364 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02365 delete j->socket;
366 j = group.idle_sockets.erase(j);
367 DecrementIdleCount();
368 } else {
369 ++j;
370 }
371 }
372
373 // Delete group if no longer needed.
[email protected]2ab05b52009-07-01 23:57:58374 if (group.IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02375 group_map_.erase(i++);
376 } else {
377 ++i;
378 }
379 }
380}
381
[email protected]d80a4322009-08-14 07:07:49382void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02383 if (++idle_socket_count_ == 1)
384 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
[email protected]d80a4322009-08-14 07:07:49385 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
[email protected]ff579d42009-06-24 15:47:02386}
387
[email protected]d80a4322009-08-14 07:07:49388void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02389 if (--idle_socket_count_ == 0)
390 timer_.Stop();
391}
392
[email protected]d80a4322009-08-14 07:07:49393void ClientSocketPoolBaseHelper::DoReleaseSocket(const std::string& group_name,
394 ClientSocket* socket) {
[email protected]ff579d42009-06-24 15:47:02395 GroupMap::iterator i = group_map_.find(group_name);
396 CHECK(i != group_map_.end());
397
398 Group& group = i->second;
399
[email protected]5edbf8d2010-01-13 18:44:11400 group.num_releasing_sockets--;
401 DCHECK_GE(group.num_releasing_sockets, 0);
402
[email protected]211d2172009-07-22 15:48:53403 CHECK(handed_out_socket_count_ > 0);
404 handed_out_socket_count_--;
405
[email protected]ff579d42009-06-24 15:47:02406 CHECK(group.active_socket_count > 0);
[email protected]2ab05b52009-07-01 23:57:58407 group.active_socket_count--;
[email protected]ff579d42009-06-24 15:47:02408
409 const bool can_reuse = socket->IsConnectedAndIdle();
410 if (can_reuse) {
[email protected]5fc08e32009-07-15 17:09:57411 AddIdleSocket(socket, true /* used socket */, &group);
[email protected]ff579d42009-06-24 15:47:02412 } else {
413 delete socket;
414 }
415
[email protected]4f2abec2010-02-03 18:10:16416 const bool more_releasing_sockets = group.num_releasing_sockets > 0;
417
[email protected]2ab05b52009-07-01 23:57:58418 OnAvailableSocketSlot(group_name, &group);
[email protected]4f2abec2010-02-03 18:10:16419
420 // If there are no more releasing sockets, then we might have to process
421 // multiple available socket slots, since we stalled their processing until
422 // all sockets have been released.
423 if (more_releasing_sockets)
424 return;
425
426 while (true) {
427 // We can't activate more sockets since we're already at our global limit.
428 if (ReachedMaxSocketsLimit())
429 return;
430
431 // |group| might now be deleted.
432 i = group_map_.find(group_name);
433 if (i == group_map_.end())
434 return;
435
436 group = i->second;
437
438 // If we already have enough ConnectJobs to satisfy the pending requests,
439 // don't bother starting up more.
440 if (group.pending_requests.size() <= group.jobs.size())
441 return;
442
443 if (!group.HasAvailableSocketSlot(max_sockets_per_group_))
444 return;
445
446 OnAvailableSocketSlot(group_name, &group);
447 }
[email protected]ff579d42009-06-24 15:47:02448}
449
[email protected]211d2172009-07-22 15:48:53450// Search for the highest priority pending request, amongst the groups that
451// are not at the |max_sockets_per_group_| limit. Note: for requests with
452// the same priority, the winner is based on group hash ordering (and not
453// insertion order).
[email protected]d80a4322009-08-14 07:07:49454int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
455 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53456 Group* top_group = NULL;
457 const std::string* top_group_name = NULL;
458 int stalled_group_count = 0;
459 for (GroupMap::iterator i = group_map_.begin();
460 i != group_map_.end(); ++i) {
461 Group& group = i->second;
462 const RequestQueue& queue = group.pending_requests;
463 if (queue.empty())
464 continue;
465 bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
466 if (has_slot)
467 stalled_group_count++;
468 bool has_higher_priority = !top_group ||
[email protected]ac790b42009-12-02 04:31:31469 group.TopPendingPriority() < top_group->TopPendingPriority();
[email protected]211d2172009-07-22 15:48:53470 if (has_slot && has_higher_priority) {
471 top_group = &group;
472 top_group_name = &i->first;
473 }
474 }
475 if (top_group) {
476 *group = top_group;
477 *group_name = *top_group_name;
478 }
479 return stalled_group_count;
480}
481
[email protected]d80a4322009-08-14 07:07:49482void ClientSocketPoolBaseHelper::OnConnectJobComplete(
483 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58484 DCHECK_NE(ERR_IO_PENDING, result);
485 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02486 GroupMap::iterator group_it = group_map_.find(group_name);
487 CHECK(group_it != group_map_.end());
488 Group& group = group_it->second;
489
[email protected]5fc08e32009-07-15 17:09:57490 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02491
[email protected]4d3b05d2010-01-27 21:27:29492 scoped_refptr<LoadLog> job_load_log(job->load_log());
493 RemoveConnectJob(job, &group);
[email protected]5fc08e32009-07-15 17:09:57494
[email protected]4d3b05d2010-01-27 21:27:29495 LoadLog::EndEvent(job_load_log, LoadLog::TYPE_SOCKET_POOL);
496
497 if (result == OK) {
498 DCHECK(socket.get());
[email protected]fd7b7c92009-08-20 19:38:30499 if (!group.pending_requests.empty()) {
[email protected]4d3b05d2010-01-27 21:27:29500 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]fd7b7c92009-08-20 19:38:30501 group.pending_requests.begin(), &group.pending_requests));
[email protected]fd7b7c92009-08-20 19:38:30502 if (r->load_log())
503 r->load_log()->Append(job_load_log);
[email protected]4d3b05d2010-01-27 21:27:29504 HandOutSocket(
505 socket.release(), false /* unused socket */, r->handle(),
[email protected]fd4fe0b2010-02-08 23:02:15506 base::TimeDelta(), &group, r->load_log());
[email protected]4d3b05d2010-01-27 21:27:29507 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57508 } else {
[email protected]4d3b05d2010-01-27 21:27:29509 AddIdleSocket(socket.release(), false /* unused socket */, &group);
510 OnAvailableSocketSlot(group_name, &group);
[email protected]5fc08e32009-07-15 17:09:57511 }
[email protected]94c20472010-01-14 08:14:36512 } else {
[email protected]4d3b05d2010-01-27 21:27:29513 DCHECK(!socket.get());
514 if (!group.pending_requests.empty()) {
515 scoped_ptr<const Request> r(RemoveRequestFromQueue(
516 group.pending_requests.begin(), &group.pending_requests));
517 if (r->load_log())
518 r->load_log()->Append(job_load_log);
519 r->callback()->Run(result);
520 }
521 MaybeOnAvailableSocketSlot(group_name);
[email protected]ff579d42009-06-24 15:47:02522 }
[email protected]ff579d42009-06-24 15:47:02523}
524
[email protected]100d5fb92009-12-21 21:08:35525void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
526 CloseIdleSockets();
527}
528
[email protected]4d3b05d2010-01-27 21:27:29529void ClientSocketPoolBaseHelper::RemoveConnectJob(const ConnectJob *job,
530 Group* group) {
[email protected]211d2172009-07-22 15:48:53531 CHECK(connecting_socket_count_ > 0);
532 connecting_socket_count_--;
533
[email protected]4d3b05d2010-01-27 21:27:29534 DCHECK(job);
535 delete job;
[email protected]5fc08e32009-07-15 17:09:57536
537 if (group) {
538 DCHECK(ContainsKey(group->jobs, job));
539 group->jobs.erase(job);
540 }
[email protected]ff579d42009-06-24 15:47:02541}
542
[email protected]d80a4322009-08-14 07:07:49543void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot(
[email protected]2ab05b52009-07-01 23:57:58544 const std::string& group_name) {
545 GroupMap::iterator it = group_map_.find(group_name);
546 if (it != group_map_.end()) {
547 Group& group = it->second;
548 if (group.HasAvailableSocketSlot(max_sockets_per_group_))
549 OnAvailableSocketSlot(group_name, &group);
550 }
551}
[email protected]ff579d42009-06-24 15:47:02552
[email protected]d80a4322009-08-14 07:07:49553void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
554 const std::string& group_name, Group* group) {
[email protected]211d2172009-07-22 15:48:53555 if (may_have_stalled_group_) {
556 std::string top_group_name;
[email protected]bed37d442009-08-20 19:58:20557 Group* top_group = NULL;
[email protected]211d2172009-07-22 15:48:53558 int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
559 if (stalled_group_count <= 1)
560 may_have_stalled_group_ = false;
561 if (stalled_group_count >= 1)
562 ProcessPendingRequest(top_group_name, top_group);
563 } else if (!group->pending_requests.empty()) {
[email protected]ff579d42009-06-24 15:47:02564 ProcessPendingRequest(group_name, group);
565 // |group| may no longer be valid after this point. Be careful not to
566 // access it again.
[email protected]2ab05b52009-07-01 23:57:58567 } else if (group->IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02568 // Delete |group| if no longer needed. |group| will no longer be valid.
[email protected]ff579d42009-06-24 15:47:02569 group_map_.erase(group_name);
[email protected]ff579d42009-06-24 15:47:02570 }
571}
572
[email protected]d80a4322009-08-14 07:07:49573void ClientSocketPoolBaseHelper::ProcessPendingRequest(
574 const std::string& group_name, Group* group) {
[email protected]fd4fe0b2010-02-08 23:02:15575 scoped_ptr<const Request> r(*group->pending_requests.begin());
576 int rv = RequestSocketInternal(group_name, r.get());
[email protected]ff579d42009-06-24 15:47:02577
[email protected]2ab05b52009-07-01 23:57:58578 if (rv != ERR_IO_PENDING) {
[email protected]fd7b7c92009-08-20 19:38:30579 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
[email protected]fd4fe0b2010-02-08 23:02:15580 RemoveRequestFromQueue(group->pending_requests.begin(),
581 &group->pending_requests);
[email protected]d80a4322009-08-14 07:07:49582 r->callback()->Run(rv);
[email protected]2ab05b52009-07-01 23:57:58583 if (rv != OK) {
584 // |group| may be invalid after the callback, we need to search
585 // |group_map_| again.
586 MaybeOnAvailableSocketSlot(group_name);
587 }
[email protected]d80a4322009-08-14 07:07:49588 } else {
589 r.release();
[email protected]2ab05b52009-07-01 23:57:58590 }
591}
592
[email protected]d80a4322009-08-14 07:07:49593void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58594 ClientSocket* socket,
595 bool reused,
596 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29597 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15598 Group* group,
599 LoadLog* load_log) {
[email protected]2ab05b52009-07-01 23:57:58600 DCHECK(socket);
601 handle->set_socket(socket);
602 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29603 handle->set_idle_time(idle_time);
[email protected]211d2172009-07-22 15:48:53604
[email protected]fd4fe0b2010-02-08 23:02:15605 if (reused)
606 LoadLog::AddStringLiteral(load_log, "Reusing socket.");
607 if (idle_time != base::TimeDelta()) {
608 LoadLog::AddString(
609 load_log,
610 StringPrintf("Socket sat idle for %" PRId64 " milliseconds",
611 idle_time.InMilliseconds()));
612 }
613
[email protected]211d2172009-07-22 15:48:53614 handed_out_socket_count_++;
[email protected]2ab05b52009-07-01 23:57:58615 group->active_socket_count++;
[email protected]ff579d42009-06-24 15:47:02616}
617
[email protected]d80a4322009-08-14 07:07:49618void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57619 ClientSocket* socket, bool used, Group* group) {
620 DCHECK(socket);
621 IdleSocket idle_socket;
622 idle_socket.socket = socket;
623 idle_socket.start_time = base::TimeTicks::Now();
624 idle_socket.used = used;
625
626 group->idle_sockets.push_back(idle_socket);
627 IncrementIdleCount();
628}
629
[email protected]d80a4322009-08-14 07:07:49630void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]5fc08e32009-07-15 17:09:57631 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
632 Group& group = i->second;
[email protected]4d3b05d2010-01-27 21:27:29633 connecting_socket_count_ -= group.jobs.size();
[email protected]5fc08e32009-07-15 17:09:57634 STLDeleteElements(&group.jobs);
635
636 // Delete group if no longer needed.
637 if (group.IsEmpty()) {
[email protected]5fc08e32009-07-15 17:09:57638 group_map_.erase(i++);
639 } else {
640 ++i;
641 }
642 }
643}
644
[email protected]d80a4322009-08-14 07:07:49645bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53646 // Each connecting socket will eventually connect and be handed out.
647 int total = handed_out_socket_count_ + connecting_socket_count_;
648 DCHECK_LE(total, max_sockets_);
649 return total == max_sockets_;
650}
651
[email protected]d80a4322009-08-14 07:07:49652} // namespace internal
653
[email protected]ff579d42009-06-24 15:47:02654} // namespace net