blob: 1dc1180f0d10972d5c1b621fca5f09a049096946 [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]ff579d42009-06-24 15:47:0227} // namespace
28
29namespace net {
30
[email protected]2ab05b52009-07-01 23:57:5831ConnectJob::ConnectJob(const std::string& group_name,
32 const ClientSocketHandle* key_handle,
[email protected]974ebd62009-08-03 23:14:3433 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3034 Delegate* delegate,
35 LoadLog* load_log)
[email protected]2ab05b52009-07-01 23:57:5836 : group_name_(group_name),
37 key_handle_(key_handle),
[email protected]974ebd62009-08-03 23:14:3438 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5839 delegate_(delegate),
[email protected]fd7b7c92009-08-20 19:38:3040 load_log_(load_log) {
[email protected]2ab05b52009-07-01 23:57:5841 DCHECK(!group_name.empty());
42 DCHECK(key_handle);
43 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
93bool ClientSocketPoolBaseHelper::g_late_binding = false;
94
95ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:5396 int max_sockets,
[email protected]ff579d42009-06-24 15:47:0297 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:1698 base::TimeDelta unused_idle_socket_timeout,
99 base::TimeDelta used_idle_socket_timeout,
[email protected]ff579d42009-06-24 15:47:02100 ConnectJobFactory* connect_job_factory)
101 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53102 connecting_socket_count_(0),
103 handed_out_socket_count_(0),
104 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02105 max_sockets_per_group_(max_sockets_per_group),
[email protected]9bf28db2009-08-29 01:35:16106 unused_idle_socket_timeout_(unused_idle_socket_timeout),
107 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]211d2172009-07-22 15:48:53108 may_have_stalled_group_(false),
109 connect_job_factory_(connect_job_factory) {
110 DCHECK_LE(0, max_sockets_per_group);
111 DCHECK_LE(max_sockets_per_group, max_sockets);
112}
[email protected]ff579d42009-06-24 15:47:02113
[email protected]d80a4322009-08-14 07:07:49114ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]5fc08e32009-07-15 17:09:57115 if (g_late_binding)
116 CancelAllConnectJobs();
[email protected]ff579d42009-06-24 15:47:02117 // Clean up any idle sockets. Assert that we have no remaining active
118 // sockets or pending requests. They should have all been cleaned up prior
119 // to the manager being destroyed.
120 CloseIdleSockets();
121 DCHECK(group_map_.empty());
122 DCHECK(connect_job_map_.empty());
123}
124
125// InsertRequestIntoQueue inserts the request into the queue based on
126// priority. Highest priorities are closest to the front. Older requests are
127// prioritized over requests of equal priority.
128//
129// static
[email protected]d80a4322009-08-14 07:07:49130void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
131 const Request* r, RequestQueue* pending_requests) {
[email protected]fd7b7c92009-08-20 19:38:30132 LoadLog::BeginEvent(r->load_log(),
133 LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE);
134
[email protected]ff579d42009-06-24 15:47:02135 RequestQueue::iterator it = pending_requests->begin();
[email protected]d80a4322009-08-14 07:07:49136 while (it != pending_requests->end() && r->priority() <= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02137 ++it;
138 pending_requests->insert(it, r);
139}
140
[email protected]fd7b7c92009-08-20 19:38:30141// static
142const ClientSocketPoolBaseHelper::Request*
143ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
144 RequestQueue::iterator it, RequestQueue* pending_requests) {
145 const Request* req = *it;
146
147 LoadLog::EndEvent(req->load_log(),
148 LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE);
149
150 pending_requests->erase(it);
151 return req;
152}
153
[email protected]d80a4322009-08-14 07:07:49154int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02155 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49156 const Request* request) {
157 DCHECK_GE(request->priority(), 0);
158 CompletionCallback* const callback = request->callback();
159 CHECK(callback);
160 ClientSocketHandle* const handle = request->handle();
161 CHECK(handle);
[email protected]ff579d42009-06-24 15:47:02162 Group& group = group_map_[group_name];
163
[email protected]ff579d42009-06-24 15:47:02164 // Can we make another active socket now?
[email protected]211d2172009-07-22 15:48:53165 if (ReachedMaxSocketsLimit() ||
166 !group.HasAvailableSocketSlot(max_sockets_per_group_)) {
167 if (ReachedMaxSocketsLimit()) {
168 // We could check if we really have a stalled group here, but it requires
169 // a scan of all groups, so just flip a flag here, and do the check later.
170 may_have_stalled_group_ = true;
[email protected]fd7b7c92009-08-20 19:38:30171
172 LoadLog::AddEvent(request->load_log(),
173 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
174 } else {
175 LoadLog::AddEvent(request->load_log(),
176 LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]211d2172009-07-22 15:48:53177 }
[email protected]d80a4322009-08-14 07:07:49178 InsertRequestIntoQueue(request, &group.pending_requests);
[email protected]ff579d42009-06-24 15:47:02179 return ERR_IO_PENDING;
180 }
181
[email protected]ff579d42009-06-24 15:47:02182 while (!group.idle_sockets.empty()) {
183 IdleSocket idle_socket = group.idle_sockets.back();
184 group.idle_sockets.pop_back();
185 DecrementIdleCount();
186 if (idle_socket.socket->IsConnectedAndIdle()) {
187 // We found one we can reuse!
[email protected]f9d285c2009-08-17 19:54:29188 base::TimeDelta idle_time =
189 base::TimeTicks::Now() - idle_socket.start_time;
[email protected]d80a4322009-08-14 07:07:49190 HandOutSocket(
[email protected]f9d285c2009-08-17 19:54:29191 idle_socket.socket, idle_socket.used, handle, idle_time, &group);
[email protected]ff579d42009-06-24 15:47:02192 return OK;
193 }
194 delete idle_socket.socket;
195 }
196
197 // We couldn't find a socket to reuse, so allocate and connect a new one.
198
[email protected]fd7b7c92009-08-20 19:38:30199 // If we aren't using late binding, the job lines up with a request so
200 // just write directly into the request's LoadLog.
201 scoped_refptr<LoadLog> job_load_log = g_late_binding ?
202 new LoadLog : request->load_log();
203
[email protected]2ab05b52009-07-01 23:57:58204 scoped_ptr<ConnectJob> connect_job(
[email protected]fd7b7c92009-08-20 19:38:30205 connect_job_factory_->NewConnectJob(group_name, *request, this,
206 job_load_log));
[email protected]ff579d42009-06-24 15:47:02207
[email protected]2ab05b52009-07-01 23:57:58208 int rv = connect_job->Connect();
[email protected]fd7b7c92009-08-20 19:38:30209
210 if (g_late_binding && rv != ERR_IO_PENDING && request->load_log())
211 request->load_log()->Append(job_load_log);
212
[email protected]2ab05b52009-07-01 23:57:58213 if (rv == OK) {
214 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]f9d285c2009-08-17 19:54:29215 handle, base::TimeDelta(), &group);
[email protected]2ab05b52009-07-01 23:57:58216 } else if (rv == ERR_IO_PENDING) {
[email protected]211d2172009-07-22 15:48:53217 connecting_socket_count_++;
218
[email protected]5fc08e32009-07-15 17:09:57219 ConnectJob* job = connect_job.release();
220 if (g_late_binding) {
221 CHECK(!ContainsKey(connect_job_map_, handle));
[email protected]d80a4322009-08-14 07:07:49222 InsertRequestIntoQueue(request, &group.pending_requests);
[email protected]5fc08e32009-07-15 17:09:57223 } else {
[email protected]d80a4322009-08-14 07:07:49224 group.connecting_requests[handle] = request;
[email protected]5fc08e32009-07-15 17:09:57225 CHECK(!ContainsKey(connect_job_map_, handle));
226 connect_job_map_[handle] = job;
227 }
228 group.jobs.insert(job);
[email protected]2b7523d2009-07-29 20:29:23229 } else if (group.IsEmpty()) {
230 group_map_.erase(group_name);
[email protected]2ab05b52009-07-01 23:57:58231 }
[email protected]ff579d42009-06-24 15:47:02232
[email protected]2ab05b52009-07-01 23:57:58233 return rv;
[email protected]ff579d42009-06-24 15:47:02234}
235
[email protected]d80a4322009-08-14 07:07:49236void ClientSocketPoolBaseHelper::CancelRequest(
237 const std::string& group_name, const ClientSocketHandle* handle) {
[email protected]ff579d42009-06-24 15:47:02238 CHECK(ContainsKey(group_map_, group_name));
239
240 Group& group = group_map_[group_name];
241
[email protected]ff579d42009-06-24 15:47:02242 // Search pending_requests for matching handle.
243 RequestQueue::iterator it = group.pending_requests.begin();
244 for (; it != group.pending_requests.end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49245 if ((*it)->handle() == handle) {
[email protected]fd7b7c92009-08-20 19:38:30246 const Request* req = RemoveRequestFromQueue(it, &group.pending_requests);
247 LoadLog::AddEvent(req->load_log(), LoadLog::TYPE_CANCELLED);
248 LoadLog::EndEvent(req->load_log(), LoadLog::TYPE_SOCKET_POOL);
249 delete req;
[email protected]974ebd62009-08-03 23:14:34250 if (g_late_binding &&
251 group.jobs.size() > group.pending_requests.size() + 1) {
252 // TODO(willchan): Cancel the job in the earliest LoadState.
253 RemoveConnectJob(handle, *group.jobs.begin(), &group);
254 OnAvailableSocketSlot(group_name, &group);
255 }
[email protected]ff579d42009-06-24 15:47:02256 return;
257 }
258 }
259
[email protected]5fc08e32009-07-15 17:09:57260 if (!g_late_binding) {
261 // It's invalid to cancel a non-existent request.
262 CHECK(ContainsKey(group.connecting_requests, handle));
[email protected]ff579d42009-06-24 15:47:02263
[email protected]5fc08e32009-07-15 17:09:57264 RequestMap::iterator map_it = group.connecting_requests.find(handle);
265 if (map_it != group.connecting_requests.end()) {
[email protected]fd7b7c92009-08-20 19:38:30266 scoped_refptr<LoadLog> log(map_it->second->load_log());
267 LoadLog::AddEvent(log, LoadLog::TYPE_CANCELLED);
268 LoadLog::EndEvent(log, LoadLog::TYPE_SOCKET_POOL);
[email protected]5fc08e32009-07-15 17:09:57269 RemoveConnectJob(handle, NULL, &group);
270 OnAvailableSocketSlot(group_name, &group);
271 }
[email protected]ff579d42009-06-24 15:47:02272 }
273}
274
[email protected]d80a4322009-08-14 07:07:49275void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
276 ClientSocket* socket) {
[email protected]ff579d42009-06-24 15:47:02277 // Run this asynchronously to allow the caller to finish before we let
278 // another to begin doing work. This also avoids nasty recursion issues.
279 // NOTE: We cannot refer to the handle argument after this method returns.
280 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
[email protected]d80a4322009-08-14 07:07:49281 this, &ClientSocketPoolBaseHelper::DoReleaseSocket, group_name, socket));
[email protected]ff579d42009-06-24 15:47:02282}
283
[email protected]d80a4322009-08-14 07:07:49284void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02285 CleanupIdleSockets(true);
286}
287
[email protected]d80a4322009-08-14 07:07:49288int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02289 const std::string& group_name) const {
290 GroupMap::const_iterator i = group_map_.find(group_name);
291 CHECK(i != group_map_.end());
292
293 return i->second.idle_sockets.size();
294}
295
[email protected]d80a4322009-08-14 07:07:49296LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02297 const std::string& group_name,
298 const ClientSocketHandle* handle) const {
299 if (!ContainsKey(group_map_, group_name)) {
300 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
301 << " for handle: " << handle;
302 return LOAD_STATE_IDLE;
303 }
304
305 // Can't use operator[] since it is non-const.
306 const Group& group = group_map_.find(group_name)->second;
307
308 // Search connecting_requests for matching handle.
309 RequestMap::const_iterator map_it = group.connecting_requests.find(handle);
310 if (map_it != group.connecting_requests.end()) {
[email protected]ab838892009-06-30 18:49:05311 ConnectJobMap::const_iterator job_it = connect_job_map_.find(handle);
312 if (job_it == connect_job_map_.end()) {
313 NOTREACHED();
314 return LOAD_STATE_IDLE;
315 }
[email protected]46451352009-09-01 14:54:21316 return job_it->second->GetLoadState();
[email protected]ff579d42009-06-24 15:47:02317 }
318
319 // Search pending_requests for matching handle.
320 RequestQueue::const_iterator it = group.pending_requests.begin();
[email protected]5fc08e32009-07-15 17:09:57321 for (size_t i = 0; it != group.pending_requests.end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49322 if ((*it)->handle() == handle) {
[email protected]5fc08e32009-07-15 17:09:57323 if (g_late_binding && i < group.jobs.size()) {
324 LoadState max_state = LOAD_STATE_IDLE;
325 for (ConnectJobSet::const_iterator job_it = group.jobs.begin();
326 job_it != group.jobs.end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21327 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57328 }
329 return max_state;
330 } else {
331 // TODO(wtc): Add a state for being on the wait list.
332 // See http://www.crbug.com/5077.
333 return LOAD_STATE_IDLE;
334 }
[email protected]ff579d42009-06-24 15:47:02335 }
336 }
337
338 NOTREACHED();
339 return LOAD_STATE_IDLE;
340}
341
[email protected]d80a4322009-08-14 07:07:49342bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16343 base::TimeTicks now,
344 base::TimeDelta timeout) const {
345 bool timed_out = (now - start_time) >= timeout;
[email protected]5fc08e32009-07-15 17:09:57346 return timed_out ||
347 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
[email protected]ff579d42009-06-24 15:47:02348}
349
[email protected]d80a4322009-08-14 07:07:49350void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02351 if (idle_socket_count_ == 0)
352 return;
353
354 // Current time value. Retrieving it once at the function start rather than
355 // inside the inner loop, since it shouldn't change by any meaningful amount.
356 base::TimeTicks now = base::TimeTicks::Now();
357
358 GroupMap::iterator i = group_map_.begin();
359 while (i != group_map_.end()) {
360 Group& group = i->second;
361
362 std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
363 while (j != group.idle_sockets.end()) {
[email protected]9bf28db2009-08-29 01:35:16364 base::TimeDelta timeout =
365 j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_;
366 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02367 delete j->socket;
368 j = group.idle_sockets.erase(j);
369 DecrementIdleCount();
370 } else {
371 ++j;
372 }
373 }
374
375 // Delete group if no longer needed.
[email protected]2ab05b52009-07-01 23:57:58376 if (group.IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02377 group_map_.erase(i++);
378 } else {
379 ++i;
380 }
381 }
382}
383
[email protected]d80a4322009-08-14 07:07:49384void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02385 if (++idle_socket_count_ == 1)
386 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
[email protected]d80a4322009-08-14 07:07:49387 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
[email protected]ff579d42009-06-24 15:47:02388}
389
[email protected]d80a4322009-08-14 07:07:49390void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02391 if (--idle_socket_count_ == 0)
392 timer_.Stop();
393}
394
[email protected]d80a4322009-08-14 07:07:49395void ClientSocketPoolBaseHelper::DoReleaseSocket(const std::string& group_name,
396 ClientSocket* socket) {
[email protected]ff579d42009-06-24 15:47:02397 GroupMap::iterator i = group_map_.find(group_name);
398 CHECK(i != group_map_.end());
399
400 Group& group = i->second;
401
[email protected]211d2172009-07-22 15:48:53402 CHECK(handed_out_socket_count_ > 0);
403 handed_out_socket_count_--;
404
[email protected]ff579d42009-06-24 15:47:02405 CHECK(group.active_socket_count > 0);
[email protected]2ab05b52009-07-01 23:57:58406 group.active_socket_count--;
[email protected]ff579d42009-06-24 15:47:02407
408 const bool can_reuse = socket->IsConnectedAndIdle();
409 if (can_reuse) {
[email protected]5fc08e32009-07-15 17:09:57410 AddIdleSocket(socket, true /* used socket */, &group);
[email protected]ff579d42009-06-24 15:47:02411 } else {
412 delete socket;
413 }
414
[email protected]2ab05b52009-07-01 23:57:58415 OnAvailableSocketSlot(group_name, &group);
[email protected]ff579d42009-06-24 15:47:02416}
417
[email protected]211d2172009-07-22 15:48:53418// Search for the highest priority pending request, amongst the groups that
419// are not at the |max_sockets_per_group_| limit. Note: for requests with
420// the same priority, the winner is based on group hash ordering (and not
421// insertion order).
[email protected]d80a4322009-08-14 07:07:49422int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
423 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53424 Group* top_group = NULL;
425 const std::string* top_group_name = NULL;
426 int stalled_group_count = 0;
427 for (GroupMap::iterator i = group_map_.begin();
428 i != group_map_.end(); ++i) {
429 Group& group = i->second;
430 const RequestQueue& queue = group.pending_requests;
431 if (queue.empty())
432 continue;
433 bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_);
434 if (has_slot)
435 stalled_group_count++;
436 bool has_higher_priority = !top_group ||
437 group.TopPendingPriority() > top_group->TopPendingPriority();
438 if (has_slot && has_higher_priority) {
439 top_group = &group;
440 top_group_name = &i->first;
441 }
442 }
443 if (top_group) {
444 *group = top_group;
445 *group_name = *top_group_name;
446 }
447 return stalled_group_count;
448}
449
[email protected]d80a4322009-08-14 07:07:49450void ClientSocketPoolBaseHelper::OnConnectJobComplete(
451 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58452 DCHECK_NE(ERR_IO_PENDING, result);
453 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02454 GroupMap::iterator group_it = group_map_.find(group_name);
455 CHECK(group_it != group_map_.end());
456 Group& group = group_it->second;
457
[email protected]5fc08e32009-07-15 17:09:57458 const ClientSocketHandle* const key_handle = job->key_handle();
459 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02460
[email protected]5fc08e32009-07-15 17:09:57461 if (g_late_binding) {
[email protected]fd7b7c92009-08-20 19:38:30462 scoped_refptr<LoadLog> job_load_log(job->load_log());
[email protected]5fc08e32009-07-15 17:09:57463 RemoveConnectJob(key_handle, job, &group);
464
[email protected]fd7b7c92009-08-20 19:38:30465 scoped_ptr<const Request> r;
466 if (!group.pending_requests.empty()) {
467 r.reset(RemoveRequestFromQueue(
468 group.pending_requests.begin(), &group.pending_requests));
469
470 if (r->load_log())
471 r->load_log()->Append(job_load_log);
472
473 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
474 }
475
[email protected]5fc08e32009-07-15 17:09:57476 if (result == OK) {
477 DCHECK(socket.get());
[email protected]fd7b7c92009-08-20 19:38:30478 if (r.get()) {
[email protected]5fc08e32009-07-15 17:09:57479 HandOutSocket(
[email protected]f9d285c2009-08-17 19:54:29480 socket.release(), false /* unused socket */, r->handle(),
481 base::TimeDelta(), &group);
[email protected]d80a4322009-08-14 07:07:49482 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57483 } else {
484 AddIdleSocket(socket.release(), false /* unused socket */, &group);
485 OnAvailableSocketSlot(group_name, &group);
486 }
487 } else {
488 DCHECK(!socket.get());
[email protected]fd7b7c92009-08-20 19:38:30489 if (r.get())
[email protected]d80a4322009-08-14 07:07:49490 r->callback()->Run(result);
[email protected]5fc08e32009-07-15 17:09:57491 MaybeOnAvailableSocketSlot(group_name);
492 }
493
494 return;
495 }
496
497 RequestMap* request_map = &group.connecting_requests;
498 RequestMap::iterator it = request_map->find(key_handle);
[email protected]ff579d42009-06-24 15:47:02499 CHECK(it != request_map->end());
[email protected]d80a4322009-08-14 07:07:49500 const Request* request = it->second;
501 ClientSocketHandle* const handle = request->handle();
502 CompletionCallback* const callback = request->callback();
[email protected]ff579d42009-06-24 15:47:02503
[email protected]fd7b7c92009-08-20 19:38:30504 LoadLog::EndEvent(request->load_log(), LoadLog::TYPE_SOCKET_POOL);
505
[email protected]5fc08e32009-07-15 17:09:57506 RemoveConnectJob(key_handle, job, &group);
[email protected]2ab05b52009-07-01 23:57:58507
508 if (result != OK) {
[email protected]8e12ae02009-07-02 16:15:04509 DCHECK(!socket.get());
[email protected]2ab05b52009-07-01 23:57:58510 callback->Run(result); // |group| is not necessarily valid after this.
511 // |group| may be invalid after the callback, we need to search
512 // |group_map_| again.
513 MaybeOnAvailableSocketSlot(group_name);
[email protected]ff579d42009-06-24 15:47:02514 } else {
[email protected]5fc08e32009-07-15 17:09:57515 DCHECK(socket.get());
[email protected]f9d285c2009-08-17 19:54:29516 HandOutSocket(socket.release(), false /* not reused */, handle,
517 base::TimeDelta(), &group);
[email protected]2ab05b52009-07-01 23:57:58518 callback->Run(result);
[email protected]ff579d42009-06-24 15:47:02519 }
[email protected]ff579d42009-06-24 15:47:02520}
521
[email protected]d80a4322009-08-14 07:07:49522void ClientSocketPoolBaseHelper::EnableLateBindingOfSockets(bool enabled) {
[email protected]5fc08e32009-07-15 17:09:57523 g_late_binding = enabled;
524}
525
[email protected]d80a4322009-08-14 07:07:49526void ClientSocketPoolBaseHelper::RemoveConnectJob(
[email protected]974ebd62009-08-03 23:14:34527 const ClientSocketHandle* handle, const ConnectJob *job, Group* group) {
[email protected]211d2172009-07-22 15:48:53528 CHECK(connecting_socket_count_ > 0);
529 connecting_socket_count_--;
530
[email protected]5fc08e32009-07-15 17:09:57531 if (g_late_binding) {
532 DCHECK(job);
533 delete job;
534 } else {
535 ConnectJobMap::iterator it = connect_job_map_.find(handle);
536 CHECK(it != connect_job_map_.end());
537 job = it->second;
538 delete job;
539 connect_job_map_.erase(it);
[email protected]d80a4322009-08-14 07:07:49540 RequestMap::iterator map_it = group->connecting_requests.find(handle);
541 CHECK(map_it != group->connecting_requests.end());
542 const Request* request = map_it->second;
543 delete request;
544 group->connecting_requests.erase(map_it);
[email protected]5fc08e32009-07-15 17:09:57545 }
546
547 if (group) {
548 DCHECK(ContainsKey(group->jobs, job));
549 group->jobs.erase(job);
550 }
[email protected]ff579d42009-06-24 15:47:02551}
552
[email protected]d80a4322009-08-14 07:07:49553void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot(
[email protected]2ab05b52009-07-01 23:57:58554 const std::string& group_name) {
555 GroupMap::iterator it = group_map_.find(group_name);
556 if (it != group_map_.end()) {
557 Group& group = it->second;
558 if (group.HasAvailableSocketSlot(max_sockets_per_group_))
559 OnAvailableSocketSlot(group_name, &group);
560 }
561}
[email protected]ff579d42009-06-24 15:47:02562
[email protected]d80a4322009-08-14 07:07:49563void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
564 const std::string& group_name, Group* group) {
[email protected]211d2172009-07-22 15:48:53565 if (may_have_stalled_group_) {
566 std::string top_group_name;
[email protected]bed37d442009-08-20 19:58:20567 Group* top_group = NULL;
[email protected]211d2172009-07-22 15:48:53568 int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name);
569 if (stalled_group_count <= 1)
570 may_have_stalled_group_ = false;
571 if (stalled_group_count >= 1)
572 ProcessPendingRequest(top_group_name, top_group);
573 } else if (!group->pending_requests.empty()) {
[email protected]ff579d42009-06-24 15:47:02574 ProcessPendingRequest(group_name, group);
575 // |group| may no longer be valid after this point. Be careful not to
576 // access it again.
[email protected]2ab05b52009-07-01 23:57:58577 } else if (group->IsEmpty()) {
[email protected]ff579d42009-06-24 15:47:02578 // Delete |group| if no longer needed. |group| will no longer be valid.
[email protected]ff579d42009-06-24 15:47:02579 group_map_.erase(group_name);
[email protected]ff579d42009-06-24 15:47:02580 }
581}
582
[email protected]d80a4322009-08-14 07:07:49583void ClientSocketPoolBaseHelper::ProcessPendingRequest(
584 const std::string& group_name, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30585 scoped_ptr<const Request> r(RemoveRequestFromQueue(
586 group->pending_requests.begin(), &group->pending_requests));
[email protected]ff579d42009-06-24 15:47:02587
[email protected]d80a4322009-08-14 07:07:49588 int rv = RequestSocket(group_name, r.get());
[email protected]ff579d42009-06-24 15:47:02589
[email protected]2ab05b52009-07-01 23:57:58590 if (rv != ERR_IO_PENDING) {
[email protected]fd7b7c92009-08-20 19:38:30591 LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL);
[email protected]d80a4322009-08-14 07:07:49592 r->callback()->Run(rv);
[email protected]2ab05b52009-07-01 23:57:58593 if (rv != OK) {
594 // |group| may be invalid after the callback, we need to search
595 // |group_map_| again.
596 MaybeOnAvailableSocketSlot(group_name);
597 }
[email protected]d80a4322009-08-14 07:07:49598 } else {
599 r.release();
[email protected]2ab05b52009-07-01 23:57:58600 }
601}
602
[email protected]d80a4322009-08-14 07:07:49603void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58604 ClientSocket* socket,
605 bool reused,
606 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29607 base::TimeDelta idle_time,
[email protected]2ab05b52009-07-01 23:57:58608 Group* group) {
609 DCHECK(socket);
610 handle->set_socket(socket);
611 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29612 handle->set_idle_time(idle_time);
[email protected]211d2172009-07-22 15:48:53613
614 handed_out_socket_count_++;
[email protected]2ab05b52009-07-01 23:57:58615 group->active_socket_count++;
[email protected]ff579d42009-06-24 15:47:02616}
617
[email protected]d80a4322009-08-14 07:07:49618void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57619 ClientSocket* socket, bool used, Group* group) {
620 DCHECK(socket);
621 IdleSocket idle_socket;
622 idle_socket.socket = socket;
623 idle_socket.start_time = base::TimeTicks::Now();
624 idle_socket.used = used;
625
626 group->idle_sockets.push_back(idle_socket);
627 IncrementIdleCount();
628}
629
[email protected]d80a4322009-08-14 07:07:49630void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]5fc08e32009-07-15 17:09:57631 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
632 Group& group = i->second;
633 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
653void EnableLateBindingOfSockets(bool enabled) {
654 internal::ClientSocketPoolBaseHelper::EnableLateBindingOfSockets(enabled);
655}
656
[email protected]ff579d42009-06-24 15:47:02657} // namespace net