blob: d39890b4e1c0ea987d56d1238a304208bb1b787e [file] [log] [blame]
[email protected]5acdce12011-03-30 13:00:201// Copyright (c) 2011 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]ff579d42009-06-24 15:47:0213#include "base/stl_util-inl.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
26// The timeout value, in seconds, used to clean up idle sockets that can't be
27// reused.
28//
29// Note: It's important to close idle sockets that have received data as soon
30// as possible because the received data may cause BSOD on Windows XP under
31// some conditions. See http://crbug.com/4606.
32const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
33
[email protected]c847c2a2011-04-08 13:56:1434// Indicate whether or not we should establish a new transport layer connection
35// after a certain timeout has passed without receiving an ACK.
[email protected]06d94042010-08-25 01:45:2236bool g_connect_backup_jobs_enabled = true;
37
[email protected]5e6efa52011-06-27 17:26:4138double g_socket_reuse_policy_penalty_exponent = -1;
39int g_socket_reuse_policy = -1;
40
[email protected]ff579d42009-06-24 15:47:0241} // namespace
42
43namespace net {
44
[email protected]5e6efa52011-06-27 17:26:4145int GetSocketReusePolicy() {
46 return g_socket_reuse_policy;
47}
48
49void SetSocketReusePolicy(int policy) {
50 DCHECK_GE(policy, 0);
51 DCHECK_LE(policy, 2);
52 if (policy > 2 || policy < 0) {
53 LOG(ERROR) << "Invalid socket reuse policy";
54 return;
55 }
56
57 double exponents[] = { 0, 0.25, -1 };
58 g_socket_reuse_policy_penalty_exponent = exponents[policy];
59 g_socket_reuse_policy = policy;
60
61 VLOG(1) << "Setting g_socket_reuse_policy_penalty_exponent = "
62 << g_socket_reuse_policy_penalty_exponent;
63}
64
[email protected]2ab05b52009-07-01 23:57:5865ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3466 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3067 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5368 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5869 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3470 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5871 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0272 net_log_(net_log),
[email protected]2c2bef152010-10-13 00:55:0373 idle_(true),
74 preconnect_state_(NOT_PRECONNECT) {
[email protected]2ab05b52009-07-01 23:57:5875 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5876 DCHECK(delegate);
[email protected]06650c52010-06-03 00:49:1777 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL);
[email protected]2ab05b52009-07-01 23:57:5878}
79
[email protected]fd7b7c92009-08-20 19:38:3080ConnectJob::~ConnectJob() {
[email protected]06650c52010-06-03 00:49:1781 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL);
[email protected]fd7b7c92009-08-20 19:38:3082}
[email protected]2ab05b52009-07-01 23:57:5883
[email protected]2c2bef152010-10-13 00:55:0384void ConnectJob::Initialize(bool is_preconnect) {
85 if (is_preconnect)
86 preconnect_state_ = UNUSED_PRECONNECT;
87 else
88 preconnect_state_ = NOT_PRECONNECT;
89}
90
[email protected]974ebd62009-08-03 23:14:3491int ConnectJob::Connect() {
92 if (timeout_duration_ != base::TimeDelta())
93 timer_.Start(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]2c2bef152010-10-13 00:55:03109void ConnectJob::UseForNormalRequest() {
110 DCHECK_EQ(UNUSED_PRECONNECT, preconnect_state_);
111 preconnect_state_ = USED_PRECONNECT;
112}
113
[email protected]3268023f2011-05-05 00:08:10114void ConnectJob::set_socket(StreamSocket* socket) {
[email protected]06650c52010-06-03 00:49:17115 if (socket) {
[email protected]00cd9c42010-11-02 20:15:57116 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET, make_scoped_refptr(
117 new NetLogSourceParameter("source_dependency",
118 socket->NetLog().source())));
[email protected]06650c52010-06-03 00:49:17119 }
120 socket_.reset(socket);
121}
122
[email protected]fd7b7c92009-08-20 19:38:30123void ConnectJob::NotifyDelegateOfCompletion(int rv) {
124 // The delegate will delete |this|.
125 Delegate *delegate = delegate_;
126 delegate_ = NULL;
127
[email protected]06650c52010-06-03 00:49:17128 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:30129 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:34130}
131
[email protected]a796bcec2010-03-22 17:17:26132void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
133 timer_.Stop();
134 timer_.Start(remaining_time, this, &ConnectJob::OnTimeout);
135}
136
[email protected]06650c52010-06-03 00:49:17137void ConnectJob::LogConnectStart() {
138 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT,
[email protected]00cd9c42010-11-02 20:15:57139 make_scoped_refptr(new NetLogStringParameter("group_name", group_name_)));
[email protected]06650c52010-06-03 00:49:17140}
141
142void ConnectJob::LogConnectCompletion(int net_error) {
[email protected]d7fd1782011-02-08 19:16:43143 net_log().EndEventWithNetErrorCode(
144 NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error);
[email protected]06650c52010-06-03 00:49:17145}
146
[email protected]974ebd62009-08-03 23:14:34147void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40148 // Make sure the socket is NULL before calling into |delegate|.
149 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30150
[email protected]ec11be62010-04-28 19:28:09151 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT, NULL);
[email protected]fd7b7c92009-08-20 19:38:30152
153 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34154}
155
[email protected]d80a4322009-08-14 07:07:49156namespace internal {
157
[email protected]fd4fe0b2010-02-08 23:02:15158ClientSocketPoolBaseHelper::Request::Request(
159 ClientSocketHandle* handle,
160 CompletionCallback* callback,
161 RequestPriority priority,
[email protected]5acdce12011-03-30 13:00:20162 bool ignore_limits,
[email protected]2c2bef152010-10-13 00:55:03163 Flags flags,
[email protected]9e743cd2010-03-16 07:03:53164 const BoundNetLog& net_log)
[email protected]2431756e2010-09-29 20:26:13165 : handle_(handle),
166 callback_(callback),
167 priority_(priority),
[email protected]5acdce12011-03-30 13:00:20168 ignore_limits_(ignore_limits),
[email protected]2c2bef152010-10-13 00:55:03169 flags_(flags),
[email protected]9e743cd2010-03-16 07:03:53170 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15171
172ClientSocketPoolBaseHelper::Request::~Request() {}
173
[email protected]d80a4322009-08-14 07:07:49174ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53175 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02176 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16177 base::TimeDelta unused_idle_socket_timeout,
178 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38179 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02180 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53181 connecting_socket_count_(0),
182 handed_out_socket_count_(0),
183 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02184 max_sockets_per_group_(max_sockets_per_group),
[email protected]9bf28db2009-08-29 01:35:16185 unused_idle_socket_timeout_(unused_idle_socket_timeout),
186 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35187 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22188 connect_backup_jobs_enabled_(false),
[email protected]2431756e2010-09-29 20:26:13189 pool_generation_number_(0),
190 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
[email protected]211d2172009-07-22 15:48:53191 DCHECK_LE(0, max_sockets_per_group);
192 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52193
[email protected]232a5812011-03-04 22:42:08194 NetworkChangeNotifier::AddIPAddressObserver(this);
[email protected]211d2172009-07-22 15:48:53195}
[email protected]ff579d42009-06-24 15:47:02196
[email protected]d80a4322009-08-14 07:07:49197ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]2431756e2010-09-29 20:26:13198 // Clean up any idle sockets and pending connect jobs. Assert that we have no
199 // remaining active sockets or pending requests. They should have all been
200 // cleaned up prior to |this| being destroyed.
201 Flush();
202 DCHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21203 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29204 DCHECK_EQ(0, connecting_socket_count_);
[email protected]a554a8262010-05-20 00:13:52205
[email protected]232a5812011-03-04 22:42:08206 NetworkChangeNotifier::RemoveIPAddressObserver(this);
[email protected]ff579d42009-06-24 15:47:02207}
208
209// InsertRequestIntoQueue inserts the request into the queue based on
210// priority. Highest priorities are closest to the front. Older requests are
211// prioritized over requests of equal priority.
212//
213// static
[email protected]d80a4322009-08-14 07:07:49214void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
215 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02216 RequestQueue::iterator it = pending_requests->begin();
[email protected]ac790b42009-12-02 04:31:31217 while (it != pending_requests->end() && r->priority() >= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02218 ++it;
219 pending_requests->insert(it, r);
220}
221
[email protected]fd7b7c92009-08-20 19:38:30222// static
223const ClientSocketPoolBaseHelper::Request*
224ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
[email protected]417da102011-03-11 17:45:05225 const RequestQueue::iterator& it, Group* group) {
[email protected]fd7b7c92009-08-20 19:38:30226 const Request* req = *it;
[email protected]3f00be82010-09-27 19:50:02227 group->mutable_pending_requests()->erase(it);
228 // If there are no more requests, we kill the backup timer.
229 if (group->pending_requests().empty())
[email protected]e4d9a9722011-05-12 00:16:00230 group->CleanupBackupJob();
[email protected]fd7b7c92009-08-20 19:38:30231 return req;
232}
233
[email protected]d80a4322009-08-14 07:07:49234int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02235 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49236 const Request* request) {
[email protected]2c2bef152010-10-13 00:55:03237 CHECK(request->callback());
238 CHECK(request->handle());
239
[email protected]ec11be62010-04-28 19:28:09240 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]aed99ef02010-08-26 14:04:32241 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26242
[email protected]fd4fe0b2010-02-08 23:02:15243 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17244 if (rv != ERR_IO_PENDING) {
[email protected]d7fd1782011-02-08 19:16:43245 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21246 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17247 delete request;
248 } else {
[email protected]aed99ef02010-08-26 14:04:32249 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]e7e99322010-05-04 23:30:17250 }
[email protected]fd4fe0b2010-02-08 23:02:15251 return rv;
252}
253
[email protected]2c2bef152010-10-13 00:55:03254void ClientSocketPoolBaseHelper::RequestSockets(
255 const std::string& group_name,
256 const Request& request,
257 int num_sockets) {
258 DCHECK(!request.callback());
259 DCHECK(!request.handle());
260
261 if (num_sockets > max_sockets_per_group_) {
[email protected]2c2bef152010-10-13 00:55:03262 num_sockets = max_sockets_per_group_;
263 }
264
265 request.net_log().BeginEvent(
266 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
[email protected]00cd9c42010-11-02 20:15:57267 make_scoped_refptr(new NetLogIntegerParameter(
268 "num_sockets", num_sockets)));
[email protected]2c2bef152010-10-13 00:55:03269
270 Group* group = GetOrCreateGroup(group_name);
271
[email protected]3c819f522010-12-02 02:03:12272 // RequestSocketsInternal() may delete the group.
273 bool deleted_group = false;
274
[email protected]d7fd1782011-02-08 19:16:43275 int rv = OK;
[email protected]2c2bef152010-10-13 00:55:03276 for (int num_iterations_left = num_sockets;
277 group->NumActiveSocketSlots() < num_sockets &&
278 num_iterations_left > 0 ; num_iterations_left--) {
[email protected]d7fd1782011-02-08 19:16:43279 rv = RequestSocketInternal(group_name, &request);
[email protected]2c2bef152010-10-13 00:55:03280 if (rv < 0 && rv != ERR_IO_PENDING) {
281 // We're encountering a synchronous error. Give up.
[email protected]3c819f522010-12-02 02:03:12282 if (!ContainsKey(group_map_, group_name))
283 deleted_group = true;
284 break;
285 }
286 if (!ContainsKey(group_map_, group_name)) {
287 // Unexpected. The group should only be getting deleted on synchronous
288 // error.
289 NOTREACHED();
290 deleted_group = true;
[email protected]2c2bef152010-10-13 00:55:03291 break;
292 }
293 }
294
[email protected]3c819f522010-12-02 02:03:12295 if (!deleted_group && group->IsEmpty())
[email protected]2c2bef152010-10-13 00:55:03296 RemoveGroup(group_name);
297
[email protected]d7fd1782011-02-08 19:16:43298 if (rv == ERR_IO_PENDING)
299 rv = OK;
300 request.net_log().EndEventWithNetErrorCode(
301 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
[email protected]2c2bef152010-10-13 00:55:03302}
303
[email protected]fd4fe0b2010-02-08 23:02:15304int ClientSocketPoolBaseHelper::RequestSocketInternal(
305 const std::string& group_name,
306 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49307 DCHECK_GE(request->priority(), 0);
[email protected]d80a4322009-08-14 07:07:49308 ClientSocketHandle* const handle = request->handle();
[email protected]2c2bef152010-10-13 00:55:03309 const bool preconnecting = !handle;
[email protected]aed99ef02010-08-26 14:04:32310 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02311
[email protected]2c2bef152010-10-13 00:55:03312 if (!(request->flags() & NO_IDLE_SOCKETS)) {
313 // Try to reuse a socket.
314 if (AssignIdleSocketToGroup(request, group))
315 return OK;
316 }
317
318 if (!preconnecting && group->TryToUsePreconnectConnectJob())
319 return ERR_IO_PENDING;
[email protected]65552102010-04-09 22:58:10320
[email protected]43a21b82010-06-10 21:30:54321 // Can we make another active socket now?
[email protected]5acdce12011-03-30 13:00:20322 if (!group->HasAvailableSocketSlot(max_sockets_per_group_) &&
323 !request->ignore_limits()) {
[email protected]43a21b82010-06-10 21:30:54324 request->net_log().AddEvent(
325 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
326 return ERR_IO_PENDING;
327 }
328
[email protected]5acdce12011-03-30 13:00:20329 if (ReachedMaxSocketsLimit() && !request->ignore_limits()) {
[email protected]43a21b82010-06-10 21:30:54330 if (idle_socket_count() > 0) {
[email protected]dcbe168a2010-12-02 03:14:46331 bool closed = CloseOneIdleSocketExceptInGroup(group);
332 if (preconnecting && !closed)
333 return ERR_PRECONNECT_MAX_SOCKET_LIMIT;
[email protected]43a21b82010-06-10 21:30:54334 } else {
335 // We could check if we really have a stalled group here, but it requires
336 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]43a21b82010-06-10 21:30:54337 request->net_log().AddEvent(
338 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
339 return ERR_IO_PENDING;
340 }
341 }
342
[email protected]ff579d42009-06-24 15:47:02343 // We couldn't find a socket to reuse, so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58344 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17345 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02346
[email protected]2c2bef152010-10-13 00:55:03347 connect_job->Initialize(preconnecting);
[email protected]2ab05b52009-07-01 23:57:58348 int rv = connect_job->Connect();
349 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17350 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2c2bef152010-10-13 00:55:03351 if (!preconnecting) {
352 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
353 handle, base::TimeDelta(), group, request->net_log());
354 } else {
355 AddIdleSocket(connect_job->ReleaseSocket(), group);
356 }
[email protected]2ab05b52009-07-01 23:57:58357 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32358 // If we don't have any sockets in this group, set a timer for potentially
359 // creating a new one. If the SYN is lost, this backup socket may complete
360 // before the slow socket, improving end user latency.
[email protected]2c2bef152010-10-13 00:55:03361 if (connect_backup_jobs_enabled_ &&
[email protected]e4d9a9722011-05-12 00:16:00362 group->IsEmpty() && !group->HasBackupJob()) {
[email protected]aed99ef02010-08-26 14:04:32363 group->StartBackupSocketTimer(group_name, this);
[email protected]2c2bef152010-10-13 00:55:03364 }
[email protected]6b624c62010-03-14 08:37:32365
[email protected]211d2172009-07-22 15:48:53366 connecting_socket_count_++;
367
[email protected]aed99ef02010-08-26 14:04:32368 group->AddJob(connect_job.release());
[email protected]a2006ece2010-04-23 16:44:02369 } else {
[email protected]06650c52010-06-03 00:49:17370 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]3268023f2011-05-05 00:08:10371 StreamSocket* error_socket = NULL;
[email protected]fd2e53e2011-01-14 20:40:52372 if (!preconnecting) {
373 DCHECK(handle);
374 connect_job->GetAdditionalErrorState(handle);
375 error_socket = connect_job->ReleaseSocket();
376 }
[email protected]e772db3f2010-07-12 18:11:13377 if (error_socket) {
378 HandOutSocket(error_socket, false /* not reused */, handle,
[email protected]aed99ef02010-08-26 14:04:32379 base::TimeDelta(), group, request->net_log());
380 } else if (group->IsEmpty()) {
381 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21382 }
[email protected]2ab05b52009-07-01 23:57:58383 }
[email protected]ff579d42009-06-24 15:47:02384
[email protected]2ab05b52009-07-01 23:57:58385 return rv;
[email protected]ff579d42009-06-24 15:47:02386}
387
[email protected]05ea9ff2010-07-15 19:08:21388bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup(
[email protected]aed99ef02010-08-26 14:04:32389 const Request* request, Group* group) {
[email protected]e1b54dc2010-10-06 21:27:22390 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
391 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end();
[email protected]5e6efa52011-06-27 17:26:41392 double max_score = -1;
[email protected]e1b54dc2010-10-06 21:27:22393
394 // Iterate through the idle sockets forwards (oldest to newest)
395 // * Delete any disconnected ones.
396 // * If we find a used idle socket, assign to |idle_socket|. At the end,
397 // the |idle_socket_it| will be set to the newest used idle socket.
398 for (std::list<IdleSocket>::iterator it = idle_sockets->begin();
399 it != idle_sockets->end();) {
400 if (!it->socket->IsConnectedAndIdle()) {
401 DecrementIdleCount();
402 delete it->socket;
403 it = idle_sockets->erase(it);
404 continue;
[email protected]eb5a99382010-07-11 03:18:26405 }
[email protected]e1b54dc2010-10-06 21:27:22406
407 if (it->socket->WasEverUsed()) {
408 // We found one we can reuse!
[email protected]5e6efa52011-06-27 17:26:41409 double score = 0;
410 int64 bytes_read = it->socket->NumBytesRead();
411 double num_kb = static_cast<double>(bytes_read) / 1024.0;
412 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds();
413 idle_time_sec = std::max(1, idle_time_sec);
414
415 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) {
416 score = num_kb / pow(idle_time_sec,
417 g_socket_reuse_policy_penalty_exponent);
418 }
419
420 // Equality to prefer recently used connection.
421 if (score >= max_score) {
422 idle_socket_it = it;
423 max_score = score;
424 }
[email protected]e1b54dc2010-10-06 21:27:22425 }
426
427 ++it;
[email protected]eb5a99382010-07-11 03:18:26428 }
[email protected]e1b54dc2010-10-06 21:27:22429
430 // If we haven't found an idle socket, that means there are no used idle
431 // sockets. Pick the oldest (first) idle socket (FIFO).
432
433 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
434 idle_socket_it = idle_sockets->begin();
435
436 if (idle_socket_it != idle_sockets->end()) {
437 DecrementIdleCount();
438 base::TimeDelta idle_time =
439 base::TimeTicks::Now() - idle_socket_it->start_time;
440 IdleSocket idle_socket = *idle_socket_it;
441 idle_sockets->erase(idle_socket_it);
442 HandOutSocket(
443 idle_socket.socket,
444 idle_socket.socket->WasEverUsed(),
445 request->handle(),
446 idle_time,
447 group,
448 request->net_log());
449 return true;
450 }
451
[email protected]eb5a99382010-07-11 03:18:26452 return false;
453}
454
[email protected]06650c52010-06-03 00:49:17455// static
456void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
457 const NetLog::Source& connect_job_source, const Request* request) {
458 request->net_log().AddEvent(
459 NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
[email protected]00cd9c42010-11-02 20:15:57460 make_scoped_refptr(new NetLogSourceParameter(
461 "source_dependency", connect_job_source)));
[email protected]06650c52010-06-03 00:49:17462}
463
[email protected]d80a4322009-08-14 07:07:49464void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21465 const std::string& group_name, ClientSocketHandle* handle) {
466 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
467 if (callback_it != pending_callback_map_.end()) {
468 int result = callback_it->second.result;
469 pending_callback_map_.erase(callback_it);
[email protected]3268023f2011-05-05 00:08:10470 StreamSocket* socket = handle->release_socket();
[email protected]05ea9ff2010-07-15 19:08:21471 if (socket) {
472 if (result != OK)
473 socket->Disconnect();
474 ReleaseSocket(handle->group_name(), socket, handle->id());
475 }
476 return;
477 }
[email protected]b6501d3d2010-06-03 23:53:34478
[email protected]ff579d42009-06-24 15:47:02479 CHECK(ContainsKey(group_map_, group_name));
480
[email protected]aed99ef02010-08-26 14:04:32481 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02482
[email protected]ff579d42009-06-24 15:47:02483 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32484 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
485 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49486 if ((*it)->handle() == handle) {
[email protected]2c2bef152010-10-13 00:55:03487 scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group));
[email protected]ec11be62010-04-28 19:28:09488 req->net_log().AddEvent(NetLog::TYPE_CANCELLED, NULL);
489 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]eb5a99382010-07-11 03:18:26490
491 // We let the job run, unless we're at the socket limit.
[email protected]aed99ef02010-08-26 14:04:32492 if (group->jobs().size() && ReachedMaxSocketsLimit()) {
493 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26494 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34495 }
[email protected]eb5a99382010-07-11 03:18:26496 break;
[email protected]ff579d42009-06-24 15:47:02497 }
498 }
[email protected]ff579d42009-06-24 15:47:02499}
500
[email protected]2abfe90a2010-08-25 17:49:51501bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
502 return ContainsKey(group_map_, group_name);
503}
504
[email protected]d80a4322009-08-14 07:07:49505void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02506 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14507 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02508}
509
[email protected]d80a4322009-08-14 07:07:49510int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02511 const std::string& group_name) const {
512 GroupMap::const_iterator i = group_map_.find(group_name);
513 CHECK(i != group_map_.end());
514
[email protected]aed99ef02010-08-26 14:04:32515 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02516}
517
[email protected]d80a4322009-08-14 07:07:49518LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02519 const std::string& group_name,
520 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21521 if (ContainsKey(pending_callback_map_, handle))
522 return LOAD_STATE_CONNECTING;
523
[email protected]ff579d42009-06-24 15:47:02524 if (!ContainsKey(group_map_, group_name)) {
525 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
526 << " for handle: " << handle;
527 return LOAD_STATE_IDLE;
528 }
529
530 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32531 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02532
[email protected]ff579d42009-06-24 15:47:02533 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32534 RequestQueue::const_iterator it = group.pending_requests().begin();
535 for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49536 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32537 if (i < group.jobs().size()) {
[email protected]5fc08e32009-07-15 17:09:57538 LoadState max_state = LOAD_STATE_IDLE;
[email protected]aed99ef02010-08-26 14:04:32539 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
540 job_it != group.jobs().end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21541 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57542 }
543 return max_state;
544 } else {
545 // TODO(wtc): Add a state for being on the wait list.
546 // See http://www.crbug.com/5077.
547 return LOAD_STATE_IDLE;
548 }
[email protected]ff579d42009-06-24 15:47:02549 }
550 }
551
552 NOTREACHED();
553 return LOAD_STATE_IDLE;
554}
555
[email protected]ba00b492010-09-08 14:53:38556DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue(
[email protected]59d7a5a2010-08-30 16:44:27557 const std::string& name, const std::string& type) const {
558 DictionaryValue* dict = new DictionaryValue();
559 dict->SetString("name", name);
560 dict->SetString("type", type);
561 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
562 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
563 dict->SetInteger("idle_socket_count", idle_socket_count_);
564 dict->SetInteger("max_socket_count", max_sockets_);
565 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
566 dict->SetInteger("pool_generation_number", pool_generation_number_);
567
568 if (group_map_.empty())
569 return dict;
570
571 DictionaryValue* all_groups_dict = new DictionaryValue();
572 for (GroupMap::const_iterator it = group_map_.begin();
573 it != group_map_.end(); it++) {
574 const Group* group = it->second;
575 DictionaryValue* group_dict = new DictionaryValue();
576
577 group_dict->SetInteger("pending_request_count",
578 group->pending_requests().size());
579 if (!group->pending_requests().empty()) {
580 group_dict->SetInteger("top_pending_priority",
581 group->TopPendingPriority());
582 }
583
584 group_dict->SetInteger("active_socket_count", group->active_socket_count());
[email protected]0496f9a32010-09-30 16:08:07585
586 ListValue* idle_socket_list = new ListValue();
[email protected]e1b54dc2010-10-06 21:27:22587 std::list<IdleSocket>::const_iterator idle_socket;
[email protected]0496f9a32010-09-30 16:08:07588 for (idle_socket = group->idle_sockets().begin();
589 idle_socket != group->idle_sockets().end();
590 idle_socket++) {
591 int source_id = idle_socket->socket->NetLog().source().id;
592 idle_socket_list->Append(Value::CreateIntegerValue(source_id));
593 }
594 group_dict->Set("idle_sockets", idle_socket_list);
595
596 ListValue* connect_jobs_list = new ListValue();
[email protected]2c2bef152010-10-13 00:55:03597 std::set<ConnectJob*>::const_iterator job = group->jobs().begin();
[email protected]0496f9a32010-09-30 16:08:07598 for (job = group->jobs().begin(); job != group->jobs().end(); job++) {
599 int source_id = (*job)->net_log().source().id;
600 connect_jobs_list->Append(Value::CreateIntegerValue(source_id));
601 }
602 group_dict->Set("connect_jobs", connect_jobs_list);
[email protected]59d7a5a2010-08-30 16:44:27603
604 group_dict->SetBoolean("is_stalled",
605 group->IsStalled(max_sockets_per_group_));
[email protected]e4d9a9722011-05-12 00:16:00606 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
[email protected]59d7a5a2010-08-30 16:44:27607
608 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
609 }
610 dict->Set("groups", all_groups_dict);
611 return dict;
612}
613
[email protected]d80a4322009-08-14 07:07:49614bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16615 base::TimeTicks now,
616 base::TimeDelta timeout) const {
617 bool timed_out = (now - start_time) >= timeout;
[email protected]0f873e82010-09-02 16:09:01618 if (timed_out)
619 return true;
620 if (socket->WasEverUsed())
621 return !socket->IsConnectedAndIdle();
622 return !socket->IsConnected();
[email protected]ff579d42009-06-24 15:47:02623}
624
[email protected]d80a4322009-08-14 07:07:49625void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02626 if (idle_socket_count_ == 0)
627 return;
628
629 // Current time value. Retrieving it once at the function start rather than
630 // inside the inner loop, since it shouldn't change by any meaningful amount.
631 base::TimeTicks now = base::TimeTicks::Now();
632
633 GroupMap::iterator i = group_map_.begin();
634 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32635 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02636
[email protected]e1b54dc2010-10-06 21:27:22637 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
[email protected]aed99ef02010-08-26 14:04:32638 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16639 base::TimeDelta timeout =
[email protected]0f873e82010-09-02 16:09:01640 j->socket->WasEverUsed() ?
641 used_idle_socket_timeout_ : unused_idle_socket_timeout_;
[email protected]9bf28db2009-08-29 01:35:16642 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02643 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32644 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02645 DecrementIdleCount();
646 } else {
647 ++j;
648 }
649 }
650
651 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32652 if (group->IsEmpty()) {
653 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02654 } else {
655 ++i;
656 }
657 }
658}
659
[email protected]aed99ef02010-08-26 14:04:32660ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
661 const std::string& group_name) {
662 GroupMap::iterator it = group_map_.find(group_name);
663 if (it != group_map_.end())
664 return it->second;
665 Group* group = new Group;
666 group_map_[group_name] = group;
667 return group;
668}
669
670void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
671 GroupMap::iterator it = group_map_.find(group_name);
672 CHECK(it != group_map_.end());
673
674 RemoveGroup(it);
675}
676
677void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
678 delete it->second;
679 group_map_.erase(it);
680}
681
[email protected]06d94042010-08-25 01:45:22682// static
[email protected]2d6728692011-03-12 01:39:55683bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() {
684 return g_connect_backup_jobs_enabled;
685}
686
687// static
[email protected]636b8252011-04-08 19:56:54688bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
689 bool old_value = g_connect_backup_jobs_enabled;
[email protected]06d94042010-08-25 01:45:22690 g_connect_backup_jobs_enabled = enabled;
[email protected]636b8252011-04-08 19:56:54691 return old_value;
[email protected]06d94042010-08-25 01:45:22692}
693
694void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
695 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
696}
697
[email protected]d80a4322009-08-14 07:07:49698void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02699 if (++idle_socket_count_ == 1)
700 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
[email protected]d80a4322009-08-14 07:07:49701 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
[email protected]ff579d42009-06-24 15:47:02702}
703
[email protected]d80a4322009-08-14 07:07:49704void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02705 if (--idle_socket_count_ == 0)
706 timer_.Stop();
707}
708
[email protected]eb5a99382010-07-11 03:18:26709void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
[email protected]3268023f2011-05-05 00:08:10710 StreamSocket* socket,
[email protected]eb5a99382010-07-11 03:18:26711 int id) {
[email protected]ff579d42009-06-24 15:47:02712 GroupMap::iterator i = group_map_.find(group_name);
713 CHECK(i != group_map_.end());
714
[email protected]aed99ef02010-08-26 14:04:32715 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02716
[email protected]b1f031dd2010-03-02 23:19:33717 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53718 handed_out_socket_count_--;
719
[email protected]aed99ef02010-08-26 14:04:32720 CHECK_GT(group->active_socket_count(), 0);
721 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02722
[email protected]a7e38572010-06-07 18:22:24723 const bool can_reuse = socket->IsConnectedAndIdle() &&
724 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02725 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26726 // Add it to the idle list.
[email protected]0f873e82010-09-02 16:09:01727 AddIdleSocket(socket, group);
[email protected]aed99ef02010-08-26 14:04:32728 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02729 } else {
730 delete socket;
731 }
[email protected]05ea9ff2010-07-15 19:08:21732
[email protected]eb5a99382010-07-11 03:18:26733 CheckForStalledSocketGroups();
734}
[email protected]ff579d42009-06-24 15:47:02735
[email protected]eb5a99382010-07-11 03:18:26736void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
737 // If we have idle sockets, see if we can give one to the top-stalled group.
738 std::string top_group_name;
739 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21740 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26741 return;
[email protected]4f2abec2010-02-03 18:10:16742
[email protected]eb5a99382010-07-11 03:18:26743 if (ReachedMaxSocketsLimit()) {
744 if (idle_socket_count() > 0) {
745 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54746 } else {
[email protected]eb5a99382010-07-11 03:18:26747 // We can't activate more sockets since we're already at our global
748 // limit.
[email protected]4f2abec2010-02-03 18:10:16749 return;
[email protected]d7027bb2010-05-10 18:58:54750 }
[email protected]4f2abec2010-02-03 18:10:16751 }
[email protected]eb5a99382010-07-11 03:18:26752
753 // Note: we don't loop on waking stalled groups. If the stalled group is at
754 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21755 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26756 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21757 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02758}
759
[email protected]211d2172009-07-22 15:48:53760// Search for the highest priority pending request, amongst the groups that
761// are not at the |max_sockets_per_group_| limit. Note: for requests with
762// the same priority, the winner is based on group hash ordering (and not
763// insertion order).
[email protected]05ea9ff2010-07-15 19:08:21764bool ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
765 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53766 Group* top_group = NULL;
767 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21768 bool has_stalled_group = false;
[email protected]211d2172009-07-22 15:48:53769 for (GroupMap::iterator i = group_map_.begin();
770 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32771 Group* curr_group = i->second;
772 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53773 if (queue.empty())
774 continue;
[email protected]aed99ef02010-08-26 14:04:32775 if (curr_group->IsStalled(max_sockets_per_group_)) {
[email protected]05ea9ff2010-07-15 19:08:21776 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41777 bool has_higher_priority = !top_group ||
[email protected]aed99ef02010-08-26 14:04:32778 curr_group->TopPendingPriority() < top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41779 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32780 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41781 top_group_name = &i->first;
782 }
[email protected]211d2172009-07-22 15:48:53783 }
784 }
[email protected]05ea9ff2010-07-15 19:08:21785
[email protected]211d2172009-07-22 15:48:53786 if (top_group) {
787 *group = top_group;
788 *group_name = *top_group_name;
789 }
[email protected]05ea9ff2010-07-15 19:08:21790 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53791}
792
[email protected]d80a4322009-08-14 07:07:49793void ClientSocketPoolBaseHelper::OnConnectJobComplete(
794 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58795 DCHECK_NE(ERR_IO_PENDING, result);
796 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02797 GroupMap::iterator group_it = group_map_.find(group_name);
798 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32799 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02800
[email protected]3268023f2011-05-05 00:08:10801 scoped_ptr<StreamSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02802
[email protected]9e743cd2010-03-16 07:03:53803 BoundNetLog job_log = job->net_log();
[email protected]5fc08e32009-07-15 17:09:57804
[email protected]4d3b05d2010-01-27 21:27:29805 if (result == OK) {
806 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32807 RemoveConnectJob(job, group);
808 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29809 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02810 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17811 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29812 HandOutSocket(
813 socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32814 base::TimeDelta(), group, r->net_log());
[email protected]06650c52010-06-03 00:49:17815 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]05ea9ff2010-07-15 19:08:21816 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57817 } else {
[email protected]0f873e82010-09-02 16:09:01818 AddIdleSocket(socket.release(), group);
[email protected]aed99ef02010-08-26 14:04:32819 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21820 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57821 }
[email protected]94c20472010-01-14 08:14:36822 } else {
[email protected]e772db3f2010-07-12 18:11:13823 // If we got a socket, it must contain error information so pass that
824 // up so that the caller can retrieve it.
825 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32826 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29827 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02828 group->mutable_pending_requests()->begin(), group));
[email protected]06650c52010-06-03 00:49:17829 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18830 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32831 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13832 if (socket.get()) {
833 handed_out_socket = true;
834 HandOutSocket(socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32835 base::TimeDelta(), group, r->net_log());
[email protected]e772db3f2010-07-12 18:11:13836 }
[email protected]d7fd1782011-02-08 19:16:43837 r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL,
838 result);
[email protected]05ea9ff2010-07-15 19:08:21839 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18840 } else {
[email protected]aed99ef02010-08-26 14:04:32841 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29842 }
[email protected]05ea9ff2010-07-15 19:08:21843 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32844 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21845 CheckForStalledSocketGroups();
846 }
[email protected]ff579d42009-06-24 15:47:02847 }
[email protected]ff579d42009-06-24 15:47:02848}
849
[email protected]66761b952010-06-25 21:30:38850void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
851 Flush();
852}
853
[email protected]a7e38572010-06-07 18:22:24854void ClientSocketPoolBaseHelper::Flush() {
855 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14856 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52857 CloseIdleSockets();
[email protected]06f92462010-08-31 19:24:14858 AbortAllRequests();
[email protected]a554a8262010-05-20 00:13:52859}
860
[email protected]2c2bef152010-10-13 00:55:03861void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29862 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33863 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53864 connecting_socket_count_--;
865
[email protected]25eea382010-07-10 23:55:26866 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32867 DCHECK(ContainsKey(group->jobs(), job));
868 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26869
870 // If we've got no more jobs for this group, then we no longer need a
871 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32872 if (group->jobs().empty())
[email protected]e4d9a9722011-05-12 00:16:00873 group->CleanupBackupJob();
[email protected]25eea382010-07-10 23:55:26874
[email protected]8ae03f42010-07-07 19:08:10875 DCHECK(job);
876 delete job;
[email protected]2ab05b52009-07-01 23:57:58877}
[email protected]ff579d42009-06-24 15:47:02878
[email protected]8ae03f42010-07-07 19:08:10879void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21880 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51881 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21882 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32883 RemoveGroup(group_name);
884 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51885 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10886}
[email protected]06650c52010-06-03 00:49:17887
[email protected]8ae03f42010-07-07 19:08:10888void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21889 const std::string& group_name, Group* group) {
890 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32891 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21892 if (rv != ERR_IO_PENDING) {
893 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]3f00be82010-09-27 19:50:02894 group->mutable_pending_requests()->begin(), group));
[email protected]2abfe90a2010-08-25 17:49:51895 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32896 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10897
[email protected]d7fd1782011-02-08 19:16:43898 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
[email protected]05ea9ff2010-07-15 19:08:21899 InvokeUserCallbackLater(
900 request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58901 }
902}
903
[email protected]d80a4322009-08-14 07:07:49904void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]3268023f2011-05-05 00:08:10905 StreamSocket* socket,
[email protected]2ab05b52009-07-01 23:57:58906 bool reused,
907 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29908 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15909 Group* group,
[email protected]9e743cd2010-03-16 07:03:53910 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58911 DCHECK(socket);
912 handle->set_socket(socket);
913 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29914 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24915 handle->set_pool_id(pool_generation_number_);
[email protected]211d2172009-07-22 15:48:53916
[email protected]d13f51b2010-04-27 23:20:45917 if (reused) {
[email protected]ec11be62010-04-28 19:28:09918 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:45919 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]00cd9c42010-11-02 20:15:57920 make_scoped_refptr(new NetLogIntegerParameter(
921 "idle_ms", static_cast<int>(idle_time.InMilliseconds()))));
[email protected]fd4fe0b2010-02-08 23:02:15922 }
[email protected]d13f51b2010-04-27 23:20:45923
[email protected]06650c52010-06-03 00:49:17924 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
[email protected]00cd9c42010-11-02 20:15:57925 make_scoped_refptr(new NetLogSourceParameter(
926 "source_dependency", socket->NetLog().source())));
[email protected]fd4fe0b2010-02-08 23:02:15927
[email protected]211d2172009-07-22 15:48:53928 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:32929 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02930}
931
[email protected]d80a4322009-08-14 07:07:49932void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]3268023f2011-05-05 00:08:10933 StreamSocket* socket, Group* group) {
[email protected]5fc08e32009-07-15 17:09:57934 DCHECK(socket);
935 IdleSocket idle_socket;
936 idle_socket.socket = socket;
937 idle_socket.start_time = base::TimeTicks::Now();
[email protected]5fc08e32009-07-15 17:09:57938
[email protected]aed99ef02010-08-26 14:04:32939 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:57940 IncrementIdleCount();
941}
942
[email protected]d80a4322009-08-14 07:07:49943void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:54944 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:32945 Group* group = i->second;
946 connecting_socket_count_ -= group->jobs().size();
947 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:32948
[email protected]5fc08e32009-07-15 17:09:57949 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32950 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:14951 // RemoveGroup() will call .erase() which will invalidate the iterator,
952 // but i will already have been incremented to a valid iterator before
953 // RemoveGroup() is called.
954 RemoveGroup(i++);
955 } else {
956 ++i;
957 }
958 }
959 DCHECK_EQ(0, connecting_socket_count_);
960}
961
962void ClientSocketPoolBaseHelper::AbortAllRequests() {
963 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
964 Group* group = i->second;
965
966 RequestQueue pending_requests;
967 pending_requests.swap(*group->mutable_pending_requests());
968 for (RequestQueue::iterator it2 = pending_requests.begin();
969 it2 != pending_requests.end(); ++it2) {
[email protected]2c2bef152010-10-13 00:55:03970 scoped_ptr<const Request> request(*it2);
[email protected]06f92462010-08-31 19:24:14971 InvokeUserCallbackLater(
972 request->handle(), request->callback(), ERR_ABORTED);
973 }
974
975 // Delete group if no longer needed.
976 if (group->IsEmpty()) {
977 // RemoveGroup() will call .erase() which will invalidate the iterator,
978 // but i will already have been incremented to a valid iterator before
979 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:32980 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:54981 } else {
982 ++i;
[email protected]5fc08e32009-07-15 17:09:57983 }
984 }
985}
986
[email protected]d80a4322009-08-14 07:07:49987bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53988 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:54989 int total = handed_out_socket_count_ + connecting_socket_count_ +
990 idle_socket_count();
[email protected]5acdce12011-03-30 13:00:20991 // There can be more sockets than the limit since some requests can ignore
992 // the limit
[email protected]c901f6d2010-04-27 17:48:28993 if (total < max_sockets_)
994 return false;
[email protected]c901f6d2010-04-27 17:48:28995 return true;
[email protected]211d2172009-07-22 15:48:53996}
997
[email protected]43a21b82010-06-10 21:30:54998void ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
[email protected]dcbe168a2010-12-02 03:14:46999 CloseOneIdleSocketExceptInGroup(NULL);
1000}
1001
1002bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup(
1003 const Group* exception_group) {
[email protected]43a21b82010-06-10 21:30:541004 CHECK_GT(idle_socket_count(), 0);
1005
1006 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:321007 Group* group = i->second;
[email protected]dcbe168a2010-12-02 03:14:461008 if (exception_group == group)
1009 continue;
[email protected]e1b54dc2010-10-06 21:27:221010 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets();
[email protected]43a21b82010-06-10 21:30:541011
[email protected]e1b54dc2010-10-06 21:27:221012 if (!idle_sockets->empty()) {
1013 delete idle_sockets->front().socket;
1014 idle_sockets->pop_front();
[email protected]43a21b82010-06-10 21:30:541015 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:321016 if (group->IsEmpty())
1017 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:541018
[email protected]dcbe168a2010-12-02 03:14:461019 return true;
[email protected]43a21b82010-06-10 21:30:541020 }
1021 }
1022
[email protected]dcbe168a2010-12-02 03:14:461023 if (!exception_group)
1024 LOG(DFATAL) << "No idle socket found to close!.";
1025
1026 return false;
[email protected]43a21b82010-06-10 21:30:541027}
1028
[email protected]05ea9ff2010-07-15 19:08:211029void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
1030 ClientSocketHandle* handle, CompletionCallback* callback, int rv) {
1031 CHECK(!ContainsKey(pending_callback_map_, handle));
1032 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
1033 MessageLoop::current()->PostTask(
1034 FROM_HERE,
[email protected]2431756e2010-09-29 20:26:131035 method_factory_.NewRunnableMethod(
[email protected]05ea9ff2010-07-15 19:08:211036 &ClientSocketPoolBaseHelper::InvokeUserCallback,
1037 handle));
1038}
1039
1040void ClientSocketPoolBaseHelper::InvokeUserCallback(
1041 ClientSocketHandle* handle) {
1042 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
1043
1044 // Exit if the request has already been cancelled.
1045 if (it == pending_callback_map_.end())
1046 return;
1047
1048 CHECK(!handle->is_initialized());
1049 CompletionCallback* callback = it->second.callback;
1050 int result = it->second.result;
1051 pending_callback_map_.erase(it);
1052 callback->Run(result);
1053}
1054
[email protected]aed99ef02010-08-26 14:04:321055ClientSocketPoolBaseHelper::Group::Group()
1056 : active_socket_count_(0),
1057 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {}
1058
[email protected]e4d9a9722011-05-12 00:16:001059ClientSocketPoolBaseHelper::Group::~Group() {
1060 CleanupBackupJob();
[email protected]aed99ef02010-08-26 14:04:321061}
1062
1063void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
1064 const std::string& group_name,
1065 ClientSocketPoolBaseHelper* pool) {
1066 // Only allow one timer pending to create a backup socket.
1067 if (!method_factory_.empty())
1068 return;
1069
1070 MessageLoop::current()->PostDelayedTask(
1071 FROM_HERE,
1072 method_factory_.NewRunnableMethod(
1073 &Group::OnBackupSocketTimerFired, group_name, pool),
1074 pool->ConnectRetryIntervalMs());
1075}
1076
[email protected]2c2bef152010-10-13 00:55:031077bool ClientSocketPoolBaseHelper::Group::TryToUsePreconnectConnectJob() {
1078 for (std::set<ConnectJob*>::iterator it = jobs_.begin();
1079 it != jobs_.end(); ++it) {
1080 ConnectJob* job = *it;
1081 if (job->is_unused_preconnect()) {
1082 job->UseForNormalRequest();
1083 return true;
1084 }
1085 }
1086 return false;
1087}
1088
[email protected]aed99ef02010-08-26 14:04:321089void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
1090 std::string group_name,
1091 ClientSocketPoolBaseHelper* pool) {
1092 // If there are no more jobs pending, there is no work to do.
1093 // If we've done our cleanups correctly, this should not happen.
1094 if (jobs_.empty()) {
1095 NOTREACHED();
1096 return;
1097 }
1098
1099 // If our backup job is waiting on DNS, or if we can't create any sockets
1100 // right now due to limits, just reset the timer.
1101 if (pool->ReachedMaxSocketsLimit() ||
1102 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
1103 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
1104 StartBackupSocketTimer(group_name, pool);
1105 return;
1106 }
1107
[email protected]a9fc8fc2011-05-10 02:41:071108 if (pending_requests_.empty())
[email protected]4baaf9d2010-08-31 15:15:441109 return;
[email protected]4baaf9d2010-08-31 15:15:441110
[email protected]aed99ef02010-08-26 14:04:321111 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
1112 group_name, **pending_requests_.begin(), pool);
1113 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED, NULL);
1114 SIMPLE_STATS_COUNTER("socket.backup_created");
1115 int rv = backup_job->Connect();
1116 pool->connecting_socket_count_++;
1117 AddJob(backup_job);
1118 if (rv != ERR_IO_PENDING)
1119 pool->OnConnectJobComplete(rv, backup_job);
1120}
1121
1122void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
1123 // Delete active jobs.
1124 STLDeleteElements(&jobs_);
1125
1126 // Cancel pending backup job.
1127 method_factory_.RevokeAll();
1128}
1129
[email protected]d80a4322009-08-14 07:07:491130} // namespace internal
1131
[email protected]ff579d42009-06-24 15:47:021132} // namespace net