blob: 9b14f4b33f4a1d9d17d12a7f6f7efce24e75b0cf [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,
99 const scoped_refptr<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]2ab05b52009-07-01 23:57:58398 OnAvailableSocketSlot(group_name, &group);
[email protected]ff579d42009-06-24 15:47:02399}
400
[email protected]211d2172009-07-22 15:48:53401// Search for the highest priority pending request, amongst the groups that
402// are not at the |max_sockets_per_group_| limit. Note: for requests with
403// the same priority, the winner is based on group hash ordering (and not
404// insertion order).
[email protected]d80a4322009-08-14 07:07:49405int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
406 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53407 Group* top_group = NULL;
408 const std::string* top_group_name = NULL;
409 int stalled_group_count = 0;
410 for (GroupMap::iterator i = group_map_.begin();
411 i != group_map_.end(); ++i) {
412 Group& group = i->second;
413 const RequestQueue& queue = group.pending_requests;
414 if (queue.empty())
415 continue;
416 bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
417 if (has_slot)
418 stalled_group_count++;
419 bool has_higher_priority = !top_group ||
[email protected]ac790b42009-12-02 04:31:31420 group.TopPendingPriority() < top_group->TopPendingPriority();
[email protected]211d2172009-07-22 15:48:53421 if (has_slot && has_higher_priority) {
422 top_group = &group;
423 top_group_name = &i->first;
424 }
425 }
426 if (top_group) {
427 *group = top_group;
428 *group_name = *top_group_name;
429 }
430 return stalled_group_count;
431}
432
[email protected]d80a4322009-08-14 07:07:49433void ClientSocketPoolBaseHelper::OnConnectJobComplete(
434 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58435 DCHECK_NE(ERR_IO_PENDING, result);
436 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02437 GroupMap::iterator group_it = group_map_.find(group_name);
438 CHECK(group_it != group_map_.end());
439 Group& group = group_it->second;
440
[email protected]5fc08e32009-07-15 17:09:57441 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02442
[email protected]4d3b05d2010-01-27 21:27:29443 scoped_refptr<LoadLog> job_load_log(job->load_log());
444 RemoveConnectJob(job, &group);
[email protected]5fc08e32009-07-15 17:09:57445
[email protected]4d3b05d2010-01-27 21:27:29446 LoadLog::EndEvent(job_load_log, LoadLog::TYPE_SOCKET_POOL);
447
448 if (result == OK) {
449 DCHECK(socket.get());
[email protected]fd7b7c92009-08-20 19:38:30450 if (!group.pending_requests.empty()) {
[email protected]4d3b05d2010-01-27 21:27:29451 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]fd7b7c92009-08-20 19:38:30452 group.pending_requests.begin(), &group.pending_requests));
[email protected]fd7b7c92009-08-20 19:38:30453 if (r->load_log())
454 r->load_log()->Append(job_load_log);
[email protected]4d3b05d2010-01-27 21:27:29455 HandOutSocket(
456 socket.release(), false /* unused socket */, r->handle(),
457 base::TimeDelta(), &group);
458 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57459 } else {
[email protected]4d3b05d2010-01-27 21:27:29460 AddIdleSocket(socket.release(), false /* unused socket */, &group);
461 OnAvailableSocketSlot(group_name, &group);
[email protected]5fc08e32009-07-15 17:09:57462 }
[email protected]94c20472010-01-14 08:14:36463 } else {
[email protected]4d3b05d2010-01-27 21:27:29464 DCHECK(!socket.get());
465 if (!group.pending_requests.empty()) {
466 scoped_ptr<const Request> r(RemoveRequestFromQueue(
467 group.pending_requests.begin(), &group.pending_requests));
468 if (r->load_log())
469 r->load_log()->Append(job_load_log);
470 r->callback()->Run(result);
471 }
472 MaybeOnAvailableSocketSlot(group_name);
[email protected]ff579d42009-06-24 15:47:02473 }
[email protected]ff579d42009-06-24 15:47:02474}
475
[email protected]100d5fb92009-12-21 21:08:35476void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
477 CloseIdleSockets();
478}
479
[email protected]4d3b05d2010-01-27 21:27:29480void ClientSocketPoolBaseHelper::RemoveConnectJob(const ConnectJob *job,
481 Group* group) {
[email protected]211d2172009-07-22 15:48:53482 CHECK(connecting_socket_count_ > 0);
483 connecting_socket_count_--;
484
[email protected]4d3b05d2010-01-27 21:27:29485 DCHECK(job);
486 delete job;
[email protected]5fc08e32009-07-15 17:09:57487
488 if (group) {
489 DCHECK(ContainsKey(group->jobs, job));
490 group->jobs.erase(job);
491 }
[email protected]ff579d42009-06-24 15:47:02492}
493
[email protected]d80a4322009-08-14 07:07:49494void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot(
[email protected]2ab05b52009-07-01 23:57:58495 const std::string& group_name) {
496 GroupMap::iterator it = group_map_.find(group_name);
497 if (it != group_map_.end()) {
498 Group& group = it->second;
499 if (group.HasAvailableSocketSlot(max_sockets_per_group_))
500 OnAvailableSocketSlot(group_name, &group);
501 }
502}
[email protected]ff579d42009-06-24 15:47:02503
[email protected]d80a4322009-08-14 07:07:49504void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
505 const std::string& group_name, Group* group) {
[email protected]211d2172009-07-22 15:48:53506 if (may_have_stalled_group_) {
507 std::string top_group_name;
[email protected]bed37d442009-08-20 19:58:20508 Group* top_group = NULL;
[email protected]211d2172009-07-22 15:48:53509 int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
510 if (stalled_group_count <= 1)
511 may_have_stalled_group_ = false;
512 if (stalled_group_count >= 1)
513 ProcessPendingRequest(top_group_name, top_group);
514 } else if (!group->pending_requests.empty()) {
[email protected]ff579d42009-06-24 15:47:02515 ProcessPendingRequest(group_name, group);
516 // |group| may no longer be valid after this point. Be careful not to
517 // access it again.
[email protected]2ab05b52009-07-01 23:57:58518 } else if (group->IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02519 // Delete |group| if no longer needed. |group| will no longer be valid.
[email protected]ff579d42009-06-24 15:47:02520 group_map_.erase(group_name);
[email protected]ff579d42009-06-24 15:47:02521 }
522}
523
[email protected]d80a4322009-08-14 07:07:49524void ClientSocketPoolBaseHelper::ProcessPendingRequest(
525 const std::string& group_name, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30526 scoped_ptr<const Request> r(RemoveRequestFromQueue(
527 group->pending_requests.begin(), &group->pending_requests));
[email protected]ff579d42009-06-24 15:47:02528
[email protected]d80a4322009-08-14 07:07:49529 int rv = RequestSocket(group_name, r.get());
[email protected]ff579d42009-06-24 15:47:02530
[email protected]2ab05b52009-07-01 23:57:58531 if (rv != ERR_IO_PENDING) {
[email protected]fd7b7c92009-08-20 19:38:30532 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
[email protected]d80a4322009-08-14 07:07:49533 r->callback()->Run(rv);
[email protected]2ab05b52009-07-01 23:57:58534 if (rv != OK) {
535 // |group| may be invalid after the callback, we need to search
536 // |group_map_| again.
537 MaybeOnAvailableSocketSlot(group_name);
538 }
[email protected]d80a4322009-08-14 07:07:49539 } else {
540 r.release();
[email protected]2ab05b52009-07-01 23:57:58541 }
542}
543
[email protected]d80a4322009-08-14 07:07:49544void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58545 ClientSocket* socket,
546 bool reused,
547 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29548 base::TimeDelta idle_time,
[email protected]2ab05b52009-07-01 23:57:58549 Group* group) {
550 DCHECK(socket);
551 handle->set_socket(socket);
552 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29553 handle->set_idle_time(idle_time);
[email protected]211d2172009-07-22 15:48:53554
555 handed_out_socket_count_++;
[email protected]2ab05b52009-07-01 23:57:58556 group->active_socket_count++;
[email protected]ff579d42009-06-24 15:47:02557}
558
[email protected]d80a4322009-08-14 07:07:49559void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57560 ClientSocket* socket, bool used, Group* group) {
561 DCHECK(socket);
562 IdleSocket idle_socket;
563 idle_socket.socket = socket;
564 idle_socket.start_time = base::TimeTicks::Now();
565 idle_socket.used = used;
566
567 group->idle_sockets.push_back(idle_socket);
568 IncrementIdleCount();
569}
570
[email protected]d80a4322009-08-14 07:07:49571void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]5fc08e32009-07-15 17:09:57572 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
573 Group& group = i->second;
[email protected]4d3b05d2010-01-27 21:27:29574 connecting_socket_count_ -= group.jobs.size();
[email protected]5fc08e32009-07-15 17:09:57575 STLDeleteElements(&group.jobs);
576
577 // Delete group if no longer needed.
578 if (group.IsEmpty()) {
[email protected]5fc08e32009-07-15 17:09:57579 group_map_.erase(i++);
580 } else {
581 ++i;
582 }
583 }
584}
585
[email protected]d80a4322009-08-14 07:07:49586bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53587 // Each connecting socket will eventually connect and be handed out.
588 int total = handed_out_socket_count_ + connecting_socket_count_;
589 DCHECK_LE(total, max_sockets_);
590 return total == max_sockets_;
591}
592
[email protected]d80a4322009-08-14 07:07:49593} // namespace internal
594
[email protected]ff579d42009-06-24 15:47:02595} // namespace net