blob: 4a5c88ca5165cc670e6b46b15ff4346510c6de67 [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"
8#include "base/message_loop.h"
9#include "base/stl_util-inl.h"
10#include "base/time.h"
[email protected]fd7b7c92009-08-20 19:38:3011#include "net/base/load_log.h"
[email protected]ff579d42009-06-24 15:47:0212#include "net/base/net_errors.h"
13#include "net/socket/client_socket_handle.h"
14
15using base::TimeDelta;
16
17namespace {
18
19// The timeout value, in seconds, used to clean up idle sockets that can't be
20// reused.
21//
22// Note: It's important to close idle sockets that have received data as soon
23// as possible because the received data may cause BSOD on Windows XP under
24// some conditions. See http://crbug.com/4606.
25const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
26
[email protected]60c4c412009-11-06 19:59:3627// The maximum size of the ConnectJob's LoadLog.
28const int kMaxNumLoadLogEntries = 50;
29
[email protected]ff579d42009-06-24 15:47:0230} // namespace
31
32namespace net {
33
[email protected]2ab05b52009-07-01 23:57:5834ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3435 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3036 Delegate* delegate,
37 LoadLog* load_log)
[email protected]2ab05b52009-07-01 23:57:5838 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3439 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5840 delegate_(delegate),
[email protected]fd7b7c92009-08-20 19:38:3041 load_log_(load_log) {
[email protected]2ab05b52009-07-01 23:57:5842 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5843 DCHECK(delegate);
44}
45
[email protected]fd7b7c92009-08-20 19:38:3046ConnectJob::~ConnectJob() {
47 if (delegate_) {
48 // If the delegate was not NULLed, then NotifyDelegateOfCompletion has
49 // not been called yet (hence we are cancelling).
50 LoadLog::AddEvent(load_log_, LoadLog::TYPE_CANCELLED);
51 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
52 }
53}
[email protected]2ab05b52009-07-01 23:57:5854
[email protected]974ebd62009-08-03 23:14:3455int ConnectJob::Connect() {
56 if (timeout_duration_ != base::TimeDelta())
57 timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3058
59 LoadLog::BeginEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
60
61 int rv = ConnectInternal();
62
63 if (rv != ERR_IO_PENDING) {
64 delegate_ = NULL;
65 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
66 }
67
68 return rv;
69}
70
71void ConnectJob::NotifyDelegateOfCompletion(int rv) {
72 // The delegate will delete |this|.
73 Delegate *delegate = delegate_;
74 delegate_ = NULL;
75
76 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
77
78 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:3479}
80
81void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:4082 // Make sure the socket is NULL before calling into |delegate|.
83 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:3084
85 LoadLog::AddEvent(load_log_,
86 LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
87
88 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:3489}
90
[email protected]d80a4322009-08-14 07:07:4991namespace internal {
92
[email protected]d80a4322009-08-14 07:07:4993ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:5394 int max_sockets,
[email protected]ff579d42009-06-24 15:47:0295 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:1696 base::TimeDelta unused_idle_socket_timeout,
97 base::TimeDelta used_idle_socket_timeout,
[email protected]100d5fb92009-12-21 21:08:3598 ConnectJobFactory* connect_job_factory,
[email protected]d13c3272010-02-04 00:24:5199 NetworkChangeNotifier* network_change_notifier)
[email protected]ff579d42009-06-24 15:47:02100 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53101 connecting_socket_count_(0),
102 handed_out_socket_count_(0),
103 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02104 max_sockets_per_group_(max_sockets_per_group),
[email protected]9bf28db2009-08-29 01:35:16105 unused_idle_socket_timeout_(unused_idle_socket_timeout),
106 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]211d2172009-07-22 15:48:53107 may_have_stalled_group_(false),
[email protected]100d5fb92009-12-21 21:08:35108 connect_job_factory_(connect_job_factory),
109 network_change_notifier_(network_change_notifier) {
[email protected]211d2172009-07-22 15:48:53110 DCHECK_LE(0, max_sockets_per_group);
111 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]100d5fb92009-12-21 21:08:35112
113 if (network_change_notifier_)
114 network_change_notifier_->AddObserver(this);
[email protected]211d2172009-07-22 15:48:53115}
[email protected]ff579d42009-06-24 15:47:02116
[email protected]d80a4322009-08-14 07:07:49117ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]4d3b05d2010-01-27 21:27:29118 CancelAllConnectJobs();
119
[email protected]ff579d42009-06-24 15:47:02120 // Clean up any idle sockets. Assert that we have no remaining active
121 // sockets or pending requests. They should have all been cleaned up prior
122 // to the manager being destroyed.
123 CloseIdleSockets();
124 DCHECK(group_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29125 DCHECK_EQ(0, connecting_socket_count_);
[email protected]100d5fb92009-12-21 21:08:35126
127 if (network_change_notifier_)
128 network_change_notifier_->RemoveObserver(this);
[email protected]ff579d42009-06-24 15:47:02129}
130
131// InsertRequestIntoQueue inserts the request into the queue based on
132// priority. Highest priorities are closest to the front. Older requests are
133// prioritized over requests of equal priority.
134//
135// static
[email protected]d80a4322009-08-14 07:07:49136void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
137 const Request* r, RequestQueue* pending_requests) {
[email protected]fd7b7c92009-08-20 19:38:30138 LoadLog::BeginEvent(r->load_log(),
139 LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE);
140
[email protected]ff579d42009-06-24 15:47:02141 RequestQueue::iterator it = pending_requests->begin();
[email protected]ac790b42009-12-02 04:31:31142 while (it != pending_requests->end() && r->priority() >= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02143 ++it;
144 pending_requests->insert(it, r);
145}
146
[email protected]fd7b7c92009-08-20 19:38:30147// static
148const ClientSocketPoolBaseHelper::Request*
149ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
150 RequestQueue::iterator it, RequestQueue* pending_requests) {
151 const Request* req = *it;
152
153 LoadLog::EndEvent(req->load_log(),
[email protected]4d3b05d2010-01-27 21:27:29154 LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE);
[email protected]fd7b7c92009-08-20 19:38:30155
156 pending_requests->erase(it);
157 return req;
158}
159
[email protected]d80a4322009-08-14 07:07:49160int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02161 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49162 const Request* request) {
163 DCHECK_GE(request->priority(), 0);
164 CompletionCallback* const callback = request->callback();
165 CHECK(callback);
166 ClientSocketHandle* const handle = request->handle();
167 CHECK(handle);
[email protected]ff579d42009-06-24 15:47:02168 Group& group = group_map_[group_name];
169
[email protected]ff579d42009-06-24 15:47:02170 // Can we make another active socket now?
[email protected]211d2172009-07-22 15:48:53171 if (ReachedMaxSocketsLimit() ||
172 !group.HasAvailableSocketSlot(max_sockets_per_group_)) {
173 if (ReachedMaxSocketsLimit()) {
174 // We could check if we really have a stalled group here, but it requires
175 // a scan of all groups, so just flip a flag here, and do the check later.
176 may_have_stalled_group_ = true;
[email protected]fd7b7c92009-08-20 19:38:30177
178 LoadLog::AddEvent(request->load_log(),
179 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
180 } else {
181 LoadLog::AddEvent(request->load_log(),
182 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]211d2172009-07-22 15:48:53183 }
[email protected]d80a4322009-08-14 07:07:49184 InsertRequestIntoQueue(request, &group.pending_requests);
[email protected]ff579d42009-06-24 15:47:02185 return ERR_IO_PENDING;
186 }
187
[email protected]5edbf8d2010-01-13 18:44:11188 // Try to reuse a socket.
[email protected]ff579d42009-06-24 15:47:02189 while (!group.idle_sockets.empty()) {
190 IdleSocket idle_socket = group.idle_sockets.back();
191 group.idle_sockets.pop_back();
192 DecrementIdleCount();
193 if (idle_socket.socket->IsConnectedAndIdle()) {
194 // We found one we can reuse!
[email protected]f9d285c2009-08-17 19:54:29195 base::TimeDelta idle_time =
196 base::TimeTicks::Now() - idle_socket.start_time;
[email protected]d80a4322009-08-14 07:07:49197 HandOutSocket(
[email protected]f9d285c2009-08-17 19:54:29198 idle_socket.socket, idle_socket.used, handle, idle_time, &group);
[email protected]ff579d42009-06-24 15:47:02199 return OK;
200 }
201 delete idle_socket.socket;
202 }
203
[email protected]5edbf8d2010-01-13 18:44:11204 // See if we already have enough connect jobs or sockets that will be released
205 // soon.
[email protected]4d3b05d2010-01-27 21:27:29206 if (group.HasReleasingSockets()) {
[email protected]5edbf8d2010-01-13 18:44:11207 InsertRequestIntoQueue(request, &group.pending_requests);
208 return ERR_IO_PENDING;
209 }
210
[email protected]ff579d42009-06-24 15:47:02211 // We couldn't find a socket to reuse, so allocate and connect a new one.
[email protected]4d3b05d2010-01-27 21:27:29212 scoped_refptr<LoadLog> job_load_log = new LoadLog(kMaxNumLoadLogEntries);
[email protected]fd7b7c92009-08-20 19:38:30213
[email protected]2ab05b52009-07-01 23:57:58214 scoped_ptr<ConnectJob> connect_job(
[email protected]fd7b7c92009-08-20 19:38:30215 connect_job_factory_->NewConnectJob(group_name, *request, this,
[email protected]5edbf8d2010-01-13 18:44:11216 job_load_log));
[email protected]ff579d42009-06-24 15:47:02217
[email protected]2ab05b52009-07-01 23:57:58218 int rv = connect_job->Connect();
[email protected]fd7b7c92009-08-20 19:38:30219
[email protected]4d3b05d2010-01-27 21:27:29220 if (rv != ERR_IO_PENDING && request->load_log())
[email protected]fd7b7c92009-08-20 19:38:30221 request->load_log()->Append(job_load_log);
222
[email protected]2ab05b52009-07-01 23:57:58223 if (rv == OK) {
224 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]f9d285c2009-08-17 19:54:29225 handle, base::TimeDelta(), &group);
[email protected]2ab05b52009-07-01 23:57:58226 } else if (rv == ERR_IO_PENDING) {
[email protected]211d2172009-07-22 15:48:53227 connecting_socket_count_++;
228
[email protected]5fc08e32009-07-15 17:09:57229 ConnectJob* job = connect_job.release();
[email protected]4d3b05d2010-01-27 21:27:29230 InsertRequestIntoQueue(request, &group.pending_requests);
[email protected]5fc08e32009-07-15 17:09:57231 group.jobs.insert(job);
[email protected]2b7523d2009-07-29 20:29:23232 } else if (group.IsEmpty()) {
233 group_map_.erase(group_name);
[email protected]2ab05b52009-07-01 23:57:58234 }
[email protected]ff579d42009-06-24 15:47:02235
[email protected]2ab05b52009-07-01 23:57:58236 return rv;
[email protected]ff579d42009-06-24 15:47:02237}
238
[email protected]d80a4322009-08-14 07:07:49239void ClientSocketPoolBaseHelper::CancelRequest(
240 const std::string& group_name, const ClientSocketHandle* handle) {
[email protected]ff579d42009-06-24 15:47:02241 CHECK(ContainsKey(group_map_, group_name));
242
243 Group& group = group_map_[group_name];
244
[email protected]ff579d42009-06-24 15:47:02245 // Search pending_requests for matching handle.
246 RequestQueue::iterator it = group.pending_requests.begin();
247 for (; it != group.pending_requests.end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49248 if ((*it)->handle() == handle) {
[email protected]fd7b7c92009-08-20 19:38:30249 const Request* req = RemoveRequestFromQueue(it, &group.pending_requests);
250 LoadLog::AddEvent(req->load_log(), LoadLog::TYPE_CANCELLED);
251 LoadLog::EndEvent(req->load_log(), LoadLog::TYPE_SOCKET_POOL);
252 delete req;
[email protected]4d3b05d2010-01-27 21:27:29253 if (group.jobs.size() > group.pending_requests.size() + 1) {
[email protected]974ebd62009-08-03 23:14:34254 // TODO(willchan): Cancel the job in the earliest LoadState.
[email protected]4d3b05d2010-01-27 21:27:29255 RemoveConnectJob(*group.jobs.begin(), &group);
[email protected]974ebd62009-08-03 23:14:34256 OnAvailableSocketSlot(group_name, &group);
257 }
[email protected]ff579d42009-06-24 15:47:02258 return;
259 }
260 }
[email protected]ff579d42009-06-24 15:47:02261}
262
[email protected]d80a4322009-08-14 07:07:49263void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
264 ClientSocket* socket) {
[email protected]5edbf8d2010-01-13 18:44:11265 Group& group = group_map_[group_name];
266 group.num_releasing_sockets++;
267 DCHECK_LE(group.num_releasing_sockets, group.active_socket_count);
[email protected]ff579d42009-06-24 15:47:02268 // Run this asynchronously to allow the caller to finish before we let
269 // another to begin doing work. This also avoids nasty recursion issues.
270 // NOTE: We cannot refer to the handle argument after this method returns.
271 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]d80a4322009-08-14 07:07:49272 this, &ClientSocketPoolBaseHelper::DoReleaseSocket, group_name, socket));
[email protected]ff579d42009-06-24 15:47:02273}
274
[email protected]d80a4322009-08-14 07:07:49275void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02276 CleanupIdleSockets(true);
277}
278
[email protected]d80a4322009-08-14 07:07:49279int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02280 const std::string& group_name) const {
281 GroupMap::const_iterator i = group_map_.find(group_name);
282 CHECK(i != group_map_.end());
283
284 return i->second.idle_sockets.size();
285}
286
[email protected]d80a4322009-08-14 07:07:49287LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02288 const std::string& group_name,
289 const ClientSocketHandle* handle) const {
290 if (!ContainsKey(group_map_, group_name)) {
291 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
292 << " for handle: " << handle;
293 return LOAD_STATE_IDLE;
294 }
295
296 // Can't use operator[] since it is non-const.
297 const Group& group = group_map_.find(group_name)->second;
298
[email protected]ff579d42009-06-24 15:47:02299 // Search pending_requests for matching handle.
300 RequestQueue::const_iterator it = group.pending_requests.begin();
[email protected]5fc08e32009-07-15 17:09:57301 for (size_t i = 0; it != group.pending_requests.end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49302 if ((*it)->handle() == handle) {
[email protected]4d3b05d2010-01-27 21:27:29303 if (i < group.jobs.size()) {
[email protected]5fc08e32009-07-15 17:09:57304 LoadState max_state = LOAD_STATE_IDLE;
305 for (ConnectJobSet::const_iterator job_it = group.jobs.begin();
306 job_it != group.jobs.end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21307 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57308 }
309 return max_state;
310 } else {
311 // TODO(wtc): Add a state for being on the wait list.
312 // See http://www.crbug.com/5077.
313 return LOAD_STATE_IDLE;
314 }
[email protected]ff579d42009-06-24 15:47:02315 }
316 }
317
318 NOTREACHED();
319 return LOAD_STATE_IDLE;
320}
321
[email protected]d80a4322009-08-14 07:07:49322bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16323 base::TimeTicks now,
324 base::TimeDelta timeout) const {
325 bool timed_out = (now - start_time) >= timeout;
[email protected]5fc08e32009-07-15 17:09:57326 return timed_out ||
327 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
[email protected]ff579d42009-06-24 15:47:02328}
329
[email protected]d80a4322009-08-14 07:07:49330void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02331 if (idle_socket_count_ == 0)
332 return;
333
334 // Current time value. Retrieving it once at the function start rather than
335 // inside the inner loop, since it shouldn't change by any meaningful amount.
336 base::TimeTicks now = base::TimeTicks::Now();
337
338 GroupMap::iterator i = group_map_.begin();
339 while (i != group_map_.end()) {
340 Group& group = i->second;
341
342 std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
343 while (j != group.idle_sockets.end()) {
[email protected]9bf28db2009-08-29 01:35:16344 base::TimeDelta timeout =
345 j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_;
346 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02347 delete j->socket;
348 j = group.idle_sockets.erase(j);
349 DecrementIdleCount();
350 } else {
351 ++j;
352 }
353 }
354
355 // Delete group if no longer needed.
[email protected]2ab05b52009-07-01 23:57:58356 if (group.IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02357 group_map_.erase(i++);
358 } else {
359 ++i;
360 }
361 }
362}
363
[email protected]d80a4322009-08-14 07:07:49364void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02365 if (++idle_socket_count_ == 1)
366 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
[email protected]d80a4322009-08-14 07:07:49367 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
[email protected]ff579d42009-06-24 15:47:02368}
369
[email protected]d80a4322009-08-14 07:07:49370void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02371 if (--idle_socket_count_ == 0)
372 timer_.Stop();
373}
374
[email protected]d80a4322009-08-14 07:07:49375void ClientSocketPoolBaseHelper::DoReleaseSocket(const std::string& group_name,
376 ClientSocket* socket) {
[email protected]ff579d42009-06-24 15:47:02377 GroupMap::iterator i = group_map_.find(group_name);
378 CHECK(i != group_map_.end());
379
380 Group& group = i->second;
381
[email protected]5edbf8d2010-01-13 18:44:11382 group.num_releasing_sockets--;
383 DCHECK_GE(group.num_releasing_sockets, 0);
384
[email protected]211d2172009-07-22 15:48:53385 CHECK(handed_out_socket_count_ > 0);
386 handed_out_socket_count_--;
387
[email protected]ff579d42009-06-24 15:47:02388 CHECK(group.active_socket_count > 0);
[email protected]2ab05b52009-07-01 23:57:58389 group.active_socket_count--;
[email protected]ff579d42009-06-24 15:47:02390
391 const bool can_reuse = socket->IsConnectedAndIdle();
392 if (can_reuse) {
[email protected]5fc08e32009-07-15 17:09:57393 AddIdleSocket(socket, true /* used socket */, &group);
[email protected]ff579d42009-06-24 15:47:02394 } else {
395 delete socket;
396 }
397
[email protected]4f2abec2010-02-03 18:10:16398 const bool more_releasing_sockets = group.num_releasing_sockets > 0;
399
[email protected]2ab05b52009-07-01 23:57:58400 OnAvailableSocketSlot(group_name, &group);
[email protected]4f2abec2010-02-03 18:10:16401
402 // If there are no more releasing sockets, then we might have to process
403 // multiple available socket slots, since we stalled their processing until
404 // all sockets have been released.
405 if (more_releasing_sockets)
406 return;
407
408 while (true) {
409 // We can't activate more sockets since we're already at our global limit.
410 if (ReachedMaxSocketsLimit())
411 return;
412
413 // |group| might now be deleted.
414 i = group_map_.find(group_name);
415 if (i == group_map_.end())
416 return;
417
418 group = i->second;
419
420 // If we already have enough ConnectJobs to satisfy the pending requests,
421 // don't bother starting up more.
422 if (group.pending_requests.size() <= group.jobs.size())
423 return;
424
425 if (!group.HasAvailableSocketSlot(max_sockets_per_group_))
426 return;
427
428 OnAvailableSocketSlot(group_name, &group);
429 }
[email protected]ff579d42009-06-24 15:47:02430}
431
[email protected]211d2172009-07-22 15:48:53432// Search for the highest priority pending request, amongst the groups that
433// are not at the |max_sockets_per_group_| limit. Note: for requests with
434// the same priority, the winner is based on group hash ordering (and not
435// insertion order).
[email protected]d80a4322009-08-14 07:07:49436int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
437 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53438 Group* top_group = NULL;
439 const std::string* top_group_name = NULL;
440 int stalled_group_count = 0;
441 for (GroupMap::iterator i = group_map_.begin();
442 i != group_map_.end(); ++i) {
443 Group& group = i->second;
444 const RequestQueue& queue = group.pending_requests;
445 if (queue.empty())
446 continue;
447 bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
448 if (has_slot)
449 stalled_group_count++;
450 bool has_higher_priority = !top_group ||
[email protected]ac790b42009-12-02 04:31:31451 group.TopPendingPriority() < top_group->TopPendingPriority();
[email protected]211d2172009-07-22 15:48:53452 if (has_slot && has_higher_priority) {
453 top_group = &group;
454 top_group_name = &i->first;
455 }
456 }
457 if (top_group) {
458 *group = top_group;
459 *group_name = *top_group_name;
460 }
461 return stalled_group_count;
462}
463
[email protected]d80a4322009-08-14 07:07:49464void ClientSocketPoolBaseHelper::OnConnectJobComplete(
465 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58466 DCHECK_NE(ERR_IO_PENDING, result);
467 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02468 GroupMap::iterator group_it = group_map_.find(group_name);
469 CHECK(group_it != group_map_.end());
470 Group& group = group_it->second;
471
[email protected]5fc08e32009-07-15 17:09:57472 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02473
[email protected]4d3b05d2010-01-27 21:27:29474 scoped_refptr<LoadLog> job_load_log(job->load_log());
475 RemoveConnectJob(job, &group);
[email protected]5fc08e32009-07-15 17:09:57476
[email protected]4d3b05d2010-01-27 21:27:29477 LoadLog::EndEvent(job_load_log, LoadLog::TYPE_SOCKET_POOL);
478
479 if (result == OK) {
480 DCHECK(socket.get());
[email protected]fd7b7c92009-08-20 19:38:30481 if (!group.pending_requests.empty()) {
[email protected]4d3b05d2010-01-27 21:27:29482 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]fd7b7c92009-08-20 19:38:30483 group.pending_requests.begin(), &group.pending_requests));
[email protected]fd7b7c92009-08-20 19:38:30484 if (r->load_log())
485 r->load_log()->Append(job_load_log);
[email protected]4d3b05d2010-01-27 21:27:29486 HandOutSocket(
487 socket.release(), false /* unused socket */, r->handle(),
488 base::TimeDelta(), &group);
489 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57490 } else {
[email protected]4d3b05d2010-01-27 21:27:29491 AddIdleSocket(socket.release(), false /* unused socket */, &group);
492 OnAvailableSocketSlot(group_name, &group);
[email protected]5fc08e32009-07-15 17:09:57493 }
[email protected]94c20472010-01-14 08:14:36494 } else {
[email protected]4d3b05d2010-01-27 21:27:29495 DCHECK(!socket.get());
496 if (!group.pending_requests.empty()) {
497 scoped_ptr<const Request> r(RemoveRequestFromQueue(
498 group.pending_requests.begin(), &group.pending_requests));
499 if (r->load_log())
500 r->load_log()->Append(job_load_log);
501 r->callback()->Run(result);
502 }
503 MaybeOnAvailableSocketSlot(group_name);
[email protected]ff579d42009-06-24 15:47:02504 }
[email protected]ff579d42009-06-24 15:47:02505}
506
[email protected]100d5fb92009-12-21 21:08:35507void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
508 CloseIdleSockets();
509}
510
[email protected]4d3b05d2010-01-27 21:27:29511void ClientSocketPoolBaseHelper::RemoveConnectJob(const ConnectJob *job,
512 Group* group) {
[email protected]211d2172009-07-22 15:48:53513 CHECK(connecting_socket_count_ > 0);
514 connecting_socket_count_--;
515
[email protected]4d3b05d2010-01-27 21:27:29516 DCHECK(job);
517 delete job;
[email protected]5fc08e32009-07-15 17:09:57518
519 if (group) {
520 DCHECK(ContainsKey(group->jobs, job));
521 group->jobs.erase(job);
522 }
[email protected]ff579d42009-06-24 15:47:02523}
524
[email protected]d80a4322009-08-14 07:07:49525void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot(
[email protected]2ab05b52009-07-01 23:57:58526 const std::string& group_name) {
527 GroupMap::iterator it = group_map_.find(group_name);
528 if (it != group_map_.end()) {
529 Group& group = it->second;
530 if (group.HasAvailableSocketSlot(max_sockets_per_group_))
531 OnAvailableSocketSlot(group_name, &group);
532 }
533}
[email protected]ff579d42009-06-24 15:47:02534
[email protected]d80a4322009-08-14 07:07:49535void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
536 const std::string& group_name, Group* group) {
[email protected]211d2172009-07-22 15:48:53537 if (may_have_stalled_group_) {
538 std::string top_group_name;
[email protected]bed37d442009-08-20 19:58:20539 Group* top_group = NULL;
[email protected]211d2172009-07-22 15:48:53540 int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
541 if (stalled_group_count <= 1)
542 may_have_stalled_group_ = false;
543 if (stalled_group_count >= 1)
544 ProcessPendingRequest(top_group_name, top_group);
545 } else if (!group->pending_requests.empty()) {
[email protected]ff579d42009-06-24 15:47:02546 ProcessPendingRequest(group_name, group);
547 // |group| may no longer be valid after this point. Be careful not to
548 // access it again.
[email protected]2ab05b52009-07-01 23:57:58549 } else if (group->IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02550 // Delete |group| if no longer needed. |group| will no longer be valid.
[email protected]ff579d42009-06-24 15:47:02551 group_map_.erase(group_name);
[email protected]ff579d42009-06-24 15:47:02552 }
553}
554
[email protected]d80a4322009-08-14 07:07:49555void ClientSocketPoolBaseHelper::ProcessPendingRequest(
556 const std::string& group_name, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30557 scoped_ptr<const Request> r(RemoveRequestFromQueue(
558 group->pending_requests.begin(), &group->pending_requests));
[email protected]ff579d42009-06-24 15:47:02559
[email protected]d80a4322009-08-14 07:07:49560 int rv = RequestSocket(group_name, r.get());
[email protected]ff579d42009-06-24 15:47:02561
[email protected]2ab05b52009-07-01 23:57:58562 if (rv != ERR_IO_PENDING) {
[email protected]fd7b7c92009-08-20 19:38:30563 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
[email protected]d80a4322009-08-14 07:07:49564 r->callback()->Run(rv);
[email protected]2ab05b52009-07-01 23:57:58565 if (rv != OK) {
566 // |group| may be invalid after the callback, we need to search
567 // |group_map_| again.
568 MaybeOnAvailableSocketSlot(group_name);
569 }
[email protected]d80a4322009-08-14 07:07:49570 } else {
571 r.release();
[email protected]2ab05b52009-07-01 23:57:58572 }
573}
574
[email protected]d80a4322009-08-14 07:07:49575void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58576 ClientSocket* socket,
577 bool reused,
578 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29579 base::TimeDelta idle_time,
[email protected]2ab05b52009-07-01 23:57:58580 Group* group) {
581 DCHECK(socket);
582 handle->set_socket(socket);
583 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29584 handle->set_idle_time(idle_time);
[email protected]211d2172009-07-22 15:48:53585
586 handed_out_socket_count_++;
[email protected]2ab05b52009-07-01 23:57:58587 group->active_socket_count++;
[email protected]ff579d42009-06-24 15:47:02588}
589
[email protected]d80a4322009-08-14 07:07:49590void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57591 ClientSocket* socket, bool used, Group* group) {
592 DCHECK(socket);
593 IdleSocket idle_socket;
594 idle_socket.socket = socket;
595 idle_socket.start_time = base::TimeTicks::Now();
596 idle_socket.used = used;
597
598 group->idle_sockets.push_back(idle_socket);
599 IncrementIdleCount();
600}
601
[email protected]d80a4322009-08-14 07:07:49602void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]5fc08e32009-07-15 17:09:57603 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
604 Group& group = i->second;
[email protected]4d3b05d2010-01-27 21:27:29605 connecting_socket_count_ -= group.jobs.size();
[email protected]5fc08e32009-07-15 17:09:57606 STLDeleteElements(&group.jobs);
607
608 // Delete group if no longer needed.
609 if (group.IsEmpty()) {
[email protected]5fc08e32009-07-15 17:09:57610 group_map_.erase(i++);
611 } else {
612 ++i;
613 }
614 }
615}
616
[email protected]d80a4322009-08-14 07:07:49617bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53618 // Each connecting socket will eventually connect and be handed out.
619 int total = handed_out_socket_count_ + connecting_socket_count_;
620 DCHECK_LE(total, max_sockets_);
621 return total == max_sockets_;
622}
623
[email protected]d80a4322009-08-14 07:07:49624} // namespace internal
625
[email protected]ff579d42009-06-24 15:47:02626} // namespace net