blob: 9413005f5f08975e729ae4b0bbd39eafabc58190 [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,
35 const ClientSocketHandle* key_handle,
[email protected]974ebd62009-08-03 23:14:3436 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3037 Delegate* delegate,
38 LoadLog* load_log)
[email protected]2ab05b52009-07-01 23:57:5839 : group_name_(group_name),
40 key_handle_(key_handle),
[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());
45 DCHECK(key_handle);
46 DCHECK(delegate);
47}
48
[email protected]fd7b7c92009-08-20 19:38:3049ConnectJob::~ConnectJob() {
50 if (delegate_) {
51 // If the delegate was not NULLed, then NotifyDelegateOfCompletion has
52 // not been called yet (hence we are cancelling).
53 LoadLog::AddEvent(load_log_, LoadLog::TYPE_CANCELLED);
54 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
55 }
56}
[email protected]2ab05b52009-07-01 23:57:5857
[email protected]974ebd62009-08-03 23:14:3458int ConnectJob::Connect() {
59 if (timeout_duration_ != base::TimeDelta())
60 timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3061
62 LoadLog::BeginEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
63
64 int rv = ConnectInternal();
65
66 if (rv != ERR_IO_PENDING) {
67 delegate_ = NULL;
68 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
69 }
70
71 return rv;
72}
73
74void ConnectJob::NotifyDelegateOfCompletion(int rv) {
75 // The delegate will delete |this|.
76 Delegate *delegate = delegate_;
77 delegate_ = NULL;
78
79 LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB);
80
81 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:3482}
83
84void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:4085 // Make sure the socket is NULL before calling into |delegate|.
86 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:3087
88 LoadLog::AddEvent(load_log_,
89 LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
90
91 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:3492}
93
[email protected]d80a4322009-08-14 07:07:4994namespace internal {
95
96bool ClientSocketPoolBaseHelper::g_late_binding = false;
97
98ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:5399 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02100 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16101 base::TimeDelta unused_idle_socket_timeout,
102 base::TimeDelta used_idle_socket_timeout,
[email protected]ff579d42009-06-24 15:47:02103 ConnectJobFactory* connect_job_factory)
104 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53105 connecting_socket_count_(0),
106 handed_out_socket_count_(0),
107 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02108 max_sockets_per_group_(max_sockets_per_group),
[email protected]9bf28db2009-08-29 01:35:16109 unused_idle_socket_timeout_(unused_idle_socket_timeout),
110 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]211d2172009-07-22 15:48:53111 may_have_stalled_group_(false),
112 connect_job_factory_(connect_job_factory) {
113 DCHECK_LE(0, max_sockets_per_group);
114 DCHECK_LE(max_sockets_per_group, max_sockets);
115}
[email protected]ff579d42009-06-24 15:47:02116
[email protected]d80a4322009-08-14 07:07:49117ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]5fc08e32009-07-15 17:09:57118 if (g_late_binding)
119 CancelAllConnectJobs();
[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());
125 DCHECK(connect_job_map_.empty());
126}
127
128// InsertRequestIntoQueue inserts the request into the queue based on
129// priority. Highest priorities are closest to the front. Older requests are
130// prioritized over requests of equal priority.
131//
132// static
[email protected]d80a4322009-08-14 07:07:49133void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
134 const Request* r, RequestQueue* pending_requests) {
[email protected]fd7b7c92009-08-20 19:38:30135 LoadLog::BeginEvent(r->load_log(),
136 LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE);
137
[email protected]ff579d42009-06-24 15:47:02138 RequestQueue::iterator it = pending_requests->begin();
[email protected]d80a4322009-08-14 07:07:49139 while (it != pending_requests->end() && r->priority() <= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02140 ++it;
141 pending_requests->insert(it, r);
142}
143
[email protected]fd7b7c92009-08-20 19:38:30144// static
145const ClientSocketPoolBaseHelper::Request*
146ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
147 RequestQueue::iterator it, RequestQueue* pending_requests) {
148 const Request* req = *it;
149
150 LoadLog::EndEvent(req->load_log(),
151 LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE);
152
153 pending_requests->erase(it);
154 return req;
155}
156
[email protected]d80a4322009-08-14 07:07:49157int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02158 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49159 const Request* request) {
160 DCHECK_GE(request->priority(), 0);
161 CompletionCallback* const callback = request->callback();
162 CHECK(callback);
163 ClientSocketHandle* const handle = request->handle();
164 CHECK(handle);
[email protected]ff579d42009-06-24 15:47:02165 Group& group = group_map_[group_name];
166
[email protected]ff579d42009-06-24 15:47:02167 // Can we make another active socket now?
[email protected]211d2172009-07-22 15:48:53168 if (ReachedMaxSocketsLimit() ||
169 !group.HasAvailableSocketSlot(max_sockets_per_group_)) {
170 if (ReachedMaxSocketsLimit()) {
171 // We could check if we really have a stalled group here, but it requires
172 // a scan of all groups, so just flip a flag here, and do the check later.
173 may_have_stalled_group_ = true;
[email protected]fd7b7c92009-08-20 19:38:30174
175 LoadLog::AddEvent(request->load_log(),
176 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
177 } else {
178 LoadLog::AddEvent(request->load_log(),
179 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]211d2172009-07-22 15:48:53180 }
[email protected]d80a4322009-08-14 07:07:49181 InsertRequestIntoQueue(request, &group.pending_requests);
[email protected]ff579d42009-06-24 15:47:02182 return ERR_IO_PENDING;
183 }
184
[email protected]ff579d42009-06-24 15:47:02185 while (!group.idle_sockets.empty()) {
186 IdleSocket idle_socket = group.idle_sockets.back();
187 group.idle_sockets.pop_back();
188 DecrementIdleCount();
189 if (idle_socket.socket->IsConnectedAndIdle()) {
190 // We found one we can reuse!
[email protected]f9d285c2009-08-17 19:54:29191 base::TimeDelta idle_time =
192 base::TimeTicks::Now() - idle_socket.start_time;
[email protected]d80a4322009-08-14 07:07:49193 HandOutSocket(
[email protected]f9d285c2009-08-17 19:54:29194 idle_socket.socket, idle_socket.used, handle, idle_time, &group);
[email protected]ff579d42009-06-24 15:47:02195 return OK;
196 }
197 delete idle_socket.socket;
198 }
199
200 // We couldn't find a socket to reuse, so allocate and connect a new one.
201
[email protected]fd7b7c92009-08-20 19:38:30202 // If we aren't using late binding, the job lines up with a request so
203 // just write directly into the request's LoadLog.
204 scoped_refptr<LoadLog> job_load_log = g_late_binding ?
[email protected]60c4c412009-11-06 19:59:36205 new LoadLog(kMaxNumLoadLogEntries) : request->load_log();
[email protected]fd7b7c92009-08-20 19:38:30206
[email protected]2ab05b52009-07-01 23:57:58207 scoped_ptr<ConnectJob> connect_job(
[email protected]fd7b7c92009-08-20 19:38:30208 connect_job_factory_->NewConnectJob(group_name, *request, this,
209 job_load_log));
[email protected]ff579d42009-06-24 15:47:02210
[email protected]2ab05b52009-07-01 23:57:58211 int rv = connect_job->Connect();
[email protected]fd7b7c92009-08-20 19:38:30212
213 if (g_late_binding && rv != ERR_IO_PENDING && request->load_log())
214 request->load_log()->Append(job_load_log);
215
[email protected]2ab05b52009-07-01 23:57:58216 if (rv == OK) {
217 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]f9d285c2009-08-17 19:54:29218 handle, base::TimeDelta(), &group);
[email protected]2ab05b52009-07-01 23:57:58219 } else if (rv == ERR_IO_PENDING) {
[email protected]211d2172009-07-22 15:48:53220 connecting_socket_count_++;
221
[email protected]5fc08e32009-07-15 17:09:57222 ConnectJob* job = connect_job.release();
223 if (g_late_binding) {
224 CHECK(!ContainsKey(connect_job_map_, handle));
[email protected]d80a4322009-08-14 07:07:49225 InsertRequestIntoQueue(request, &group.pending_requests);
[email protected]5fc08e32009-07-15 17:09:57226 } else {
[email protected]d80a4322009-08-14 07:07:49227 group.connecting_requests[handle] = request;
[email protected]5fc08e32009-07-15 17:09:57228 CHECK(!ContainsKey(connect_job_map_, handle));
229 connect_job_map_[handle] = job;
230 }
231 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]974ebd62009-08-03 23:14:34253 if (g_late_binding &&
254 group.jobs.size() > group.pending_requests.size() + 1) {
255 // TODO(willchan): Cancel the job in the earliest LoadState.
256 RemoveConnectJob(handle, *group.jobs.begin(), &group);
257 OnAvailableSocketSlot(group_name, &group);
258 }
[email protected]ff579d42009-06-24 15:47:02259 return;
260 }
261 }
262
[email protected]5fc08e32009-07-15 17:09:57263 if (!g_late_binding) {
264 // It's invalid to cancel a non-existent request.
265 CHECK(ContainsKey(group.connecting_requests, handle));
[email protected]ff579d42009-06-24 15:47:02266
[email protected]5fc08e32009-07-15 17:09:57267 RequestMap::iterator map_it = group.connecting_requests.find(handle);
268 if (map_it != group.connecting_requests.end()) {
[email protected]fd7b7c92009-08-20 19:38:30269 scoped_refptr<LoadLog> log(map_it->second->load_log());
270 LoadLog::AddEvent(log, LoadLog::TYPE_CANCELLED);
271 LoadLog::EndEvent(log, LoadLog::TYPE_SOCKET_POOL);
[email protected]5fc08e32009-07-15 17:09:57272 RemoveConnectJob(handle, NULL, &group);
273 OnAvailableSocketSlot(group_name, &group);
274 }
[email protected]ff579d42009-06-24 15:47:02275 }
276}
277
[email protected]d80a4322009-08-14 07:07:49278void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
279 ClientSocket* socket) {
[email protected]ff579d42009-06-24 15:47:02280 // Run this asynchronously to allow the caller to finish before we let
281 // another to begin doing work. This also avoids nasty recursion issues.
282 // NOTE: We cannot refer to the handle argument after this method returns.
283 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]d80a4322009-08-14 07:07:49284 this, &ClientSocketPoolBaseHelper::DoReleaseSocket, group_name, socket));
[email protected]ff579d42009-06-24 15:47:02285}
286
[email protected]d80a4322009-08-14 07:07:49287void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02288 CleanupIdleSockets(true);
289}
290
[email protected]d80a4322009-08-14 07:07:49291int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02292 const std::string& group_name) const {
293 GroupMap::const_iterator i = group_map_.find(group_name);
294 CHECK(i != group_map_.end());
295
296 return i->second.idle_sockets.size();
297}
298
[email protected]d80a4322009-08-14 07:07:49299LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02300 const std::string& group_name,
301 const ClientSocketHandle* handle) const {
302 if (!ContainsKey(group_map_, group_name)) {
303 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
304 << " for handle: " << handle;
305 return LOAD_STATE_IDLE;
306 }
307
308 // Can't use operator[] since it is non-const.
309 const Group& group = group_map_.find(group_name)->second;
310
311 // Search connecting_requests for matching handle.
312 RequestMap::const_iterator map_it = group.connecting_requests.find(handle);
313 if (map_it != group.connecting_requests.end()) {
[email protected]ab838892009-06-30 18:49:05314 ConnectJobMap::const_iterator job_it = connect_job_map_.find(handle);
315 if (job_it == connect_job_map_.end()) {
316 NOTREACHED();
317 return LOAD_STATE_IDLE;
318 }
[email protected]46451352009-09-01 14:54:21319 return job_it->second->GetLoadState();
[email protected]ff579d42009-06-24 15:47:02320 }
321
322 // Search pending_requests for matching handle.
323 RequestQueue::const_iterator it = group.pending_requests.begin();
[email protected]5fc08e32009-07-15 17:09:57324 for (size_t i = 0; it != group.pending_requests.end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49325 if ((*it)->handle() == handle) {
[email protected]5fc08e32009-07-15 17:09:57326 if (g_late_binding && i < group.jobs.size()) {
327 LoadState max_state = LOAD_STATE_IDLE;
328 for (ConnectJobSet::const_iterator job_it = group.jobs.begin();
329 job_it != group.jobs.end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21330 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57331 }
332 return max_state;
333 } else {
334 // TODO(wtc): Add a state for being on the wait list.
335 // See http://www.crbug.com/5077.
336 return LOAD_STATE_IDLE;
337 }
[email protected]ff579d42009-06-24 15:47:02338 }
339 }
340
341 NOTREACHED();
342 return LOAD_STATE_IDLE;
343}
344
[email protected]d80a4322009-08-14 07:07:49345bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16346 base::TimeTicks now,
347 base::TimeDelta timeout) const {
348 bool timed_out = (now - start_time) >= timeout;
[email protected]5fc08e32009-07-15 17:09:57349 return timed_out ||
350 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
[email protected]ff579d42009-06-24 15:47:02351}
352
[email protected]d80a4322009-08-14 07:07:49353void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02354 if (idle_socket_count_ == 0)
355 return;
356
357 // Current time value. Retrieving it once at the function start rather than
358 // inside the inner loop, since it shouldn't change by any meaningful amount.
359 base::TimeTicks now = base::TimeTicks::Now();
360
361 GroupMap::iterator i = group_map_.begin();
362 while (i != group_map_.end()) {
363 Group& group = i->second;
364
365 std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
366 while (j != group.idle_sockets.end()) {
[email protected]9bf28db2009-08-29 01:35:16367 base::TimeDelta timeout =
368 j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_;
369 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02370 delete j->socket;
371 j = group.idle_sockets.erase(j);
372 DecrementIdleCount();
373 } else {
374 ++j;
375 }
376 }
377
378 // Delete group if no longer needed.
[email protected]2ab05b52009-07-01 23:57:58379 if (group.IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02380 group_map_.erase(i++);
381 } else {
382 ++i;
383 }
384 }
385}
386
[email protected]d80a4322009-08-14 07:07:49387void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02388 if (++idle_socket_count_ == 1)
389 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
[email protected]d80a4322009-08-14 07:07:49390 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
[email protected]ff579d42009-06-24 15:47:02391}
392
[email protected]d80a4322009-08-14 07:07:49393void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02394 if (--idle_socket_count_ == 0)
395 timer_.Stop();
396}
397
[email protected]d80a4322009-08-14 07:07:49398void ClientSocketPoolBaseHelper::DoReleaseSocket(const std::string& group_name,
399 ClientSocket* socket) {
[email protected]ff579d42009-06-24 15:47:02400 GroupMap::iterator i = group_map_.find(group_name);
401 CHECK(i != group_map_.end());
402
403 Group& group = i->second;
404
[email protected]211d2172009-07-22 15:48:53405 CHECK(handed_out_socket_count_ > 0);
406 handed_out_socket_count_--;
407
[email protected]ff579d42009-06-24 15:47:02408 CHECK(group.active_socket_count > 0);
[email protected]2ab05b52009-07-01 23:57:58409 group.active_socket_count--;
[email protected]ff579d42009-06-24 15:47:02410
411 const bool can_reuse = socket->IsConnectedAndIdle();
412 if (can_reuse) {
[email protected]5fc08e32009-07-15 17:09:57413 AddIdleSocket(socket, true /* used socket */, &group);
[email protected]ff579d42009-06-24 15:47:02414 } else {
415 delete socket;
416 }
417
[email protected]2ab05b52009-07-01 23:57:58418 OnAvailableSocketSlot(group_name, &group);
[email protected]ff579d42009-06-24 15:47:02419}
420
[email protected]211d2172009-07-22 15:48:53421// Search for the highest priority pending request, amongst the groups that
422// are not at the |max_sockets_per_group_| limit. Note: for requests with
423// the same priority, the winner is based on group hash ordering (and not
424// insertion order).
[email protected]d80a4322009-08-14 07:07:49425int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
426 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53427 Group* top_group = NULL;
428 const std::string* top_group_name = NULL;
429 int stalled_group_count = 0;
430 for (GroupMap::iterator i = group_map_.begin();
431 i != group_map_.end(); ++i) {
432 Group& group = i->second;
433 const RequestQueue& queue = group.pending_requests;
434 if (queue.empty())
435 continue;
436 bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
437 if (has_slot)
438 stalled_group_count++;
439 bool has_higher_priority = !top_group ||
440 group.TopPendingPriority() > top_group->TopPendingPriority();
441 if (has_slot && has_higher_priority) {
442 top_group = &group;
443 top_group_name = &i->first;
444 }
445 }
446 if (top_group) {
447 *group = top_group;
448 *group_name = *top_group_name;
449 }
450 return stalled_group_count;
451}
452
[email protected]d80a4322009-08-14 07:07:49453void ClientSocketPoolBaseHelper::OnConnectJobComplete(
454 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58455 DCHECK_NE(ERR_IO_PENDING, result);
456 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02457 GroupMap::iterator group_it = group_map_.find(group_name);
458 CHECK(group_it != group_map_.end());
459 Group& group = group_it->second;
460
[email protected]5fc08e32009-07-15 17:09:57461 const ClientSocketHandle* const key_handle = job->key_handle();
462 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02463
[email protected]5fc08e32009-07-15 17:09:57464 if (g_late_binding) {
[email protected]fd7b7c92009-08-20 19:38:30465 scoped_refptr<LoadLog> job_load_log(job->load_log());
[email protected]5fc08e32009-07-15 17:09:57466 RemoveConnectJob(key_handle, job, &group);
467
[email protected]fd7b7c92009-08-20 19:38:30468 scoped_ptr<const Request> r;
469 if (!group.pending_requests.empty()) {
470 r.reset(RemoveRequestFromQueue(
471 group.pending_requests.begin(), &group.pending_requests));
472
473 if (r->load_log())
474 r->load_log()->Append(job_load_log);
475
476 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
477 }
478
[email protected]5fc08e32009-07-15 17:09:57479 if (result == OK) {
480 DCHECK(socket.get());
[email protected]fd7b7c92009-08-20 19:38:30481 if (r.get()) {
[email protected]5fc08e32009-07-15 17:09:57482 HandOutSocket(
[email protected]f9d285c2009-08-17 19:54:29483 socket.release(), false /* unused socket */, r->handle(),
484 base::TimeDelta(), &group);
[email protected]d80a4322009-08-14 07:07:49485 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57486 } else {
487 AddIdleSocket(socket.release(), false /* unused socket */, &group);
488 OnAvailableSocketSlot(group_name, &group);
489 }
490 } else {
491 DCHECK(!socket.get());
[email protected]fd7b7c92009-08-20 19:38:30492 if (r.get())
[email protected]d80a4322009-08-14 07:07:49493 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57494 MaybeOnAvailableSocketSlot(group_name);
495 }
496
497 return;
498 }
499
500 RequestMap* request_map = &group.connecting_requests;
501 RequestMap::iterator it = request_map->find(key_handle);
[email protected]ff579d42009-06-24 15:47:02502 CHECK(it != request_map->end());
[email protected]d80a4322009-08-14 07:07:49503 const Request* request = it->second;
504 ClientSocketHandle* const handle = request->handle();
505 CompletionCallback* const callback = request->callback();
[email protected]ff579d42009-06-24 15:47:02506
[email protected]fd7b7c92009-08-20 19:38:30507 LoadLog::EndEvent(request->load_log(), LoadLog::TYPE_SOCKET_POOL);
508
[email protected]5fc08e32009-07-15 17:09:57509 RemoveConnectJob(key_handle, job, &group);
[email protected]2ab05b52009-07-01 23:57:58510
511 if (result != OK) {
[email protected]8e12ae02009-07-02 16:15:04512 DCHECK(!socket.get());
[email protected]2ab05b52009-07-01 23:57:58513 callback->Run(result); // |group| is not necessarily valid after this.
514 // |group| may be invalid after the callback, we need to search
515 // |group_map_| again.
516 MaybeOnAvailableSocketSlot(group_name);
[email protected]ff579d42009-06-24 15:47:02517 } else {
[email protected]5fc08e32009-07-15 17:09:57518 DCHECK(socket.get());
[email protected]f9d285c2009-08-17 19:54:29519 HandOutSocket(socket.release(), false /* not reused */, handle,
520 base::TimeDelta(), &group);
[email protected]2ab05b52009-07-01 23:57:58521 callback->Run(result);
[email protected]ff579d42009-06-24 15:47:02522 }
[email protected]ff579d42009-06-24 15:47:02523}
524
[email protected]d80a4322009-08-14 07:07:49525void ClientSocketPoolBaseHelper::EnableLateBindingOfSockets(bool enabled) {
[email protected]5fc08e32009-07-15 17:09:57526 g_late_binding = enabled;
527}
528
[email protected]d80a4322009-08-14 07:07:49529void ClientSocketPoolBaseHelper::RemoveConnectJob(
[email protected]974ebd62009-08-03 23:14:34530 const ClientSocketHandle* handle, const ConnectJob *job, Group* group) {
[email protected]211d2172009-07-22 15:48:53531 CHECK(connecting_socket_count_ > 0);
532 connecting_socket_count_--;
533
[email protected]5fc08e32009-07-15 17:09:57534 if (g_late_binding) {
535 DCHECK(job);
536 delete job;
537 } else {
538 ConnectJobMap::iterator it = connect_job_map_.find(handle);
539 CHECK(it != connect_job_map_.end());
540 job = it->second;
541 delete job;
542 connect_job_map_.erase(it);
[email protected]d80a4322009-08-14 07:07:49543 RequestMap::iterator map_it = group->connecting_requests.find(handle);
544 CHECK(map_it != group->connecting_requests.end());
545 const Request* request = map_it->second;
546 delete request;
547 group->connecting_requests.erase(map_it);
[email protected]5fc08e32009-07-15 17:09:57548 }
549
550 if (group) {
551 DCHECK(ContainsKey(group->jobs, job));
552 group->jobs.erase(job);
553 }
[email protected]ff579d42009-06-24 15:47:02554}
555
[email protected]d80a4322009-08-14 07:07:49556void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot(
[email protected]2ab05b52009-07-01 23:57:58557 const std::string& group_name) {
558 GroupMap::iterator it = group_map_.find(group_name);
559 if (it != group_map_.end()) {
560 Group& group = it->second;
561 if (group.HasAvailableSocketSlot(max_sockets_per_group_))
562 OnAvailableSocketSlot(group_name, &group);
563 }
564}
[email protected]ff579d42009-06-24 15:47:02565
[email protected]d80a4322009-08-14 07:07:49566void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
567 const std::string& group_name, Group* group) {
[email protected]211d2172009-07-22 15:48:53568 if (may_have_stalled_group_) {
569 std::string top_group_name;
[email protected]bed37d442009-08-20 19:58:20570 Group* top_group = NULL;
[email protected]211d2172009-07-22 15:48:53571 int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
572 if (stalled_group_count <= 1)
573 may_have_stalled_group_ = false;
574 if (stalled_group_count >= 1)
575 ProcessPendingRequest(top_group_name, top_group);
576 } else if (!group->pending_requests.empty()) {
[email protected]ff579d42009-06-24 15:47:02577 ProcessPendingRequest(group_name, group);
578 // |group| may no longer be valid after this point. Be careful not to
579 // access it again.
[email protected]2ab05b52009-07-01 23:57:58580 } else if (group->IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02581 // Delete |group| if no longer needed. |group| will no longer be valid.
[email protected]ff579d42009-06-24 15:47:02582 group_map_.erase(group_name);
[email protected]ff579d42009-06-24 15:47:02583 }
584}
585
[email protected]d80a4322009-08-14 07:07:49586void ClientSocketPoolBaseHelper::ProcessPendingRequest(
587 const std::string& group_name, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30588 scoped_ptr<const Request> r(RemoveRequestFromQueue(
589 group->pending_requests.begin(), &group->pending_requests));
[email protected]ff579d42009-06-24 15:47:02590
[email protected]d80a4322009-08-14 07:07:49591 int rv = RequestSocket(group_name, r.get());
[email protected]ff579d42009-06-24 15:47:02592
[email protected]2ab05b52009-07-01 23:57:58593 if (rv != ERR_IO_PENDING) {
[email protected]fd7b7c92009-08-20 19:38:30594 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
[email protected]d80a4322009-08-14 07:07:49595 r->callback()->Run(rv);
[email protected]2ab05b52009-07-01 23:57:58596 if (rv != OK) {
597 // |group| may be invalid after the callback, we need to search
598 // |group_map_| again.
599 MaybeOnAvailableSocketSlot(group_name);
600 }
[email protected]d80a4322009-08-14 07:07:49601 } else {
602 r.release();
[email protected]2ab05b52009-07-01 23:57:58603 }
604}
605
[email protected]d80a4322009-08-14 07:07:49606void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58607 ClientSocket* socket,
608 bool reused,
609 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29610 base::TimeDelta idle_time,
[email protected]2ab05b52009-07-01 23:57:58611 Group* group) {
612 DCHECK(socket);
613 handle->set_socket(socket);
614 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29615 handle->set_idle_time(idle_time);
[email protected]211d2172009-07-22 15:48:53616
617 handed_out_socket_count_++;
[email protected]2ab05b52009-07-01 23:57:58618 group->active_socket_count++;
[email protected]ff579d42009-06-24 15:47:02619}
620
[email protected]d80a4322009-08-14 07:07:49621void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57622 ClientSocket* socket, bool used, Group* group) {
623 DCHECK(socket);
624 IdleSocket idle_socket;
625 idle_socket.socket = socket;
626 idle_socket.start_time = base::TimeTicks::Now();
627 idle_socket.used = used;
628
629 group->idle_sockets.push_back(idle_socket);
630 IncrementIdleCount();
631}
632
[email protected]d80a4322009-08-14 07:07:49633void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]5fc08e32009-07-15 17:09:57634 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
635 Group& group = i->second;
636 STLDeleteElements(&group.jobs);
637
638 // Delete group if no longer needed.
639 if (group.IsEmpty()) {
[email protected]5fc08e32009-07-15 17:09:57640 group_map_.erase(i++);
641 } else {
642 ++i;
643 }
644 }
645}
646
[email protected]d80a4322009-08-14 07:07:49647bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53648 // Each connecting socket will eventually connect and be handed out.
649 int total = handed_out_socket_count_ + connecting_socket_count_;
650 DCHECK_LE(total, max_sockets_);
651 return total == max_sockets_;
652}
653
[email protected]d80a4322009-08-14 07:07:49654} // namespace internal
655
656void EnableLateBindingOfSockets(bool enabled) {
657 internal::ClientSocketPoolBaseHelper::EnableLateBindingOfSockets(enabled);
658}
659
[email protected]ff579d42009-06-24 15:47:02660} // namespace net