blob: c6d9493457189c6d28ca909dfe2f808a31704dc3 [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]2ab05b52009-07-01 23:57:58416 OnAvailableSocketSlot(group_name, &group);
[email protected]4f2abec2010-02-03 18:10:16417
418 // If there are no more releasing sockets, then we might have to process
419 // multiple available socket slots, since we stalled their processing until
420 // all sockets have been released.
[email protected]4f1e4982010-03-02 18:31:04421 i = group_map_.find(group_name);
422 if (i == group_map_.end() || i->second.num_releasing_sockets > 0)
[email protected]4f2abec2010-02-03 18:10:16423 return;
424
425 while (true) {
426 // We can't activate more sockets since we're already at our global limit.
427 if (ReachedMaxSocketsLimit())
428 return;
[email protected]616925a2010-03-02 19:02:38429
[email protected]4f2abec2010-02-03 18:10:16430 // |group| might now be deleted.
431 i = group_map_.find(group_name);
432 if (i == group_map_.end())
433 return;
434
435 group = i->second;
[email protected]616925a2010-03-02 19:02:38436
[email protected]4f2abec2010-02-03 18:10:16437 // If we already have enough ConnectJobs to satisfy the pending requests,
438 // don't bother starting up more.
439 if (group.pending_requests.size() <= group.jobs.size())
440 return;
441
442 if (!group.HasAvailableSocketSlot(max_sockets_per_group_))
443 return;
444
445 OnAvailableSocketSlot(group_name, &group);
446 }
[email protected]ff579d42009-06-24 15:47:02447}
448
[email protected]211d2172009-07-22 15:48:53449// Search for the highest priority pending request, amongst the groups that
450// are not at the |max_sockets_per_group_| limit. Note: for requests with
451// the same priority, the winner is based on group hash ordering (and not
452// insertion order).
[email protected]d80a4322009-08-14 07:07:49453int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
454 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53455 Group* top_group = NULL;
456 const std::string* top_group_name = NULL;
457 int stalled_group_count = 0;
458 for (GroupMap::iterator i = group_map_.begin();
459 i != group_map_.end(); ++i) {
460 Group& group = i->second;
461 const RequestQueue& queue = group.pending_requests;
462 if (queue.empty())
463 continue;
464 bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
465 if (has_slot)
466 stalled_group_count++;
467 bool has_higher_priority = !top_group ||
[email protected]ac790b42009-12-02 04:31:31468 group.TopPendingPriority() < top_group->TopPendingPriority();
[email protected]211d2172009-07-22 15:48:53469 if (has_slot && has_higher_priority) {
470 top_group = &group;
471 top_group_name = &i->first;
472 }
473 }
474 if (top_group) {
475 *group = top_group;
476 *group_name = *top_group_name;
477 }
478 return stalled_group_count;
479}
480
[email protected]d80a4322009-08-14 07:07:49481void ClientSocketPoolBaseHelper::OnConnectJobComplete(
482 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58483 DCHECK_NE(ERR_IO_PENDING, result);
484 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02485 GroupMap::iterator group_it = group_map_.find(group_name);
486 CHECK(group_it != group_map_.end());
487 Group& group = group_it->second;
488
[email protected]5fc08e32009-07-15 17:09:57489 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02490
[email protected]4d3b05d2010-01-27 21:27:29491 scoped_refptr<LoadLog> job_load_log(job->load_log());
492 RemoveConnectJob(job, &group);
[email protected]5fc08e32009-07-15 17:09:57493
[email protected]4d3b05d2010-01-27 21:27:29494 LoadLog::EndEvent(job_load_log, LoadLog::TYPE_SOCKET_POOL);
495
496 if (result == OK) {
497 DCHECK(socket.get());
[email protected]fd7b7c92009-08-20 19:38:30498 if (!group.pending_requests.empty()) {
[email protected]4d3b05d2010-01-27 21:27:29499 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]fd7b7c92009-08-20 19:38:30500 group.pending_requests.begin(), &group.pending_requests));
[email protected]fd7b7c92009-08-20 19:38:30501 if (r->load_log())
502 r->load_log()->Append(job_load_log);
[email protected]4d3b05d2010-01-27 21:27:29503 HandOutSocket(
504 socket.release(), false /* unused socket */, r->handle(),
[email protected]fd4fe0b2010-02-08 23:02:15505 base::TimeDelta(), &group, r->load_log());
[email protected]4d3b05d2010-01-27 21:27:29506 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57507 } else {
[email protected]4d3b05d2010-01-27 21:27:29508 AddIdleSocket(socket.release(), false /* unused socket */, &group);
509 OnAvailableSocketSlot(group_name, &group);
[email protected]5fc08e32009-07-15 17:09:57510 }
[email protected]94c20472010-01-14 08:14:36511 } else {
[email protected]4d3b05d2010-01-27 21:27:29512 DCHECK(!socket.get());
513 if (!group.pending_requests.empty()) {
514 scoped_ptr<const Request> r(RemoveRequestFromQueue(
515 group.pending_requests.begin(), &group.pending_requests));
516 if (r->load_log())
517 r->load_log()->Append(job_load_log);
518 r->callback()->Run(result);
519 }
520 MaybeOnAvailableSocketSlot(group_name);
[email protected]ff579d42009-06-24 15:47:02521 }
[email protected]ff579d42009-06-24 15:47:02522}
523
[email protected]100d5fb92009-12-21 21:08:35524void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
525 CloseIdleSockets();
526}
527
[email protected]4d3b05d2010-01-27 21:27:29528void ClientSocketPoolBaseHelper::RemoveConnectJob(const ConnectJob *job,
529 Group* group) {
[email protected]211d2172009-07-22 15:48:53530 CHECK(connecting_socket_count_ > 0);
531 connecting_socket_count_--;
532
[email protected]4d3b05d2010-01-27 21:27:29533 DCHECK(job);
534 delete job;
[email protected]5fc08e32009-07-15 17:09:57535
536 if (group) {
537 DCHECK(ContainsKey(group->jobs, job));
538 group->jobs.erase(job);
539 }
[email protected]ff579d42009-06-24 15:47:02540}
541
[email protected]d80a4322009-08-14 07:07:49542void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot(
[email protected]2ab05b52009-07-01 23:57:58543 const std::string& group_name) {
544 GroupMap::iterator it = group_map_.find(group_name);
545 if (it != group_map_.end()) {
546 Group& group = it->second;
547 if (group.HasAvailableSocketSlot(max_sockets_per_group_))
548 OnAvailableSocketSlot(group_name, &group);
549 }
550}
[email protected]ff579d42009-06-24 15:47:02551
[email protected]d80a4322009-08-14 07:07:49552void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
553 const std::string& group_name, Group* group) {
[email protected]211d2172009-07-22 15:48:53554 if (may_have_stalled_group_) {
555 std::string top_group_name;
[email protected]bed37d442009-08-20 19:58:20556 Group* top_group = NULL;
[email protected]211d2172009-07-22 15:48:53557 int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
558 if (stalled_group_count <= 1)
559 may_have_stalled_group_ = false;
560 if (stalled_group_count >= 1)
561 ProcessPendingRequest(top_group_name, top_group);
562 } else if (!group->pending_requests.empty()) {
[email protected]ff579d42009-06-24 15:47:02563 ProcessPendingRequest(group_name, group);
564 // |group| may no longer be valid after this point. Be careful not to
565 // access it again.
[email protected]2ab05b52009-07-01 23:57:58566 } else if (group->IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02567 // Delete |group| if no longer needed. |group| will no longer be valid.
[email protected]ff579d42009-06-24 15:47:02568 group_map_.erase(group_name);
[email protected]ff579d42009-06-24 15:47:02569 }
570}
571
[email protected]d80a4322009-08-14 07:07:49572void ClientSocketPoolBaseHelper::ProcessPendingRequest(
573 const std::string& group_name, Group* group) {
[email protected]fd4fe0b2010-02-08 23:02:15574 scoped_ptr<const Request> r(*group->pending_requests.begin());
575 int rv = RequestSocketInternal(group_name, r.get());
[email protected]ff579d42009-06-24 15:47:02576
[email protected]2ab05b52009-07-01 23:57:58577 if (rv != ERR_IO_PENDING) {
[email protected]fd7b7c92009-08-20 19:38:30578 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
[email protected]fd4fe0b2010-02-08 23:02:15579 RemoveRequestFromQueue(group->pending_requests.begin(),
580 &group->pending_requests);
[email protected]d80a4322009-08-14 07:07:49581 r->callback()->Run(rv);
[email protected]2ab05b52009-07-01 23:57:58582 if (rv != OK) {
583 // |group| may be invalid after the callback, we need to search
584 // |group_map_| again.
585 MaybeOnAvailableSocketSlot(group_name);
586 }
[email protected]d80a4322009-08-14 07:07:49587 } else {
588 r.release();
[email protected]2ab05b52009-07-01 23:57:58589 }
590}
591
[email protected]d80a4322009-08-14 07:07:49592void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58593 ClientSocket* socket,
594 bool reused,
595 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29596 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15597 Group* group,
598 LoadLog* load_log) {
[email protected]2ab05b52009-07-01 23:57:58599 DCHECK(socket);
600 handle->set_socket(socket);
601 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29602 handle->set_idle_time(idle_time);
[email protected]211d2172009-07-22 15:48:53603
[email protected]fd4fe0b2010-02-08 23:02:15604 if (reused)
605 LoadLog::AddStringLiteral(load_log, "Reusing socket.");
606 if (idle_time != base::TimeDelta()) {
607 LoadLog::AddString(
608 load_log,
609 StringPrintf("Socket sat idle for %" PRId64 " milliseconds",
610 idle_time.InMilliseconds()));
611 }
612
[email protected]211d2172009-07-22 15:48:53613 handed_out_socket_count_++;
[email protected]2ab05b52009-07-01 23:57:58614 group->active_socket_count++;
[email protected]ff579d42009-06-24 15:47:02615}
616
[email protected]d80a4322009-08-14 07:07:49617void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57618 ClientSocket* socket, bool used, Group* group) {
619 DCHECK(socket);
620 IdleSocket idle_socket;
621 idle_socket.socket = socket;
622 idle_socket.start_time = base::TimeTicks::Now();
623 idle_socket.used = used;
624
625 group->idle_sockets.push_back(idle_socket);
626 IncrementIdleCount();
627}
628
[email protected]d80a4322009-08-14 07:07:49629void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]5fc08e32009-07-15 17:09:57630 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
631 Group& group = i->second;
[email protected]4d3b05d2010-01-27 21:27:29632 connecting_socket_count_ -= group.jobs.size();
[email protected]5fc08e32009-07-15 17:09:57633 STLDeleteElements(&group.jobs);
634
635 // Delete group if no longer needed.
636 if (group.IsEmpty()) {
[email protected]5fc08e32009-07-15 17:09:57637 group_map_.erase(i++);
638 } else {
639 ++i;
640 }
641 }
642}
643
[email protected]d80a4322009-08-14 07:07:49644bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53645 // Each connecting socket will eventually connect and be handed out.
646 int total = handed_out_socket_count_ + connecting_socket_count_;
647 DCHECK_LE(total, max_sockets_);
648 return total == max_sockets_;
649}
650
[email protected]d80a4322009-08-14 07:07:49651} // namespace internal
652
[email protected]ff579d42009-06-24 15:47:02653} // namespace net