blob: 7a029093127cbf113f7ebbb11df5c0cb0749cc86 [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
[email protected]5e6efa52011-06-27 17:26:417#include <math.h>
[email protected]ff579d42009-06-24 15:47:028#include "base/compiler_specific.h"
[email protected]fd4fe0b2010-02-08 23:02:159#include "base/format_macros.h"
[email protected]5e6efa52011-06-27 17:26:4110#include "base/logging.h"
[email protected]ff579d42009-06-24 15:47:0211#include "base/message_loop.h"
[email protected]835d7c82010-10-14 04:38:3812#include "base/metrics/stats_counters.h"
[email protected]7286e3fc2011-07-19 22:13:2413#include "base/stl_util.h"
[email protected]5e6efa52011-06-27 17:26:4114#include "base/string_number_conversions.h"
[email protected]fd4fe0b2010-02-08 23:02:1515#include "base/string_util.h"
[email protected]ff579d42009-06-24 15:47:0216#include "base/time.h"
[email protected]9349cfb2010-08-31 18:00:5317#include "base/values.h"
[email protected]9e743cd2010-03-16 07:03:5318#include "net/base/net_log.h"
[email protected]ff579d42009-06-24 15:47:0219#include "net/base/net_errors.h"
20#include "net/socket/client_socket_handle.h"
21
22using base::TimeDelta;
23
24namespace {
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]5e6efa52011-06-27 17:26:4142double g_socket_reuse_policy_penalty_exponent = -1;
43int g_socket_reuse_policy = -1;
44
[email protected]ff579d42009-06-24 15:47:0245} // namespace
46
47namespace net {
48
[email protected]5e6efa52011-06-27 17:26:4149int GetSocketReusePolicy() {
50 return g_socket_reuse_policy;
51}
52
53void SetSocketReusePolicy(int policy) {
54 DCHECK_GE(policy, 0);
55 DCHECK_LE(policy, 2);
56 if (policy > 2 || policy < 0) {
57 LOG(ERROR) << "Invalid socket reuse policy";
58 return;
59 }
60
61 double exponents[] = { 0, 0.25, -1 };
62 g_socket_reuse_policy_penalty_exponent = exponents[policy];
63 g_socket_reuse_policy = policy;
64
65 VLOG(1) << "Setting g_socket_reuse_policy_penalty_exponent = "
66 << g_socket_reuse_policy_penalty_exponent;
67}
68
[email protected]2ab05b52009-07-01 23:57:5869ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3470 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3071 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5372 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5873 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3474 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5875 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0276 net_log_(net_log),
[email protected]8159a1c2012-06-07 00:00:1077 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5878 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5879 DCHECK(delegate);
[email protected]e9d7d252013-01-04 02:33:1780 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB,
81 NetLog::StringCallback("group_name", &group_name_));
[email protected]2ab05b52009-07-01 23:57:5882}
83
[email protected]fd7b7c92009-08-20 19:38:3084ConnectJob::~ConnectJob() {
[email protected]3aa4af042012-06-14 21:02:3185 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB);
[email protected]fd7b7c92009-08-20 19:38:3086}
[email protected]2ab05b52009-07-01 23:57:5887
[email protected]974ebd62009-08-03 23:14:3488int ConnectJob::Connect() {
89 if (timeout_duration_ != base::TimeDelta())
[email protected]d323a172011-09-02 18:23:0290 timer_.Start(FROM_HERE, timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3091
[email protected]a2006ece2010-04-23 16:44:0292 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3093
[email protected]06650c52010-06-03 00:49:1794 LogConnectStart();
95
[email protected]fd7b7c92009-08-20 19:38:3096 int rv = ConnectInternal();
97
98 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:1799 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30100 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:30101 }
102
103 return rv;
104}
105
[email protected]3268023f2011-05-05 00:08:10106void ConnectJob::set_socket(StreamSocket* socket) {
[email protected]06650c52010-06-03 00:49:17107 if (socket) {
[email protected]3aa4af042012-06-14 21:02:31108 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
109 socket->NetLog().source().ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17110 }
111 socket_.reset(socket);
112}
113
[email protected]fd7b7c92009-08-20 19:38:30114void ConnectJob::NotifyDelegateOfCompletion(int rv) {
115 // The delegate will delete |this|.
116 Delegate *delegate = delegate_;
117 delegate_ = NULL;
118
[email protected]06650c52010-06-03 00:49:17119 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30120 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34121}
122
[email protected]a796bcec2010-03-22 17:17:26123void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
124 timer_.Stop();
[email protected]d323a172011-09-02 18:23:02125 timer_.Start(FROM_HERE, remaining_time, this, &ConnectJob::OnTimeout);
[email protected]a796bcec2010-03-22 17:17:26126}
127
[email protected]06650c52010-06-03 00:49:17128void ConnectJob::LogConnectStart() {
[email protected]034df0f32013-01-07 23:17:48129 connect_timing_.connect_start = base::TimeTicks::Now();
[email protected]e9d7d252013-01-04 02:33:17130 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT);
[email protected]06650c52010-06-03 00:49:17131}
132
133void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]034df0f32013-01-07 23:17:48134 connect_timing_.connect_end = base::TimeTicks::Now();
[email protected]d7fd1782011-02-08 19:16:43135 net_log().EndEventWithNetErrorCode(
136 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17137}
138
[email protected]974ebd62009-08-03 23:14:34139void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40140 // Make sure the socket is NULL before calling into |delegate|.
141 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30142
[email protected]3aa4af042012-06-14 21:02:31143 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT);
[email protected]fd7b7c92009-08-20 19:38:30144
145 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34146}
147
[email protected]d80a4322009-08-14 07:07:49148namespace internal {
149
[email protected]fd4fe0b2010-02-08 23:02:15150ClientSocketPoolBaseHelper::Request::Request(
151 ClientSocketHandle* handle,
[email protected]49639fa2011-12-20 23:22:41152 const CompletionCallback& callback,
[email protected]fd4fe0b2010-02-08 23:02:15153 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20154 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03155 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53156 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13157 : handle_(handle),
158 callback_(callback),
159 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20160 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03161 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53162 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15163
164ClientSocketPoolBaseHelper::Request::~Request() {}
165
[email protected]d80a4322009-08-14 07:07:49166ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53167 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02168 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16169 base::TimeDelta unused_idle_socket_timeout,
170 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38171 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02172 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53173 connecting_socket_count_(0),
174 handed_out_socket_count_(0),
175 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02176 max_sockets_per_group_(max_sockets_per_group),
[email protected]64770b7d2011-11-16 04:30:41177 use_cleanup_timer_(g_cleanup_timer_enabled),
[email protected]9bf28db2009-08-29 01:35:16178 unused_idle_socket_timeout_(unused_idle_socket_timeout),
179 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35180 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22181 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13182 pool_generation_number_(0),
[email protected]49639fa2011-12-20 23:22:41183 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
[email protected]211d2172009-07-22 15:48:53184 DCHECK_LE(0, max_sockets_per_group);
185 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52186
[email protected]232a5812011-03-04 22:42:08187 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53188}
[email protected]ff579d42009-06-24 15:47:02189
[email protected]d80a4322009-08-14 07:07:49190ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13191 // Clean up any idle sockets and pending connect jobs. Assert that we have no
192 // remaining active sockets or pending requests. They should have all been
193 // cleaned up prior to |this| being destroyed.
[email protected]7af985a2012-12-14 22:40:42194 FlushWithError(ERR_ABORTED);
[email protected]2431756e2010-09-29 20:26:13195 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21196 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29197 DCHECK_EQ(0, connecting_socket_count_);
[email protected]51fdc7c2012-04-10 19:19:48198 CHECK(higher_layer_pools_.empty());
[email protected]a554a8262010-05-20 00:13:52199
[email protected]232a5812011-03-04 22:42:08200 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02201}
202
[email protected]2a848e0e2012-08-09 22:27:31203ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair()
204 : result(OK) {
205}
206
207ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair(
208 const CompletionCallback& callback_in, int result_in)
209 : callback(callback_in),
210 result(result_in) {
211}
212
[email protected]49639fa2011-12-20 23:22:41213ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {}
214
[email protected]ff579d42009-06-24 15:47:02215// InsertRequestIntoQueue inserts the request into the queue based on
216// priority. Highest priorities are closest to the front. Older requests are
217// prioritized over requests of equal priority.
218//
219// static
[email protected]d80a4322009-08-14 07:07:49220void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
221 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02222 RequestQueue::iterator it = pending_requests->begin();
[email protected]31ae7ab2012-04-24 21:09:05223 while (it != pending_requests->end() && r->priority() <= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02224 ++it;
225 pending_requests->insert(it, r);
226}
227
[email protected]fd7b7c92009-08-20 19:38:30228// static
229const ClientSocketPoolBaseHelper::Request*
230ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05231 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30232 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02233 group->mutable_pending_requests()->erase(it);
234 // If there are no more requests, we kill the backup timer.
235 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00236 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30237 return req;
238}
239
[email protected]51fdc7c2012-04-10 19:19:48240void ClientSocketPoolBaseHelper::AddLayeredPool(LayeredPool* pool) {
241 CHECK(pool);
242 CHECK(!ContainsKey(higher_layer_pools_, pool));
243 higher_layer_pools_.insert(pool);
244}
245
246void ClientSocketPoolBaseHelper::RemoveLayeredPool(LayeredPool* pool) {
247 CHECK(pool);
248 CHECK(ContainsKey(higher_layer_pools_, pool));
249 higher_layer_pools_.erase(pool);
250}
251
[email protected]d80a4322009-08-14 07:07:49252int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02253 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49254 const Request* request) {
[email protected]49639fa2011-12-20 23:22:41255 CHECK(!request->callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03256 CHECK(request->handle());
257
[email protected]64770b7d2011-11-16 04:30:41258 // Cleanup any timed-out idle sockets if no timer is used.
259 if (!use_cleanup_timer_)
260 CleanupIdleSockets(false);
261
[email protected]3aa4af042012-06-14 21:02:31262 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]aed99ef02010-08-26 14:04:32263 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26264
[email protected]fd4fe0b2010-02-08 23:02:15265 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17266 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43267 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21268 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17269 delete request;
270 } else {
[email protected]aed99ef02010-08-26 14:04:32271 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]e7e99322010-05-04 23:30:17272 }
[email protected]fd4fe0b2010-02-08 23:02:15273 return rv;
274}
275
[email protected]2c2bef152010-10-13 00:55:03276void ClientSocketPoolBaseHelper::RequestSockets(
277 const std::string& group_name,
278 const Request& request,
279 int num_sockets) {
[email protected]49639fa2011-12-20 23:22:41280 DCHECK(request.callback().is_null());
[email protected]2c2bef152010-10-13 00:55:03281 DCHECK(!request.handle());
282
[email protected]64770b7d2011-11-16 04:30:41283 // Cleanup any timed out idle sockets if no timer is used.
284 if (!use_cleanup_timer_)
285 CleanupIdleSockets(false);
286
[email protected]2c2bef152010-10-13 00:55:03287 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03288 num_sockets = max_sockets_per_group_;
289 }
290
291 request.net_log().BeginEvent(
292 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]3aa4af042012-06-14 21:02:31293 NetLog::IntegerCallback("num_sockets", num_sockets));
[email protected]2c2bef152010-10-13 00:55:03294
295 Group* group = GetOrCreateGroup(group_name);
296
[email protected]3c819f522010-12-02 02:03:12297 // RequestSocketsInternal() may delete the group.
298 bool deleted_group = false;
299
[email protected]d7fd1782011-02-08 19:16:43300 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03301 for (int num_iterations_left = num_sockets;
302 group->NumActiveSocketSlots() < num_sockets &&
303 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43304 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03305 if (rv < 0 && rv != ERR_IO_PENDING) {
306 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12307 if (!ContainsKey(group_map_, group_name))
308 deleted_group = true;
309 break;
310 }
311 if (!ContainsKey(group_map_, group_name)) {
312 // Unexpected. The group should only be getting deleted on synchronous
313 // error.
314 NOTREACHED();
315 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03316 break;
317 }
318 }
319
[email protected]3c819f522010-12-02 02:03:12320 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03321 RemoveGroup(group_name);
322
[email protected]d7fd1782011-02-08 19:16:43323 if (rv == ERR_IO_PENDING)
324 rv = OK;
325 request.net_log().EndEventWithNetErrorCode(
326 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03327}
328
[email protected]fd4fe0b2010-02-08 23:02:15329int ClientSocketPoolBaseHelper::RequestSocketInternal(
330 const std::string& group_name,
331 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49332 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03333 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32334 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02335
[email protected]2c2bef152010-10-13 00:55:03336 if (!(request->flags() & NO_IDLE_SOCKETS)) {
337 // Try to reuse a socket.
[email protected]8159a1c2012-06-07 00:00:10338 if (AssignIdleSocketToRequest(request, group))
[email protected]2c2bef152010-10-13 00:55:03339 return OK;
340 }
341
[email protected]8159a1c2012-06-07 00:00:10342 // If there are more ConnectJobs than pending requests, don't need to do
343 // anything. Can just wait for the extra job to connect, and then assign it
344 // to the request.
345 if (!preconnecting && group->TryToUseUnassignedConnectJob())
[email protected]2c2bef152010-10-13 00:55:03346 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10347
[email protected]43a21b82010-06-10 21:30:54348 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20349 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
350 !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48351 // TODO(willchan): Consider whether or not we need to close a socket in a
352 // higher layered group. I don't think this makes sense since we would just
353 // reuse that socket then if we needed one and wouldn't make it down to this
354 // layer.
[email protected]43a21b82010-06-10 21:30:54355 request->net_log().AddEvent(
[email protected]3aa4af042012-06-14 21:02:31356 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP);
[email protected]43a21b82010-06-10 21:30:54357 return ERR_IO_PENDING;
358 }
359
[email protected]5acdce12011-03-30 13:00:20360 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]51fdc7c2012-04-10 19:19:48361 // NOTE(mmenke): Wonder if we really need different code for each case
362 // here. Only reason for them now seems to be preconnects.
[email protected]43a21b82010-06-10 21:30:54363 if (idle_socket_count() > 0) {
[email protected]51fdc7c2012-04-10 19:19:48364 // There's an idle socket in this pool. Either that's because there's
365 // still one in this group, but we got here due to preconnecting bypassing
366 // idle sockets, or because there's an idle socket in another group.
[email protected]dcbe168a2010-12-02 03:14:46367 bool closed = CloseOneIdleSocketExceptInGroup(group);
368 if (preconnecting && !closed)
369 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54370 } else {
[email protected]40dee91a2012-04-17 19:25:46371 // We could check if we really have a stalled group here, but it requires
372 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]3aa4af042012-06-14 21:02:31373 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS);
[email protected]40dee91a2012-04-17 19:25:46374 return ERR_IO_PENDING;
[email protected]43a21b82010-06-10 21:30:54375 }
376 }
377
[email protected]51fdc7c2012-04-10 19:19:48378 // We couldn't find a socket to reuse, and there's space to allocate one,
379 // so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58380 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17381 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02382
[email protected]2ab05b52009-07-01 23:57:58383 int rv = connect_job->Connect();
384 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17385 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03386 if (!preconnecting) {
387 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]034df0f32013-01-07 23:17:48388 connect_job->connect_timing(), handle, base::TimeDelta(),
389 group, request->net_log());
[email protected]2c2bef152010-10-13 00:55:03390 } else {
391 AddIdleSocket(connect_job->ReleaseSocket(), group);
392 }
[email protected]2ab05b52009-07-01 23:57:58393 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32394 // If we don't have any sockets in this group, set a timer for potentially
395 // creating a new one. If the SYN is lost, this backup socket may complete
396 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03397 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00398 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32399 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03400 }
[email protected]6b624c62010-03-14 08:37:32401
[email protected]211d2172009-07-22 15:48:53402 connecting_socket_count_++;
403
[email protected]8159a1c2012-06-07 00:00:10404 group->AddJob(connect_job.release(), preconnecting);
[email protected]a2006ece2010-04-23 16:44:02405 } else {
[email protected]06650c52010-06-03 00:49:17406 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]3268023f2011-05-05 00:08:10407 StreamSocket* error_socket = NULL;
[email protected]fd2e53e2011-01-14 20:40:52408 if (!preconnecting) {
409 DCHECK(handle);
410 connect_job->GetAdditionalErrorState(handle);
411 error_socket = connect_job->ReleaseSocket();
412 }
[email protected]e772db3f2010-07-12 18:11:13413 if (error_socket) {
[email protected]034df0f32013-01-07 23:17:48414 HandOutSocket(error_socket, false /* not reused */,
415 connect_job->connect_timing(), handle, base::TimeDelta(),
416 group, request->net_log());
[email protected]aed99ef02010-08-26 14:04:32417 } else if (group->IsEmpty()) {
418 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21419 }
[email protected]2ab05b52009-07-01 23:57:58420 }
[email protected]ff579d42009-06-24 15:47:02421
[email protected]2ab05b52009-07-01 23:57:58422 return rv;
[email protected]ff579d42009-06-24 15:47:02423}
424
[email protected]8159a1c2012-06-07 00:00:10425bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest(
[email protected]aed99ef02010-08-26 14:04:32426 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22427 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
428 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
[email protected]5e6efa52011-06-27 17:26:41429 double max_score = -1;
[email protected]e1b54dc2010-10-06 21:27:22430
431 // Iterate through the idle sockets forwards (oldest to newest)
432 // * Delete any disconnected ones.
433 // * If we find a used idle socket, assign to |idle_socket|. At the end,
434 // the |idle_socket_it| will be set to the newest used idle socket.
435 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
436 it != idle_sockets->end();) {
437 if (!it->socket->IsConnectedAndIdle()) {
438 DecrementIdleCount();
439 delete it->socket;
440 it = idle_sockets->erase(it);
441 continue;
[email protected]eb5a99382010-07-11 03:18:26442 }
[email protected]e1b54dc2010-10-06 21:27:22443
444 if (it->socket->WasEverUsed()) {
445 // We found one we can reuse!
[email protected]5e6efa52011-06-27 17:26:41446 double score = 0;
447 int64 bytes_read = it->socket->NumBytesRead();
448 double num_kb = static_cast<double>(bytes_read) / 1024.0;
449 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds();
450 idle_time_sec = std::max(1, idle_time_sec);
451
452 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) {
453 score = num_kb / pow(idle_time_sec,
454 g_socket_reuse_policy_penalty_exponent);
455 }
456
457 // Equality to prefer recently used connection.
458 if (score >= max_score) {
459 idle_socket_it = it;
460 max_score = score;
461 }
[email protected]e1b54dc2010-10-06 21:27:22462 }
463
464 ++it;
[email protected]eb5a99382010-07-11 03:18:26465 }
[email protected]e1b54dc2010-10-06 21:27:22466
467 // If we haven't found an idle socket, that means there are no used idle
468 // sockets. Pick the oldest (first) idle socket (FIFO).
469
470 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
471 idle_socket_it = idle_sockets->begin();
472
473 if (idle_socket_it != idle_sockets->end()) {
474 DecrementIdleCount();
475 base::TimeDelta idle_time =
476 base::TimeTicks::Now() - idle_socket_it->start_time;
477 IdleSocket idle_socket = *idle_socket_it;
478 idle_sockets->erase(idle_socket_it);
479 HandOutSocket(
480 idle_socket.socket,
481 idle_socket.socket->WasEverUsed(),
[email protected]034df0f32013-01-07 23:17:48482 LoadTimingInfo::ConnectTiming(),
[email protected]e1b54dc2010-10-06 21:27:22483 request->handle(),
484 idle_time,
485 group,
486 request->net_log());
487 return true;
488 }
489
[email protected]eb5a99382010-07-11 03:18:26490 return false;
491}
492
[email protected]06650c52010-06-03 00:49:17493// static
494void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
495 const NetLog::Source& connect_job_source, const Request* request) {
[email protected]3aa4af042012-06-14 21:02:31496 request->net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
497 connect_job_source.ToEventParametersCallback());
[email protected]06650c52010-06-03 00:49:17498}
499
[email protected]d80a4322009-08-14 07:07:49500void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21501 const std::string& group_name, ClientSocketHandle* handle) {
502 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
503 if (callback_it != pending_callback_map_.end()) {
504 int result = callback_it->second.result;
505 pending_callback_map_.erase(callback_it);
[email protected]3268023f2011-05-05 00:08:10506 StreamSocket* socket = handle->release_socket();
[email protected]05ea9ff2010-07-15 19:08:21507 if (socket) {
508 if (result != OK)
509 socket->Disconnect();
510 ReleaseSocket(handle->group_name(), socket, handle->id());
511 }
512 return;
513 }
[email protected]b6501d3d2010-06-03 23:53:34514
[email protected]ff579d42009-06-24 15:47:02515 CHECK(ContainsKey(group_map_, group_name));
516
[email protected]aed99ef02010-08-26 14:04:32517 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02518
[email protected]ff579d42009-06-24 15:47:02519 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32520 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
521 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49522 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03523 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]3aa4af042012-06-14 21:02:31524 req->net_log().AddEvent(NetLog::TYPE_CANCELLED);
525 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]eb5a99382010-07-11 03:18:26526
527 // We let the job run, unless we're at the socket limit.
[email protected]aed99ef02010-08-26 14:04:32528 if (group->jobs().size() && ReachedMaxSocketsLimit()) {
529 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]ff579d42009-06-24 15:47:02569 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32570 RequestQueue::const_iterator it = group.pending_requests().begin();
571 for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49572 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32573 if (i < group.jobs().size()) {
[email protected]5fc08e32009-07-15 17:09:57574 LoadState max_state = LOAD_STATE_IDLE;
[email protected]aed99ef02010-08-26 14:04:32575 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
576 job_it != group.jobs().end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21577 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57578 }
579 return max_state;
580 } else {
581 // TODO(wtc): Add a state for being on the wait list.
[email protected]dba9bd22012-12-06 23:04:03582 // See http://crbug.com/5077.
[email protected]5fc08e32009-07-15 17:09:57583 return LOAD_STATE_IDLE;
584 }
[email protected]ff579d42009-06-24 15:47:02585 }
586 }
587
588 NOTREACHED();
589 return LOAD_STATE_IDLE;
590}
591
[email protected]ba00b492010-09-08 14:53:38592DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27593 const std::string& name, const std::string& type) const {
594 DictionaryValue* dict = new DictionaryValue();
595 dict->SetString("name", name);
596 dict->SetString("type", type);
597 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
598 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
599 dict->SetInteger("idle_socket_count", idle_socket_count_);
600 dict->SetInteger("max_socket_count", max_sockets_);
601 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
602 dict->SetInteger("pool_generation_number", pool_generation_number_);
603
604 if (group_map_.empty())
605 return dict;
606
607 DictionaryValue* all_groups_dict = new DictionaryValue();
608 for (GroupMap::const_iterator it = group_map_.begin();
609 it != group_map_.end(); it++) {
610 const Group* group = it->second;
611 DictionaryValue* group_dict = new DictionaryValue();
612
613 group_dict->SetInteger("pending_request_count",
614 group->pending_requests().size());
615 if (!group->pending_requests().empty()) {
616 group_dict->SetInteger("top_pending_priority",
617 group->TopPendingPriority());
618 }
619
620 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07621
622 ListValue* idle_socket_list = new ListValue();
[email protected]e1b54dc2010-10-06 21:27:22623 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07624 for (idle_socket = group->idle_sockets().begin();
625 idle_socket != group->idle_sockets().end();
626 idle_socket++) {
627 int source_id = idle_socket->socket->NetLog().source().id;
628 idle_socket_list->Append(Value::CreateIntegerValue(source_id));
629 }
630 group_dict->Set("idle_sockets", idle_socket_list);
631
632 ListValue* connect_jobs_list = new ListValue();
[email protected]2c2bef152010-10-13 00:55:03633 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07634 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
635 int source_id = (*job)->net_log().source().id;
636 connect_jobs_list->Append(Value::CreateIntegerValue(source_id));
637 }
638 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27639
640 group_dict->SetBoolean("is_stalled",
[email protected]51fdc7c2012-04-10 19:19:48641 group->IsStalledOnPoolMaxSockets(
642 max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00643 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27644
645 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
646 }
647 dict->Set("groups", all_groups_dict);
648 return dict;
649}
650
[email protected]d80a4322009-08-14 07:07:49651bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16652 base::TimeTicks now,
653 base::TimeDelta timeout) const {
654 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01655 if (timed_out)
656 return true;
657 if (socket->WasEverUsed())
658 return !socket->IsConnectedAndIdle();
659 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02660}
661
[email protected]d80a4322009-08-14 07:07:49662void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02663 if (idle_socket_count_ == 0)
664 return;
665
666 // Current time value. Retrieving it once at the function start rather than
667 // inside the inner loop, since it shouldn't change by any meaningful amount.
668 base::TimeTicks now = base::TimeTicks::Now();
669
670 GroupMap::iterator i = group_map_.begin();
671 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32672 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02673
[email protected]e1b54dc2010-10-06 21:27:22674 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32675 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16676 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01677 j->socket->WasEverUsed() ?
678 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16679 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02680 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32681 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02682 DecrementIdleCount();
683 } else {
684 ++j;
685 }
686 }
687
688 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32689 if (group->IsEmpty()) {
690 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02691 } else {
692 ++i;
693 }
694 }
695}
696
[email protected]aed99ef02010-08-26 14:04:32697ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
698 const std::string& group_name) {
699 GroupMap::iterator it = group_map_.find(group_name);
700 if (it != group_map_.end())
701 return it->second;
702 Group* group = new Group;
703 group_map_[group_name] = group;
704 return group;
705}
706
707void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
708 GroupMap::iterator it = group_map_.find(group_name);
709 CHECK(it != group_map_.end());
710
711 RemoveGroup(it);
712}
713
714void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
715 delete it->second;
716 group_map_.erase(it);
717}
718
[email protected]06d94042010-08-25 01:45:22719// static
[email protected]2d6728692011-03-12 01:39:55720bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
721 return g_connect_backup_jobs_enabled;
722}
723
724// static
[email protected]636b8252011-04-08 19:56:54725bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
726 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22727 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54728 return old_value;
[email protected]06d94042010-08-25 01:45:22729}
730
731void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
732 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
733}
734
[email protected]d80a4322009-08-14 07:07:49735void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]64770b7d2011-11-16 04:30:41736 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
737 StartIdleSocketTimer();
[email protected]ff579d42009-06-24 15:47:02738}
739
[email protected]d80a4322009-08-14 07:07:49740void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02741 if (--idle_socket_count_ == 0)
742 timer_.Stop();
743}
744
[email protected]64770b7d2011-11-16 04:30:41745// static
746bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
747 return g_cleanup_timer_enabled;
748}
749
750// static
751bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) {
752 bool old_value = g_cleanup_timer_enabled;
753 g_cleanup_timer_enabled = enabled;
754 return old_value;
755}
756
757void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
758 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
759 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
760}
761
[email protected]eb5a99382010-07-11 03:18:26762void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]3268023f2011-05-05 00:08:10763 StreamSocket* socket,
[email protected]eb5a99382010-07-11 03:18:26764 int id) {
[email protected]ff579d42009-06-24 15:47:02765 GroupMap::iterator i = group_map_.find(group_name);
766 CHECK(i != group_map_.end());
767
[email protected]aed99ef02010-08-26 14:04:32768 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02769
[email protected]b1f031dd2010-03-02 23:19:33770 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53771 handed_out_socket_count_--;
772
[email protected]aed99ef02010-08-26 14:04:32773 CHECK_GT(group->active_socket_count(), 0);
774 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02775
[email protected]a7e38572010-06-07 18:22:24776 const bool can_reuse = socket->IsConnectedAndIdle() &&
777 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02778 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26779 // Add it to the idle list.
[email protected]0f873e82010-09-02 16:09:01780 AddIdleSocket(socket, group);
[email protected]aed99ef02010-08-26 14:04:32781 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02782 } else {
783 delete socket;
784 }
[email protected]05ea9ff2010-07-15 19:08:21785
[email protected]eb5a99382010-07-11 03:18:26786 CheckForStalledSocketGroups();
787}
[email protected]ff579d42009-06-24 15:47:02788
[email protected]eb5a99382010-07-11 03:18:26789void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
790 // If we have idle sockets, see if we can give one to the top-stalled group.
791 std::string top_group_name;
792 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21793 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26794 return;
[email protected]4f2abec2010-02-03 18:10:16795
[email protected]eb5a99382010-07-11 03:18:26796 if (ReachedMaxSocketsLimit()) {
797 if (idle_socket_count() > 0) {
798 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54799 } else {
[email protected]eb5a99382010-07-11 03:18:26800 // We can't activate more sockets since we're already at our global
801 // limit.
[email protected]4f2abec2010-02-03 18:10:16802 return;
[email protected]d7027bb2010-05-10 18:58:54803 }
[email protected]4f2abec2010-02-03 18:10:16804 }
[email protected]eb5a99382010-07-11 03:18:26805
806 // Note: we don't loop on waking stalled groups. If the stalled group is at
807 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21808 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26809 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21810 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02811}
812
[email protected]211d2172009-07-22 15:48:53813// Search for the highest priority pending request, amongst the groups that
814// are not at the |max_sockets_per_group_| limit. Note: for requests with
815// the same priority, the winner is based on group hash ordering (and not
816// insertion order).
[email protected]51fdc7c2012-04-10 19:19:48817bool ClientSocketPoolBaseHelper::FindTopStalledGroup(
818 Group** group,
819 std::string* group_name) const {
820 CHECK((group && group_name) || (!group && !group_name));
[email protected]211d2172009-07-22 15:48:53821 Group* top_group = NULL;
822 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21823 bool has_stalled_group = false;
[email protected]51fdc7c2012-04-10 19:19:48824 for (GroupMap::const_iterator i = group_map_.begin();
[email protected]211d2172009-07-22 15:48:53825 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32826 Group* curr_group = i->second;
827 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53828 if (queue.empty())
829 continue;
[email protected]51fdc7c2012-04-10 19:19:48830 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) {
831 if (!group)
832 return true;
[email protected]05ea9ff2010-07-15 19:08:21833 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41834 bool has_higher_priority = !top_group ||
[email protected]31ae7ab2012-04-24 21:09:05835 curr_group->TopPendingPriority() > top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41836 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32837 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41838 top_group_name = &i->first;
839 }
[email protected]211d2172009-07-22 15:48:53840 }
841 }
[email protected]05ea9ff2010-07-15 19:08:21842
[email protected]211d2172009-07-22 15:48:53843 if (top_group) {
[email protected]51fdc7c2012-04-10 19:19:48844 CHECK(group);
[email protected]211d2172009-07-22 15:48:53845 *group = top_group;
846 *group_name = *top_group_name;
[email protected]51fdc7c2012-04-10 19:19:48847 } else {
848 CHECK(!has_stalled_group);
[email protected]211d2172009-07-22 15:48:53849 }
[email protected]05ea9ff2010-07-15 19:08:21850 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53851}
852
[email protected]d80a4322009-08-14 07:07:49853void ClientSocketPoolBaseHelper::OnConnectJobComplete(
854 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58855 DCHECK_NE(ERR_IO_PENDING, result);
856 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02857 GroupMap::iterator group_it = group_map_.find(group_name);
858 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32859 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02860
[email protected]3268023f2011-05-05 00:08:10861 scoped_ptr<StreamSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02862
[email protected]034df0f32013-01-07 23:17:48863 // Copies of these are needed because |job| may be deleted before they are
864 // accessed.
[email protected]9e743cd2010-03-16 07:03:53865 BoundNetLog job_log = job->net_log();
[email protected]034df0f32013-01-07 23:17:48866 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing();
[email protected]5fc08e32009-07-15 17:09:57867
[email protected]4d3b05d2010-01-27 21:27:29868 if (result == OK) {
869 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32870 RemoveConnectJob(job, group);
871 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29872 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02873 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17874 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29875 HandOutSocket(
[email protected]034df0f32013-01-07 23:17:48876 socket.release(), false /* unused socket */, connect_timing,
877 r->handle(), base::TimeDelta(), group, r->net_log());
[email protected]3aa4af042012-06-14 21:02:31878 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL);
[email protected]05ea9ff2010-07-15 19:08:21879 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57880 } else {
[email protected]0f873e82010-09-02 16:09:01881 AddIdleSocket(socket.release(), group);
[email protected]aed99ef02010-08-26 14:04:32882 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21883 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57884 }
[email protected]94c20472010-01-14 08:14:36885 } else {
[email protected]e772db3f2010-07-12 18:11:13886 // If we got a socket, it must contain error information so pass that
887 // up so that the caller can retrieve it.
888 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32889 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29890 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02891 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17892 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18893 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32894 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13895 if (socket.get()) {
896 handed_out_socket = true;
[email protected]034df0f32013-01-07 23:17:48897 HandOutSocket(socket.release(), false /* unused socket */,
898 connect_timing, r->handle(), base::TimeDelta(), group,
899 r->net_log());
[email protected]e772db3f2010-07-12 18:11:13900 }
[email protected]034df0f32013-01-07 23:17:48901 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, result);
[email protected]05ea9ff2010-07-15 19:08:21902 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18903 } else {
[email protected]aed99ef02010-08-26 14:04:32904 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29905 }
[email protected]05ea9ff2010-07-15 19:08:21906 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32907 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21908 CheckForStalledSocketGroups();
909 }
[email protected]ff579d42009-06-24 15:47:02910 }
[email protected]ff579d42009-06-24 15:47:02911}
912
[email protected]66761b952010-06-25 21:30:38913void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
[email protected]7af985a2012-12-14 22:40:42914 FlushWithError(ERR_NETWORK_CHANGED);
[email protected]66761b952010-06-25 21:30:38915}
916
[email protected]7af985a2012-12-14 22:40:42917void ClientSocketPoolBaseHelper::FlushWithError(int error) {
[email protected]a7e38572010-06-07 18:22:24918 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14919 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52920 CloseIdleSockets();
[email protected]7af985a2012-12-14 22:40:42921 CancelAllRequestsWithError(error);
[email protected]a554a8262010-05-20 00:13:52922}
923
[email protected]51fdc7c2012-04-10 19:19:48924bool ClientSocketPoolBaseHelper::IsStalled() const {
925 // If we are not using |max_sockets_|, then clearly we are not stalled
926 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_)
927 return false;
928 // So in order to be stalled we need to be using |max_sockets_| AND
929 // we need to have a request that is actually stalled on the global
930 // socket limit. To find such a request, we look for a group that
931 // a has more requests that jobs AND where the number of jobs is less
932 // than |max_sockets_per_group_|. (If the number of jobs is equal to
933 // |max_sockets_per_group_|, then the request is stalled on the group,
934 // which does not count.)
935 for (GroupMap::const_iterator it = group_map_.begin();
936 it != group_map_.end(); ++it) {
937 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_))
938 return true;
939 }
940 return false;
941}
942
[email protected]2c2bef152010-10-13 00:55:03943void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29944 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33945 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53946 connecting_socket_count_--;
947
[email protected]25eea382010-07-10 23:55:26948 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32949 DCHECK(ContainsKey(group->jobs(), job));
950 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26951
952 // If we've got no more jobs for this group, then we no longer need a
953 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32954 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00955 group->CleanupBackupJob();
[email protected]25eea382010-07-10 23:55:26956
[email protected]8ae03f42010-07-07 19:08:10957 DCHECK(job);
958 delete job;
[email protected]2ab05b52009-07-01 23:57:58959}
[email protected]ff579d42009-06-24 15:47:02960
[email protected]8ae03f42010-07-07 19:08:10961void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21962 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51963 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21964 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32965 RemoveGroup(group_name);
966 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51967 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10968}
[email protected]06650c52010-06-03 00:49:17969
[email protected]8ae03f42010-07-07 19:08:10970void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21971 const std::string& group_name, Group* group) {
972 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32973 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21974 if (rv != ERR_IO_PENDING) {
975 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02976 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51977 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32978 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10979
[email protected]d7fd1782011-02-08 19:16:43980 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]64770b7d2011-11-16 04:30:41981 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58982 }
983}
984
[email protected]d80a4322009-08-14 07:07:49985void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]3268023f2011-05-05 00:08:10986 StreamSocket* socket,
[email protected]2ab05b52009-07-01 23:57:58987 bool reused,
[email protected]034df0f32013-01-07 23:17:48988 const LoadTimingInfo::ConnectTiming& connect_timing,
[email protected]2ab05b52009-07-01 23:57:58989 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29990 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15991 Group* group,
[email protected]9e743cd2010-03-16 07:03:53992 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58993 DCHECK(socket);
994 handle->set_socket(socket);
995 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29996 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24997 handle->set_pool_id(pool_generation_number_);
[email protected]034df0f32013-01-07 23:17:48998 handle->set_connect_timing(connect_timing);
[email protected]211d2172009-07-22 15:48:53999
[email protected]d13f51b2010-04-27 23:20:451000 if (reused) {
[email protected]ec11be62010-04-28 19:28:091001 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:451002 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]3aa4af042012-06-14 21:02:311003 NetLog::IntegerCallback(
1004 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:151005 }
[email protected]d13f51b2010-04-27 23:20:451006
[email protected]06650c52010-06-03 00:49:171007 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
[email protected]3aa4af042012-06-14 21:02:311008 socket->NetLog().source().ToEventParametersCallback());
[email protected]fd4fe0b2010-02-08 23:02:151009
[email protected]211d2172009-07-22 15:48:531010 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:321011 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:021012}
1013
[email protected]d80a4322009-08-14 07:07:491014void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]3268023f2011-05-05 00:08:101015 StreamSocket* socket, Group* group) {
[email protected]5fc08e32009-07-15 17:09:571016 DCHECK(socket);
1017 IdleSocket idle_socket;
1018 idle_socket.socket = socket;
1019 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:571020
[email protected]aed99ef02010-08-26 14:04:321021 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:571022 IncrementIdleCount();
1023}
1024
[email protected]d80a4322009-08-14 07:07:491025void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:541026 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:321027 Group* group = i->second;
1028 connecting_socket_count_ -= group->jobs().size();
1029 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:321030
[email protected]5fc08e32009-07-15 17:09:571031 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:321032 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:141033 // RemoveGroup() will call .erase() which will invalidate the iterator,
1034 // but i will already have been incremented to a valid iterator before
1035 // RemoveGroup() is called.
1036 RemoveGroup(i++);
1037 } else {
1038 ++i;
1039 }
1040 }
1041 DCHECK_EQ(0, connecting_socket_count_);
1042}
1043
[email protected]7af985a2012-12-14 22:40:421044void ClientSocketPoolBaseHelper::CancelAllRequestsWithError(int error) {
[email protected]06f92462010-08-31 19:24:141045 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
1046 Group* group = i->second;
1047
1048 RequestQueue pending_requests;
1049 pending_requests.swap(*group->mutable_pending_requests());
1050 for (RequestQueue::iterator it2 = pending_requests.begin();
1051 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:031052 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:141053 InvokeUserCallbackLater(
[email protected]7af985a2012-12-14 22:40:421054 request->handle(), request->callback(), error);
[email protected]06f92462010-08-31 19:24:141055 }
1056
1057 // Delete group if no longer needed.
1058 if (group->IsEmpty()) {
1059 // RemoveGroup() will call .erase() which will invalidate the iterator,
1060 // but i will already have been incremented to a valid iterator before
1061 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:321062 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:541063 } else {
1064 ++i;
[email protected]5fc08e32009-07-15 17:09:571065 }
1066 }
1067}
1068
[email protected]d80a4322009-08-14 07:07:491069bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:531070 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:541071 int total = handed_out_socket_count_ + connecting_socket_count_ +
1072 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:201073 // There can be more sockets than the limit since some requests can ignore
1074 // the limit
[email protected]c901f6d2010-04-27 17:48:281075 if (total < max_sockets_)
1076 return false;
[email protected]c901f6d2010-04-27 17:48:281077 return true;
[email protected]211d2172009-07-22 15:48:531078}
1079
[email protected]51fdc7c2012-04-10 19:19:481080bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
1081 if (idle_socket_count() == 0)
1082 return false;
1083 return CloseOneIdleSocketExceptInGroup(NULL);
[email protected]dcbe168a2010-12-02 03:14:461084}
1085
1086bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1087 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541088 CHECK_GT(idle_socket_count(), 0);
1089
1090 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321091 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461092 if (exception_group == group)
1093 continue;
[email protected]e1b54dc2010-10-06 21:27:221094 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541095
[email protected]e1b54dc2010-10-06 21:27:221096 if (!idle_sockets->empty()) {
1097 delete idle_sockets->front().socket;
1098 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541099 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321100 if (group->IsEmpty())
1101 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541102
[email protected]dcbe168a2010-12-02 03:14:461103 return true;
[email protected]43a21b82010-06-10 21:30:541104 }
1105 }
1106
[email protected]51fdc7c2012-04-10 19:19:481107 return false;
1108}
[email protected]dcbe168a2010-12-02 03:14:461109
[email protected]51fdc7c2012-04-10 19:19:481110bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInLayeredPool() {
1111 // This pool doesn't have any idle sockets. It's possible that a pool at a
1112 // higher layer is holding one of this sockets active, but it's actually idle.
1113 // Query the higher layers.
1114 for (std::set<LayeredPool*>::const_iterator it = higher_layer_pools_.begin();
1115 it != higher_layer_pools_.end(); ++it) {
1116 if ((*it)->CloseOneIdleConnection())
1117 return true;
1118 }
[email protected]dcbe168a2010-12-02 03:14:461119 return false;
[email protected]43a21b82010-06-10 21:30:541120}
1121
[email protected]05ea9ff2010-07-15 19:08:211122void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
[email protected]49639fa2011-12-20 23:22:411123 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) {
[email protected]05ea9ff2010-07-15 19:08:211124 CHECK(!ContainsKey(pending_callback_map_, handle));
1125 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
1126 MessageLoop::current()->PostTask(
1127 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411128 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback,
1129 weak_factory_.GetWeakPtr(), handle));
[email protected]05ea9ff2010-07-15 19:08:211130}
1131
1132void ClientSocketPoolBaseHelper::InvokeUserCallback(
1133 ClientSocketHandle* handle) {
1134 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1135
1136 // Exit if the request has already been cancelled.
1137 if (it == pending_callback_map_.end())
1138 return;
1139
1140 CHECK(!handle->is_initialized());
[email protected]49639fa2011-12-20 23:22:411141 CompletionCallback callback = it->second.callback;
[email protected]05ea9ff2010-07-15 19:08:211142 int result = it->second.result;
1143 pending_callback_map_.erase(it);
[email protected]49639fa2011-12-20 23:22:411144 callback.Run(result);
[email protected]05ea9ff2010-07-15 19:08:211145}
1146
[email protected]aed99ef02010-08-26 14:04:321147ClientSocketPoolBaseHelper::Group::Group()
[email protected]8159a1c2012-06-07 00:00:101148 : unassigned_job_count_(0),
1149 active_socket_count_(0),
[email protected]96b939a2012-01-20 21:52:151150 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {}
[email protected]aed99ef02010-08-26 14:04:321151
[email protected]e4d9a9722011-05-12 00:16:001152ClientSocketPoolBaseHelper::Group::~Group() {
1153 CleanupBackupJob();
[email protected]8159a1c2012-06-07 00:00:101154 DCHECK_EQ(0u, unassigned_job_count_);
[email protected]aed99ef02010-08-26 14:04:321155}
1156
1157void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1158 const std::string& group_name,
1159 ClientSocketPoolBaseHelper* pool) {
1160 // Only allow one timer pending to create a backup socket.
[email protected]49639fa2011-12-20 23:22:411161 if (weak_factory_.HasWeakPtrs())
[email protected]aed99ef02010-08-26 14:04:321162 return;
1163
1164 MessageLoop::current()->PostDelayedTask(
1165 FROM_HERE,
[email protected]49639fa2011-12-20 23:22:411166 base::Bind(&Group::OnBackupSocketTimerFired, weak_factory_.GetWeakPtr(),
1167 group_name, pool),
[email protected]26b9973962012-01-28 00:57:001168 pool->ConnectRetryInterval());
[email protected]aed99ef02010-08-26 14:04:321169}
1170
[email protected]8159a1c2012-06-07 00:00:101171bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() {
1172 SanityCheck();
1173
1174 if (unassigned_job_count_ == 0)
1175 return false;
1176 --unassigned_job_count_;
1177 return true;
1178}
1179
1180void ClientSocketPoolBaseHelper::Group::AddJob(ConnectJob* job,
1181 bool is_preconnect) {
1182 SanityCheck();
1183
1184 if (is_preconnect)
1185 ++unassigned_job_count_;
1186 jobs_.insert(job);
1187}
1188
1189void ClientSocketPoolBaseHelper::Group::RemoveJob(ConnectJob* job) {
1190 SanityCheck();
1191
1192 jobs_.erase(job);
1193 size_t job_count = jobs_.size();
1194 if (job_count < unassigned_job_count_)
1195 unassigned_job_count_ = job_count;
[email protected]2c2bef152010-10-13 00:55:031196}
1197
[email protected]aed99ef02010-08-26 14:04:321198void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1199 std::string group_name,
1200 ClientSocketPoolBaseHelper* pool) {
1201 // If there are no more jobs pending, there is no work to do.
1202 // If we've done our cleanups correctly, this should not happen.
1203 if (jobs_.empty()) {
1204 NOTREACHED();
1205 return;
1206 }
1207
[email protected]8159a1c2012-06-07 00:00:101208 // If our old job is waiting on DNS, or if we can't create any sockets
[email protected]aed99ef02010-08-26 14:04:321209 // right now due to limits, just reset the timer.
1210 if (pool->ReachedMaxSocketsLimit() ||
1211 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1212 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1213 StartBackupSocketTimer(group_name, pool);
1214 return;
1215 }
1216
[email protected]a9fc8fc2011-05-10 02:41:071217 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441218 return;
[email protected]4baaf9d2010-08-31 15:15:441219
[email protected]aed99ef02010-08-26 14:04:321220 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
1221 group_name, **pending_requests_.begin(), pool);
[email protected]3aa4af042012-06-14 21:02:311222 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED);
[email protected]aed99ef02010-08-26 14:04:321223 SIMPLE_STATS_COUNTER("socket.backup_created");
1224 int rv = backup_job->Connect();
1225 pool->connecting_socket_count_++;
[email protected]8159a1c2012-06-07 00:00:101226 AddJob(backup_job, false);
[email protected]aed99ef02010-08-26 14:04:321227 if (rv != ERR_IO_PENDING)
1228 pool->OnConnectJobComplete(rv, backup_job);
1229}
1230
[email protected]8159a1c2012-06-07 00:00:101231void ClientSocketPoolBaseHelper::Group::SanityCheck() {
1232 DCHECK_LE(unassigned_job_count_, jobs_.size());
1233}
1234
[email protected]aed99ef02010-08-26 14:04:321235void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
[email protected]8159a1c2012-06-07 00:00:101236 SanityCheck();
1237
[email protected]aed99ef02010-08-26 14:04:321238 // Delete active jobs.
1239 STLDeleteElements(&jobs_);
[email protected]8159a1c2012-06-07 00:00:101240 unassigned_job_count_ = 0;
[email protected]aed99ef02010-08-26 14:04:321241
1242 // Cancel pending backup job.
[email protected]49639fa2011-12-20 23:22:411243 weak_factory_.InvalidateWeakPtrs();
[email protected]aed99ef02010-08-26 14:04:321244}
1245
[email protected]d80a4322009-08-14 07:07:491246} // namespace internal
1247
[email protected]ff579d42009-06-24 15:47:021248} // namespace net