blob: 62c7c27d7d42d12e447ad5160fd3595ea5437678 [file] [log] [blame]
[email protected]e34400c32012-01-24 02:49:331// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]ff579d42009-06-24 15:47:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/socket/client_socket_pool_base.h"
6
7#include "base/compiler_specific.h"
[email protected]fd4fe0b2010-02-08 23:02:158#include "base/format_macros.h"
[email protected]5e6efa52011-06-27 17:26:419#include "base/logging.h"
[email protected]18b577412013-07-18 04:19:1510#include "base/message_loop/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3811#include "base/metrics/stats_counters.h"
[email protected]7286e3fc2011-07-19 22:13:2412#include "base/stl_util.h"
[email protected]fc9be5802013-06-11 10:56:5113#include "base/strings/string_util.h"
[email protected]f002abb2013-06-28 02:30:2114#include "base/time/time.h"
[email protected]9349cfb2010-08-31 18:00:5315#include "base/values.h"
[email protected]ff579d42009-06-24 15:47:0216#include "net/base/net_errors.h"
[email protected]f002abb2013-06-28 02:30:2117#include "net/base/net_log.h"
[email protected]ff579d42009-06-24 15:47:0218#include "net/socket/client_socket_handle.h"
19
20using base::TimeDelta;
21
[email protected]b021ece62013-06-11 11:06:3322namespace net {
23
[email protected]ff579d42009-06-24 15:47:0224namespace {
25
[email protected]64770b7d2011-11-16 04:30:4126// Indicate whether we should enable idle socket cleanup timer. When timer is
27// disabled, sockets are closed next time a socket request is made.
28bool g_cleanup_timer_enabled = true;
29
[email protected]ff579d42009-06-24 15:47:0230// The timeout value, in seconds, used to clean up idle sockets that can't be
31// reused.
32//
33// Note: It's important to close idle sockets that have received data as soon
34// as possible because the received data may cause BSOD on Windows XP under
35// some conditions. See http://crbug.com/4606.
36const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
37
[email protected]c847c2a2011-04-08 13:56:1438// Indicate whether or not we should establish a new transport layer connection
39// after a certain timeout has passed without receiving an ACK.
[email protected]06d94042010-08-25 01:45:2240bool g_connect_backup_jobs_enabled = true;
41
[email protected]b021ece62013-06-11 11:06:3342// Compares the effective priority of two results, and returns 1 if |request1|
43// has greater effective priority than |request2|, 0 if they have the same
44// effective priority, and -1 if |request2| has the greater effective priority.
45// Requests with |ignore_limits| set have higher effective priority than those
46// without. If both requests have |ignore_limits| set/unset, then the request
47// with the highest Pririoty has the highest effective priority. Does not take
48// into account the fact that Requests are serviced in FIFO order if they would
49// otherwise have the same priority.
50int CompareEffectiveRequestPriority(
51 const internal::ClientSocketPoolBaseHelper::Request& request1,
52 const internal::ClientSocketPoolBaseHelper::Request& request2) {
53 if (request1.ignore_limits() && !request2.ignore_limits())
54 return 1;
55 if (!request1.ignore_limits() && request2.ignore_limits())
56 return -1;
57 if (request1.priority() > request2.priority())
58 return 1;
59 if (request1.priority() < request2.priority())
60 return -1;
61 return 0;
62}
[email protected]ff579d42009-06-24 15:47:0263
[email protected]b021ece62013-06-11 11:06:3364} // namespace
[email protected]ff579d42009-06-24 15:47:0265
[email protected]2ab05b52009-07-01 23:57:5866ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3467 base::TimeDelta timeout_duration,
[email protected]3f6007ab2013-08-22 19:45:3968 RequestPriority priority,
[email protected]fd7b7c92009-08-20 19:38:3069 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5370 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5871 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3472 timeout_duration_(timeout_duration),
[email protected]3f6007ab2013-08-22 19:45:3973 priority_(priority),
[email protected]2ab05b52009-07-01 23:57:5874 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0275 net_log_(net_log),
[email protected]8159a1c2012-06-07 00:00:1076 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5877 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5878 DCHECK(delegate);
[email protected]e9d7d252013-01-04 02:33:1779 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB,
80 NetLog::StringCallback("group_name", &group_name_));
[email protected]2ab05b52009-07-01 23:57:5881}
82
[email protected]fd7b7c92009-08-20 19:38:3083ConnectJob::~ConnectJob() {
[email protected]3aa4af042012-06-14 21:02:3184 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB);
[email protected]fd7b7c92009-08-20 19:38:3085}
[email protected]2ab05b52009-07-01 23:57:5886
[email protected]18ccfdb2013-08-15 00:13:4487scoped_ptr<StreamSocket> ConnectJob::PassSocket() {
88 return socket_.Pass();
89}
90
[email protected]974ebd62009-08-03 23:14:3491int ConnectJob::Connect() {
92 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0293 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3094
[email protected]a2006ece2010-04-23 16:44:0295 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3096
[email protected]06650c52010-06-03 00:49:1797 LogConnectStart();
98
[email protected]fd7b7c92009-08-20 19:38:3099 int rv = ConnectInternal();
100
101 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:17102 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30103 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:30104 }
105
106 return rv;
107}
108
[email protected]18ccfdb2013-08-15 00:13:44109void ConnectJob::SetSocket(scoped_ptr<StreamSocket> socket) {
[email protected]06650c52010-06-03 00:49:17110 if (socket) {
[email protected]3aa4af042012-06-14 21:02:31111 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
112 socket->NetLog().source().ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17113 }
[email protected]18ccfdb2013-08-15 00:13:44114 socket_ = socket.Pass();
[email protected]06650c52010-06-03 00:49:17115}
116
[email protected]fd7b7c92009-08-20 19:38:30117void ConnectJob::NotifyDelegateOfCompletion(int rv) {
[email protected]18ccfdb2013-08-15 00:13:44118 // The delegate will own |this|.
[email protected]10833352013-02-12 19:39:50119 Delegate* delegate = delegate_;
[email protected]fd7b7c92009-08-20 19:38:30120 delegate_ = NULL;
121
[email protected]06650c52010-06-03 00:49:17122 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30123 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34124}
125
[email protected]a796bcec2010-03-22 17:17:26126void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
127 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02128 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26129}
130
[email protected]06650c52010-06-03 00:49:17131void ConnectJob::LogConnectStart() {
[email protected]034df0f32013-01-07 23:17:48132 connect_timing_.connect_start = base::TimeTicks::Now();
[email protected]e9d7d252013-01-04 02:33:17133 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT);
[email protected]06650c52010-06-03 00:49:17134}
135
136void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]034df0f32013-01-07 23:17:48137 connect_timing_.connect_end = base::TimeTicks::Now();
[email protected]d7fd1782011-02-08 19:16:43138 net_log().EndEventWithNetErrorCode(
139 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17140}
141
[email protected]974ebd62009-08-03 23:14:34142void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40143 // Make sure the socket is NULL before calling into |delegate|.
[email protected]18ccfdb2013-08-15 00:13:44144 SetSocket(scoped_ptr<StreamSocket>());
[email protected]fd7b7c92009-08-20 19:38:30145
[email protected]3aa4af042012-06-14 21:02:31146 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
[email protected]fd7b7c92009-08-20 19:38:30147
148 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34149}
150
[email protected]d80a4322009-08-14 07:07:49151namespace internal {
152
[email protected]fd4fe0b2010-02-08 23:02:15153ClientSocketPoolBaseHelper::Request::Request(
154 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41155 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15156 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20157 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03158 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53159 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13160 : handle_(handle),
161 callback_(callback),
162 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20163 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03164 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53165 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15166
167ClientSocketPoolBaseHelper::Request::~Request() {}
168
[email protected]d80a4322009-08-14 07:07:49169ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53170 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02171 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16172 base::TimeDelta unused_idle_socket_timeout,
173 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38174 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02175 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53176 connecting_socket_count_(0),
177 handed_out_socket_count_(0),
178 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02179 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41180 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16181 unused_idle_socket_timeout_(unused_idle_socket_timeout),
182 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35183 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22184 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13185 pool_generation_number_(0),
[email protected]aa249b52013-04-30 01:04:32186 weak_factory_(this) {
[email protected]211d2172009-07-22 15:48:53187 DCHECK_LE(0, max_sockets_per_group);
188 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52189
[email protected]232a5812011-03-04 22:42:08190 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53191}
[email protected]ff579d42009-06-24 15:47:02192
[email protected]d80a4322009-08-14 07:07:49193ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13194 // Clean up any idle sockets and pending connect jobs. Assert that we have no
195 // remaining active sockets or pending requests. They should have all been
196 // cleaned up prior to |this| being destroyed.
[email protected]7af985a2012-12-14 22:40:42197 FlushWithError(ERR_ABORTED);
[email protected]2431756e2010-09-29 20:26:13198 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21199 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29200 DCHECK_EQ(0, connecting_socket_count_);
[email protected]51fdc7c2012-04-10 19:19:48201 CHECK(higher_layer_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52202
[email protected]232a5812011-03-04 22:42:08203 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02204}
205
[email protected]2a848e0e2012-08-09 22:27:31206ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
207 : result(OK) {
208}
209
210ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
211 const CompletionCallback& callback_in, int result_in)
212 : callback(callback_in),
213 result(result_in) {
214}
215
[email protected]49639fa2011-12-20 23:22:41216ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
217
[email protected]ff579d42009-06-24 15:47:02218// static
[email protected]d80a4322009-08-14 07:07:49219void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
220 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02221 RequestQueue::iterator it = pending_requests->begin();
[email protected]b021ece62013-06-11 11:06:33222 // TODO(mmenke): Should the network stack require requests with
223 // |ignore_limits| have the highest priority?
224 while (it != pending_requests->end() &&
225 CompareEffectiveRequestPriority(*r, *(*it)) <= 0) {
[email protected]ff579d42009-06-24 15:47:02226 ++it;
[email protected]b021ece62013-06-11 11:06:33227 }
[email protected]ff579d42009-06-24 15:47:02228 pending_requests->insert(it, r);
229}
230
[email protected]fd7b7c92009-08-20 19:38:30231// static
232const ClientSocketPoolBaseHelper::Request*
233ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05234 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30235 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02236 group->mutable_pending_requests()->erase(it);
237 // If there are no more requests, we kill the backup timer.
238 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00239 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30240 return req;
241}
242
[email protected]51fdc7c2012-04-10 19:19:48243void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
244 CHECK(pool);
245 CHECK(!ContainsKey(higher_layer_pools_, pool));
246 higher_layer_pools_.insert(pool);
247}
248
249void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
250 CHECK(pool);
251 CHECK(ContainsKey(higher_layer_pools_, pool));
252 higher_layer_pools_.erase(pool);
253}
254
[email protected]d80a4322009-08-14 07:07:49255int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02256 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49257 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41258 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03259 CHECK(request->handle());
260
[email protected]64770b7d2011-11-16 04:30:41261 // Cleanup any timed-out idle sockets if no timer is used.
262 if (!use_cleanup_timer_)
263 CleanupIdleSockets(false);
264
[email protected]3aa4af042012-06-14 21:02:31265 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32266 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26267
[email protected]fd4fe0b2010-02-08 23:02:15268 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17269 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43270 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21271 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17272 delete request;
273 } else {
[email protected]aed99ef02010-08-26 14:04:32274 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]58e562f2013-04-22 17:32:20275 // Have to do this asynchronously, as closing sockets in higher level pools
276 // call back in to |this|, which will cause all sorts of fun and exciting
277 // re-entrancy issues if the socket pool is doing something else at the
278 // time.
279 if (group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
[email protected]2da659e2013-05-23 20:51:34280 base::MessageLoop::current()->PostTask(
[email protected]58e562f2013-04-22 17:32:20281 FROM_HERE,
282 base::Bind(
283 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools,
284 weak_factory_.GetWeakPtr()));
285 }
[email protected]e7e99322010-05-04 23:30:17286 }
[email protected]fd4fe0b2010-02-08 23:02:15287 return rv;
288}
289
[email protected]2c2bef152010-10-13 00:55:03290void ClientSocketPoolBaseHelper::RequestSockets(
291 const std::string& group_name,
292 const Request& request,
293 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41294 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03295 DCHECK(!request.handle());
296
[email protected]64770b7d2011-11-16 04:30:41297 // Cleanup any timed out idle sockets if no timer is used.
298 if (!use_cleanup_timer_)
299 CleanupIdleSockets(false);
300
[email protected]2c2bef152010-10-13 00:55:03301 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03302 num_sockets = max_sockets_per_group_;
303 }
304
305 request.net_log().BeginEvent(
306 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31307 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03308
309 Group* group = GetOrCreateGroup(group_name);
310
[email protected]3c819f522010-12-02 02:03:12311 // RequestSocketsInternal() may delete the group.
312 bool deleted_group = false;
313
[email protected]d7fd1782011-02-08 19:16:43314 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03315 for (int num_iterations_left = num_sockets;
316 group->NumActiveSocketSlots() < num_sockets &&
317 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43318 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03319 if (rv < 0 && rv != ERR_IO_PENDING) {
320 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12321 if (!ContainsKey(group_map_, group_name))
322 deleted_group = true;
323 break;
324 }
325 if (!ContainsKey(group_map_, group_name)) {
326 // Unexpected. The group should only be getting deleted on synchronous
327 // error.
328 NOTREACHED();
329 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03330 break;
331 }
332 }
333
[email protected]3c819f522010-12-02 02:03:12334 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03335 RemoveGroup(group_name);
336
[email protected]d7fd1782011-02-08 19:16:43337 if (rv == ERR_IO_PENDING)
338 rv = OK;
339 request.net_log().EndEventWithNetErrorCode(
340 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03341}
342
[email protected]fd4fe0b2010-02-08 23:02:15343int ClientSocketPoolBaseHelper::RequestSocketInternal(
344 const std::string& group_name,
345 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49346 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03347 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32348 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02349
[email protected]2c2bef152010-10-13 00:55:03350 if (!(request->flags() & NO_IDLE_SOCKETS)) {
351 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10352 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03353 return OK;
354 }
355
[email protected]8159a1c2012-06-07 00:00:10356 // If there are more ConnectJobs than pending requests, don't need to do
357 // anything. Can just wait for the extra job to connect, and then assign it
358 // to the request.
359 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03360 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10361
[email protected]43a21b82010-06-10 21:30:54362 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20363 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
364 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48365 // TODO(willchan): Consider whether or not we need to close a socket in a
366 // higher layered group. I don't think this makes sense since we would just
367 // reuse that socket then if we needed one and wouldn't make it down to this
368 // layer.
[email protected]43a21b82010-06-10 21:30:54369 request->net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31370 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54371 return ERR_IO_PENDING;
372 }
373
[email protected]5acdce12011-03-30 13:00:20374 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48375 // NOTE(mmenke): Wonder if we really need different code for each case
376 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54377 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48378 // There's an idle socket in this pool. Either that's because there's
379 // still one in this group, but we got here due to preconnecting bypassing
380 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46381 bool closed = CloseOneIdleSocketExceptInGroup(group);
382 if (preconnecting && !closed)
383 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54384 } else {
[email protected]40dee91a2012-04-17 19:25:46385 // We could check if we really have a stalled group here, but it requires
386 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]3aa4af042012-06-14 21:02:31387 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46388 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54389 }
390 }
391
[email protected]51fdc7c2012-04-10 19:19:48392 // We couldn't find a socket to reuse, and there's space to allocate one,
393 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58394 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17395 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02396
[email protected]2ab05b52009-07-01 23:57:58397 int rv = connect_job->Connect();
398 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17399 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03400 if (!preconnecting) {
[email protected]18ccfdb2013-08-15 00:13:44401 HandOutSocket(connect_job->PassSocket(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48402 connect_job->connect_timing(), handle, base::TimeDelta(),
403 group, request->net_log());
[email protected]2c2bef152010-10-13 00:55:03404 } else {
[email protected]18ccfdb2013-08-15 00:13:44405 AddIdleSocket(connect_job->PassSocket(), group);
[email protected]2c2bef152010-10-13 00:55:03406 }
[email protected]2ab05b52009-07-01 23:57:58407 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32408 // If we don't have any sockets in this group, set a timer for potentially
409 // creating a new one. If the SYN is lost, this backup socket may complete
410 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03411 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00412 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32413 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03414 }
[email protected]6b624c62010-03-14 08:37:32415
[email protected]211d2172009-07-22 15:48:53416 connecting_socket_count_++;
417
[email protected]18ccfdb2013-08-15 00:13:44418 group->AddJob(connect_job.Pass(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02419 } else {
[email protected]06650c52010-06-03 00:49:17420 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]18ccfdb2013-08-15 00:13:44421 scoped_ptr<StreamSocket> error_socket;
[email protected]fd2e53e2011-01-14 20:40:52422 if (!preconnecting) {
423 DCHECK(handle);
424 connect_job->GetAdditionalErrorState(handle);
[email protected]18ccfdb2013-08-15 00:13:44425 error_socket = connect_job->PassSocket();
[email protected]fd2e53e2011-01-14 20:40:52426 }
[email protected]e772db3f2010-07-12 18:11:13427 if (error_socket) {
[email protected]18ccfdb2013-08-15 00:13:44428 HandOutSocket(error_socket.Pass(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48429 connect_job->connect_timing(), handle, base::TimeDelta(),
430 group, request->net_log());
[email protected]aed99ef02010-08-26 14:04:32431 } else if (group->IsEmpty()) {
432 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21433 }
[email protected]2ab05b52009-07-01 23:57:58434 }
[email protected]ff579d42009-06-24 15:47:02435
[email protected]2ab05b52009-07-01 23:57:58436 return rv;
[email protected]ff579d42009-06-24 15:47:02437}
438
[email protected]8159a1c2012-06-07 00:00:10439bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]aed99ef02010-08-26 14:04:32440 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22441 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
442 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
443
444 // Iterate through the idle sockets forwards (oldest to newest)
445 // * Delete any disconnected ones.
446 // * If we find a used idle socket, assign to |idle_socket|. At the end,
447 // the |idle_socket_it| will be set to the newest used idle socket.
448 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
449 it != idle_sockets->end();) {
450 if (!it->socket->IsConnectedAndIdle()) {
451 DecrementIdleCount();
452 delete it->socket;
453 it = idle_sockets->erase(it);
454 continue;
[email protected]eb5a99382010-07-11 03:18:26455 }
[email protected]e1b54dc2010-10-06 21:27:22456
457 if (it->socket->WasEverUsed()) {
458 // We found one we can reuse!
[email protected]e86df8dc2013-03-30 13:18:28459 idle_socket_it = it;
[email protected]e1b54dc2010-10-06 21:27:22460 }
461
462 ++it;
[email protected]eb5a99382010-07-11 03:18:26463 }
[email protected]e1b54dc2010-10-06 21:27:22464
465 // If we haven't found an idle socket, that means there are no used idle
466 // sockets. Pick the oldest (first) idle socket (FIFO).
467
468 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
469 idle_socket_it = idle_sockets->begin();
470
471 if (idle_socket_it != idle_sockets->end()) {
472 DecrementIdleCount();
473 base::TimeDelta idle_time =
474 base::TimeTicks::Now() - idle_socket_it->start_time;
475 IdleSocket idle_socket = *idle_socket_it;
476 idle_sockets->erase(idle_socket_it);
477 HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44478 scoped_ptr<StreamSocket>(idle_socket.socket),
[email protected]e1b54dc2010-10-06 21:27:22479 idle_socket.socket->WasEverUsed(),
[email protected]034df0f32013-01-07 23:17:48480 LoadTimingInfo::ConnectTiming(),
[email protected]e1b54dc2010-10-06 21:27:22481 request->handle(),
482 idle_time,
483 group,
484 request->net_log());
485 return true;
486 }
487
[email protected]eb5a99382010-07-11 03:18:26488 return false;
489}
490
[email protected]06650c52010-06-03 00:49:17491// static
492void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
493 const NetLog::Source& connect_job_source, const Request* request) {
[email protected]3aa4af042012-06-14 21:02:31494 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
495 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17496}
497
[email protected]d80a4322009-08-14 07:07:49498void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21499 const std::string& group_name, ClientSocketHandle* handle) {
500 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
501 if (callback_it != pending_callback_map_.end()) {
502 int result = callback_it->second.result;
503 pending_callback_map_.erase(callback_it);
[email protected]18ccfdb2013-08-15 00:13:44504 scoped_ptr<StreamSocket> socket = handle->PassSocket();
[email protected]05ea9ff2010-07-15 19:08:21505 if (socket) {
506 if (result != OK)
507 socket->Disconnect();
[email protected]18ccfdb2013-08-15 00:13:44508 ReleaseSocket(handle->group_name(), socket.Pass(), handle->id());
[email protected]05ea9ff2010-07-15 19:08:21509 }
510 return;
511 }
[email protected]b6501d3d2010-06-03 23:53:34512
[email protected]ff579d42009-06-24 15:47:02513 CHECK(ContainsKey(group_map_, group_name));
514
[email protected]aed99ef02010-08-26 14:04:32515 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02516
[email protected]ff579d42009-06-24 15:47:02517 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32518 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
519 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49520 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03521 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]3aa4af042012-06-14 21:02:31522 req->net_log().AddEvent(NetLog::TYPE_CANCELLED);
523 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26524
[email protected]b021ece62013-06-11 11:06:33525 // We let the job run, unless we're at the socket limit and there is
526 // not another request waiting on the job.
527 if (group->jobs().size() > group->pending_requests().size() &&
528 ReachedMaxSocketsLimit()) {
[email protected]aed99ef02010-08-26 14:04:32529 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26530 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34531 }
[email protected]eb5a99382010-07-11 03:18:26532 break;
[email protected]ff579d42009-06-24 15:47:02533 }
534 }
[email protected]ff579d42009-06-24 15:47:02535}
536
[email protected]2abfe90a2010-08-25 17:49:51537bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
538 return ContainsKey(group_map_, group_name);
539}
540
[email protected]d80a4322009-08-14 07:07:49541void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02542 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14543 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02544}
545
[email protected]d80a4322009-08-14 07:07:49546int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02547 const std::string& group_name) const {
548 GroupMap::const_iterator i = group_map_.find(group_name);
549 CHECK(i != group_map_.end());
550
[email protected]aed99ef02010-08-26 14:04:32551 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02552}
553
[email protected]d80a4322009-08-14 07:07:49554LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02555 const std::string& group_name,
556 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21557 if (ContainsKey(pending_callback_map_, handle))
558 return LOAD_STATE_CONNECTING;
559
[email protected]ff579d42009-06-24 15:47:02560 if (!ContainsKey(group_map_, group_name)) {
561 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
562 << " for handle: " << handle;
563 return LOAD_STATE_IDLE;
564 }
565
566 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32567 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02568
[email protected]03b7c8c2013-07-20 04:38:55569 // Search the first group.jobs().size() |pending_requests| for |handle|.
570 // If it's farther back in the deque than that, it doesn't have a
571 // corresponding ConnectJob.
572 size_t connect_jobs = group.jobs().size();
[email protected]aed99ef02010-08-26 14:04:32573 RequestQueue::const_iterator it = group.pending_requests().begin();
[email protected]03b7c8c2013-07-20 04:38:55574 for (size_t i = 0; it != group.pending_requests().end() && i < connect_jobs;
575 ++it, ++i) {
576 if ((*it)->handle() != handle)
577 continue;
578
579 // Just return the state of the farthest along ConnectJob for the first
580 // group.jobs().size() pending requests.
581 LoadState max_state = LOAD_STATE_IDLE;
582 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
583 job_it != group.jobs().end(); ++job_it) {
584 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]ff579d42009-06-24 15:47:02585 }
[email protected]03b7c8c2013-07-20 04:38:55586 return max_state;
[email protected]ff579d42009-06-24 15:47:02587 }
588
[email protected]03b7c8c2013-07-20 04:38:55589 if (group.IsStalledOnPoolMaxSockets(max_sockets_per_group_))
590 return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL;
591 return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET;
[email protected]ff579d42009-06-24 15:47:02592}
593
[email protected]10833352013-02-12 19:39:50594base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27595 const std::string& name, const std::string& type) const {
[email protected]10833352013-02-12 19:39:50596 base::DictionaryValue* dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27597 dict->SetString("name", name);
598 dict->SetString("type", type);
599 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
600 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
601 dict->SetInteger("idle_socket_count", idle_socket_count_);
602 dict->SetInteger("max_socket_count", max_sockets_);
603 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
604 dict->SetInteger("pool_generation_number", pool_generation_number_);
605
606 if (group_map_.empty())
607 return dict;
608
[email protected]10833352013-02-12 19:39:50609 base::DictionaryValue* all_groups_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27610 for (GroupMap::const_iterator it = group_map_.begin();
611 it != group_map_.end(); it++) {
612 const Group* group = it->second;
[email protected]10833352013-02-12 19:39:50613 base::DictionaryValue* group_dict = new base::DictionaryValue();
[email protected]59d7a5a2010-08-30 16:44:27614
615 group_dict->SetInteger("pending_request_count",
616 group->pending_requests().size());
617 if (!group->pending_requests().empty()) {
618 group_dict->SetInteger("top_pending_priority",
619 group->TopPendingPriority());
620 }
621
622 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07623
[email protected]10833352013-02-12 19:39:50624 base::ListValue* idle_socket_list = new base::ListValue();
[email protected]e1b54dc2010-10-06 21:27:22625 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07626 for (idle_socket = group->idle_sockets().begin();
627 idle_socket != group->idle_sockets().end();
628 idle_socket++) {
629 int source_id = idle_socket->socket->NetLog().source().id;
[email protected]10833352013-02-12 19:39:50630 idle_socket_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07631 }
632 group_dict->Set("idle_sockets", idle_socket_list);
633
[email protected]10833352013-02-12 19:39:50634 base::ListValue* connect_jobs_list = new base::ListValue();
[email protected]2c2bef152010-10-13 00:55:03635 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07636 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
637 int source_id = (*job)->net_log().source().id;
[email protected]10833352013-02-12 19:39:50638 connect_jobs_list->Append(new base::FundamentalValue(source_id));
[email protected]0496f9a32010-09-30 16:08:07639 }
640 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27641
642 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48643 group->IsStalledOnPoolMaxSockets(
644 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00645 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27646
647 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
648 }
649 dict->Set("groups", all_groups_dict);
650 return dict;
651}
652
[email protected]d80a4322009-08-14 07:07:49653bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16654 base::TimeTicks now,
655 base::TimeDelta timeout) const {
656 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01657 if (timed_out)
658 return true;
659 if (socket->WasEverUsed())
660 return !socket->IsConnectedAndIdle();
661 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02662}
663
[email protected]d80a4322009-08-14 07:07:49664void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02665 if (idle_socket_count_ == 0)
666 return;
667
668 // Current time value. Retrieving it once at the function start rather than
669 // inside the inner loop, since it shouldn't change by any meaningful amount.
670 base::TimeTicks now = base::TimeTicks::Now();
671
672 GroupMap::iterator i = group_map_.begin();
673 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32674 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02675
[email protected]e1b54dc2010-10-06 21:27:22676 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32677 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16678 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01679 j->socket->WasEverUsed() ?
680 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16681 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02682 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32683 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02684 DecrementIdleCount();
685 } else {
686 ++j;
687 }
688 }
689
690 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32691 if (group->IsEmpty()) {
692 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02693 } else {
694 ++i;
695 }
696 }
697}
698
[email protected]aed99ef02010-08-26 14:04:32699ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
700 const std::string& group_name) {
701 GroupMap::iterator it = group_map_.find(group_name);
702 if (it != group_map_.end())
703 return it->second;
704 Group* group = new Group;
705 group_map_[group_name] = group;
706 return group;
707}
708
709void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
710 GroupMap::iterator it = group_map_.find(group_name);
711 CHECK(it != group_map_.end());
712
713 RemoveGroup(it);
714}
715
716void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
717 delete it->second;
718 group_map_.erase(it);
719}
720
[email protected]06d94042010-08-25 01:45:22721// static
[email protected]2d6728692011-03-12 01:39:55722bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
723 return g_connect_backup_jobs_enabled;
724}
725
726// static
[email protected]636b8252011-04-08 19:56:54727bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
728 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22729 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54730 return old_value;
[email protected]06d94042010-08-25 01:45:22731}
732
733void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
734 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
735}
736
[email protected]d80a4322009-08-14 07:07:49737void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41738 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
739 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02740}
741
[email protected]d80a4322009-08-14 07:07:49742void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02743 if (--idle_socket_count_ == 0)
744 timer_.Stop();
745}
746
[email protected]64770b7d2011-11-16 04:30:41747// static
748bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
749 return g_cleanup_timer_enabled;
750}
751
752// static
753bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
754 bool old_value = g_cleanup_timer_enabled;
755 g_cleanup_timer_enabled = enabled;
756 return old_value;
757}
758
759void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
760 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
761 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
762}
763
[email protected]eb5a99382010-07-11 03:18:26764void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]18ccfdb2013-08-15 00:13:44765 scoped_ptr<StreamSocket> socket,
[email protected]eb5a99382010-07-11 03:18:26766 int id) {
[email protected]ff579d42009-06-24 15:47:02767 GroupMap::iterator i = group_map_.find(group_name);
768 CHECK(i != group_map_.end());
769
[email protected]aed99ef02010-08-26 14:04:32770 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02771
[email protected]b1f031dd2010-03-02 23:19:33772 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53773 handed_out_socket_count_--;
774
[email protected]aed99ef02010-08-26 14:04:32775 CHECK_GT(group->active_socket_count(), 0);
776 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02777
[email protected]a7e38572010-06-07 18:22:24778 const bool can_reuse = socket->IsConnectedAndIdle() &&
779 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02780 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26781 // Add it to the idle list.
[email protected]18ccfdb2013-08-15 00:13:44782 AddIdleSocket(socket.Pass(), group);
[email protected]aed99ef02010-08-26 14:04:32783 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02784 } else {
[email protected]18ccfdb2013-08-15 00:13:44785 socket.reset();
[email protected]ff579d42009-06-24 15:47:02786 }
[email protected]05ea9ff2010-07-15 19:08:21787
[email protected]eb5a99382010-07-11 03:18:26788 CheckForStalledSocketGroups();
789}
[email protected]ff579d42009-06-24 15:47:02790
[email protected]eb5a99382010-07-11 03:18:26791void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
792 // If we have idle sockets, see if we can give one to the top-stalled group.
793 std::string top_group_name;
794 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21795 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26796 return;
[email protected]4f2abec2010-02-03 18:10:16797
[email protected]eb5a99382010-07-11 03:18:26798 if (ReachedMaxSocketsLimit()) {
799 if (idle_socket_count() > 0) {
800 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54801 } else {
[email protected]eb5a99382010-07-11 03:18:26802 // We can't activate more sockets since we're already at our global
803 // limit.
[email protected]4f2abec2010-02-03 18:10:16804 return;
[email protected]d7027bb2010-05-10 18:58:54805 }
[email protected]4f2abec2010-02-03 18:10:16806 }
[email protected]eb5a99382010-07-11 03:18:26807
808 // Note: we don't loop on waking stalled groups. If the stalled group is at
809 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21810 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26811 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21812 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02813}
814
[email protected]211d2172009-07-22 15:48:53815// Search for the highest priority pending request, amongst the groups that
816// are not at the |max_sockets_per_group_| limit. Note: for requests with
817// the same priority, the winner is based on group hash ordering (and not
818// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48819bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
820 Group** group,
821 std::string* group_name) const {
822 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53823 Group* top_group = NULL;
824 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21825 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48826 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53827 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32828 Group* curr_group = i->second;
829 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53830 if (queue.empty())
831 continue;
[email protected]51fdc7c2012-04-10 19:19:48832 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
833 if (!group)
834 return true;
[email protected]05ea9ff2010-07-15 19:08:21835 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41836 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05837 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41838 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32839 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41840 top_group_name = &i->first;
841 }
[email protected]211d2172009-07-22 15:48:53842 }
843 }
[email protected]05ea9ff2010-07-15 19:08:21844
[email protected]211d2172009-07-22 15:48:53845 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48846 CHECK(group);
[email protected]211d2172009-07-22 15:48:53847 *group = top_group;
848 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48849 } else {
850 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53851 }
[email protected]05ea9ff2010-07-15 19:08:21852 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53853}
854
[email protected]d80a4322009-08-14 07:07:49855void ClientSocketPoolBaseHelper::OnConnectJobComplete(
856 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58857 DCHECK_NE(ERR_IO_PENDING, result);
858 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02859 GroupMap::iterator group_it = group_map_.find(group_name);
860 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32861 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02862
[email protected]18ccfdb2013-08-15 00:13:44863 scoped_ptr<StreamSocket> socket = job->PassSocket();
[email protected]ff579d42009-06-24 15:47:02864
[email protected]034df0f32013-01-07 23:17:48865 // Copies of these are needed because |job| may be deleted before they are
866 // accessed.
[email protected]9e743cd2010-03-16 07:03:53867 BoundNetLog job_log = job->net_log();
[email protected]034df0f32013-01-07 23:17:48868 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
[email protected]5fc08e32009-07-15 17:09:57869
[email protected]18ccfdb2013-08-15 00:13:44870 // RemoveConnectJob(job, _) must be called by all branches below;
871 // otherwise, |job| will be leaked.
872
[email protected]4d3b05d2010-01-27 21:27:29873 if (result == OK) {
874 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32875 RemoveConnectJob(job, group);
876 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29877 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02878 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17879 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29880 HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44881 socket.Pass(), false /* unused socket */, connect_timing,
[email protected]034df0f32013-01-07 23:17:48882 r->handle(), base::TimeDelta(), group, r->net_log());
[email protected]3aa4af042012-06-14 21:02:31883 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]05ea9ff2010-07-15 19:08:21884 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57885 } else {
[email protected]18ccfdb2013-08-15 00:13:44886 AddIdleSocket(socket.Pass(), group);
[email protected]aed99ef02010-08-26 14:04:32887 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21888 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57889 }
[email protected]94c20472010-01-14 08:14:36890 } else {
[email protected]e772db3f2010-07-12 18:11:13891 // If we got a socket, it must contain error information so pass that
892 // up so that the caller can retrieve it.
893 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32894 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29895 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02896 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17897 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18898 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32899 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13900 if (socket.get()) {
901 handed_out_socket = true;
[email protected]18ccfdb2013-08-15 00:13:44902 HandOutSocket(socket.Pass(), false /* unused socket */,
[email protected]034df0f32013-01-07 23:17:48903 connect_timing, r->handle(), base::TimeDelta(), group,
904 r->net_log());
[email protected]e772db3f2010-07-12 18:11:13905 }
[email protected]034df0f32013-01-07 23:17:48906 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, result);
[email protected]05ea9ff2010-07-15 19:08:21907 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18908 } else {
[email protected]aed99ef02010-08-26 14:04:32909 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29910 }
[email protected]05ea9ff2010-07-15 19:08:21911 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32912 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21913 CheckForStalledSocketGroups();
914 }
[email protected]ff579d42009-06-24 15:47:02915 }
[email protected]ff579d42009-06-24 15:47:02916}
917
[email protected]66761b952010-06-25 21:30:38918void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42919 FlushWithError(ERR_NETWORK_CHANGED);
[email protected]66761b952010-06-25 21:30:38920}
921
[email protected]7af985a2012-12-14 22:40:42922void ClientSocketPoolBaseHelper::FlushWithError(int error) {
[email protected]a7e38572010-06-07 18:22:24923 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14924 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52925 CloseIdleSockets();
[email protected]7af985a2012-12-14 22:40:42926 CancelAllRequestsWithError(error);
[email protected]a554a8262010-05-20 00:13:52927}
928
[email protected]51fdc7c2012-04-10 19:19:48929bool ClientSocketPoolBaseHelper::IsStalled() const {
930 // If we are not using |max_sockets_|, then clearly we are not stalled
931 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
932 return false;
933 // So in order to be stalled we need to be using |max_sockets_| AND
934 // we need to have a request that is actually stalled on the global
935 // socket limit. To find such a request, we look for a group that
936 // a has more requests that jobs AND where the number of jobs is less
937 // than |max_sockets_per_group_|. (If the number of jobs is equal to
938 // |max_sockets_per_group_|, then the request is stalled on the group,
939 // which does not count.)
940 for (GroupMap::const_iterator it = group_map_.begin();
941 it != group_map_.end(); ++it) {
942 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
943 return true;
944 }
945 return false;
946}
947
[email protected]2c2bef152010-10-13 00:55:03948void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29949 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33950 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53951 connecting_socket_count_--;
952
[email protected]25eea382010-07-10 23:55:26953 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32954 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26955
956 // If we've got no more jobs for this group, then we no longer need a
957 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32958 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00959 group->CleanupBackupJob();
[email protected]2ab05b52009-07-01 23:57:58960}
[email protected]ff579d42009-06-24 15:47:02961
[email protected]8ae03f42010-07-07 19:08:10962void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21963 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51964 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21965 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32966 RemoveGroup(group_name);
967 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51968 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10969}
[email protected]06650c52010-06-03 00:49:17970
[email protected]8ae03f42010-07-07 19:08:10971void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21972 const std::string& group_name, Group* group) {
973 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32974 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21975 if (rv != ERR_IO_PENDING) {
976 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02977 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51978 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32979 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10980
[email protected]d7fd1782011-02-08 19:16:43981 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41982 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58983 }
984}
985
[email protected]d80a4322009-08-14 07:07:49986void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]18ccfdb2013-08-15 00:13:44987 scoped_ptr<StreamSocket> socket,
[email protected]2ab05b52009-07-01 23:57:58988 bool reused,
[email protected]034df0f32013-01-07 23:17:48989 const LoadTimingInfo::ConnectTiming& connect_timing,
[email protected]2ab05b52009-07-01 23:57:58990 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29991 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15992 Group* group,
[email protected]9e743cd2010-03-16 07:03:53993 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58994 DCHECK(socket);
[email protected]18ccfdb2013-08-15 00:13:44995 handle->SetSocket(socket.Pass());
[email protected]2ab05b52009-07-01 23:57:58996 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29997 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24998 handle->set_pool_id(pool_generation_number_);
[email protected]034df0f32013-01-07 23:17:48999 handle->set_connect_timing(connect_timing);
[email protected]211d2172009-07-22 15:48:531000
[email protected]d13f51b2010-04-27 23:20:451001 if (reused) {
[email protected]ec11be62010-04-28 19:28:091002 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:451003 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:311004 NetLog::IntegerCallback(
1005 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:151006 }
[email protected]d13f51b2010-04-27 23:20:451007
[email protected]18ccfdb2013-08-15 00:13:441008 net_log.AddEvent(
1009 NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
1010 handle->socket()->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:151011
[email protected]211d2172009-07-22 15:48:531012 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321013 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021014}
1015
[email protected]d80a4322009-08-14 07:07:491016void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]18ccfdb2013-08-15 00:13:441017 scoped_ptr<StreamSocket> socket,
1018 Group* group) {
[email protected]5fc08e32009-07-15 17:09:571019 DCHECK(socket);
1020 IdleSocket idle_socket;
[email protected]18ccfdb2013-08-15 00:13:441021 idle_socket.socket = socket.release();
[email protected]5fc08e32009-07-15 17:09:571022 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571023
[email protected]aed99ef02010-08-26 14:04:321024 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571025 IncrementIdleCount();
1026}
1027
[email protected]d80a4322009-08-14 07:07:491028void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541029 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321030 Group* group = i->second;
1031 connecting_socket_count_ -= group->jobs().size();
1032 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321033
[email protected]5fc08e32009-07-15 17:09:571034 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321035 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141036 // RemoveGroup() will call .erase() which will invalidate the iterator,
1037 // but i will already have been incremented to a valid iterator before
1038 // RemoveGroup() is called.
1039 RemoveGroup(i++);
1040 } else {
1041 ++i;
1042 }
1043 }
1044 DCHECK_EQ(0, connecting_socket_count_);
1045}
1046
[email protected]7af985a2012-12-14 22:40:421047void ClientSocketPoolBaseHelper::CancelAllRequestsWithError(int error) {
[email protected]06f92462010-08-31 19:24:141048 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1049 Group* group = i->second;
1050
1051 RequestQueue pending_requests;
1052 pending_requests.swap(*group->mutable_pending_requests());
1053 for (RequestQueue::iterator it2 = pending_requests.begin();
1054 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031055 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141056 InvokeUserCallbackLater(
[email protected]7af985a2012-12-14 22:40:421057 request->handle(), request->callback(), error);
[email protected]06f92462010-08-31 19:24:141058 }
1059
1060 // Delete group if no longer needed.
1061 if (group->IsEmpty()) {
1062 // RemoveGroup() will call .erase() which will invalidate the iterator,
1063 // but i will already have been incremented to a valid iterator before
1064 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321065 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541066 } else {
1067 ++i;
[email protected]5fc08e32009-07-15 17:09:571068 }
1069 }
1070}
1071
[email protected]d80a4322009-08-14 07:07:491072bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531073 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541074 int total = handed_out_socket_count_ + connecting_socket_count_ +
1075 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201076 // There can be more sockets than the limit since some requests can ignore
1077 // the limit
[email protected]c901f6d2010-04-27 17:48:281078 if (total < max_sockets_)
1079 return false;
[email protected]c901f6d2010-04-27 17:48:281080 return true;
[email protected]211d2172009-07-22 15:48:531081}
1082
[email protected]51fdc7c2012-04-10 19:19:481083bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1084 if (idle_socket_count() == 0)
1085 return false;
1086 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461087}
1088
1089bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1090 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541091 CHECK_GT(idle_socket_count(), 0);
1092
1093 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321094 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461095 if (exception_group == group)
1096 continue;
[email protected]e1b54dc2010-10-06 21:27:221097 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541098
[email protected]e1b54dc2010-10-06 21:27:221099 if (!idle_sockets->empty()) {
1100 delete idle_sockets->front().socket;
1101 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541102 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321103 if (group->IsEmpty())
1104 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541105
[email protected]dcbe168a2010-12-02 03:14:461106 return true;
[email protected]43a21b82010-06-10 21:30:541107 }
1108 }
1109
[email protected]51fdc7c2012-04-10 19:19:481110 return false;
1111}
[email protected]dcbe168a2010-12-02 03:14:461112
[email protected]51fdc7c2012-04-10 19:19:481113bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1114 // This pool doesn't have any idle sockets. It's possible that a pool at a
1115 // higher layer is holding one of this sockets active, but it's actually idle.
1116 // Query the higher layers.
1117 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1118 it != higher_layer_pools_.end(); ++it) {
1119 if ((*it)->CloseOneIdleConnection())
1120 return true;
1121 }
[email protected]dcbe168a2010-12-02 03:14:461122 return false;
[email protected]43a21b82010-06-10 21:30:541123}
1124
[email protected]05ea9ff2010-07-15 19:08:211125void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411126 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211127 CHECK(!ContainsKey(pending_callback_map_, handle));
1128 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
[email protected]2da659e2013-05-23 20:51:341129 base::MessageLoop::current()->PostTask(
[email protected]05ea9ff2010-07-15 19:08:211130 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411131 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1132 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211133}
1134
1135void ClientSocketPoolBaseHelper::InvokeUserCallback(
1136 ClientSocketHandle* handle) {
1137 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1138
1139 // Exit if the request has already been cancelled.
1140 if (it == pending_callback_map_.end())
1141 return;
1142
1143 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411144 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211145 int result = it->second.result;
1146 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411147 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211148}
1149
[email protected]58e562f2013-04-22 17:32:201150void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() {
1151 while (IsStalled()) {
1152 // Closing a socket will result in calling back into |this| to use the freed
1153 // socket slot, so nothing else is needed.
1154 if (!CloseOneIdleConnectionInLayeredPool())
1155 return;
1156 }
1157}
1158
[email protected]aed99ef02010-08-26 14:04:321159ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101160 : unassigned_job_count_(0),
1161 active_socket_count_(0),
[email protected]aa249b52013-04-30 01:04:321162 weak_factory_(this) {}
[email protected]aed99ef02010-08-26 14:04:321163
[email protected]e4d9a9722011-05-12 00:16:001164ClientSocketPoolBaseHelper::Group::~Group() {
1165 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101166 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321167}
1168
1169void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1170 const std::string& group_name,
1171 ClientSocketPoolBaseHelper* pool) {
1172 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411173 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321174 return;
1175
[email protected]2da659e2013-05-23 20:51:341176 base::MessageLoop::current()->PostDelayedTask(
[email protected]aed99ef02010-08-26 14:04:321177 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411178 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1179 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001180 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321181}
1182
[email protected]8159a1c2012-06-07 00:00:101183bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1184 SanityCheck();
1185
1186 if (unassigned_job_count_ == 0)
1187 return false;
1188 --unassigned_job_count_;
1189 return true;
1190}
1191
[email protected]18ccfdb2013-08-15 00:13:441192void ClientSocketPoolBaseHelper::Group::AddJob(scoped_ptr<ConnectJob> job,
[email protected]8159a1c2012-06-07 00:00:101193 bool is_preconnect) {
1194 SanityCheck();
1195
1196 if (is_preconnect)
1197 ++unassigned_job_count_;
[email protected]18ccfdb2013-08-15 00:13:441198 jobs_.insert(job.release());
[email protected]8159a1c2012-06-07 00:00:101199}
1200
1201void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
[email protected]ee6e0b62013-08-14 21:55:421202 scoped_ptr<ConnectJob> owned_job(job);
[email protected]8159a1c2012-06-07 00:00:101203 SanityCheck();
1204
[email protected]ee6e0b62013-08-14 21:55:421205 std::set<ConnectJob*>::iterator it = jobs_.find(job);
1206 if (it != jobs_.end()) {
1207 jobs_.erase(it);
1208 } else {
1209 NOTREACHED();
1210 }
[email protected]8159a1c2012-06-07 00:00:101211 size_t job_count = jobs_.size();
1212 if (job_count < unassigned_job_count_)
1213 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031214}
1215
[email protected]aed99ef02010-08-26 14:04:321216void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1217 std::string group_name,
1218 ClientSocketPoolBaseHelper* pool) {
1219 // If there are no more jobs pending, there is no work to do.
1220 // If we've done our cleanups correctly, this should not happen.
1221 if (jobs_.empty()) {
1222 NOTREACHED();
1223 return;
1224 }
1225
[email protected]8159a1c2012-06-07 00:00:101226 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321227 // right now due to limits, just reset the timer.
1228 if (pool->ReachedMaxSocketsLimit() ||
1229 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1230 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1231 StartBackupSocketTimer(group_name, pool);
1232 return;
1233 }
1234
[email protected]a9fc8fc2011-05-10 02:41:071235 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441236 return;
[email protected]4baaf9d2010-08-31 15:15:441237
[email protected]18ccfdb2013-08-15 00:13:441238 scoped_ptr<ConnectJob> backup_job =
1239 pool->connect_job_factory_->NewConnectJob(
1240 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311241 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321242 SIMPLE_STATS_COUNTER("socket.backup_created");
1243 int rv = backup_job->Connect();
1244 pool->connecting_socket_count_++;
[email protected]18ccfdb2013-08-15 00:13:441245 ConnectJob* raw_backup_job = backup_job.get();
1246 AddJob(backup_job.Pass(), false);
[email protected]aed99ef02010-08-26 14:04:321247 if (rv != ERR_IO_PENDING)
[email protected]18ccfdb2013-08-15 00:13:441248 pool->OnConnectJobComplete(rv, raw_backup_job);
[email protected]aed99ef02010-08-26 14:04:321249}
1250
[email protected]8159a1c2012-06-07 00:00:101251void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1252 DCHECK_LE(unassigned_job_count_, jobs_.size());
1253}
1254
[email protected]aed99ef02010-08-26 14:04:321255void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101256 SanityCheck();
1257
[email protected]aed99ef02010-08-26 14:04:321258 // Delete active jobs.
1259 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101260 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321261
1262 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411263 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321264}
1265
[email protected]d80a4322009-08-14 07:07:491266} // namespace internal
1267
[email protected]ff579d42009-06-24 15:47:021268} // namespace net