blob: 0f13c0ff5a22ff00b994197ae82e974f4d7d6a8b [file] [log] [blame]
[email protected]a796bcec2010-03-22 17:17:261// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]ff579d42009-06-24 15:47:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "net/socket/client_socket_pool_base.h"
6
7#include "base/compiler_specific.h"
[email protected]fd4fe0b2010-02-08 23:02:158#include "base/format_macros.h"
[email protected]ff579d42009-06-24 15:47:029#include "base/message_loop.h"
[email protected]6b624c62010-03-14 08:37:3210#include "base/stats_counters.h"
[email protected]ff579d42009-06-24 15:47:0211#include "base/stl_util-inl.h"
[email protected]fd4fe0b2010-02-08 23:02:1512#include "base/string_util.h"
[email protected]ff579d42009-06-24 15:47:0213#include "base/time.h"
[email protected]9349cfb2010-08-31 18:00:5314#include "base/values.h"
[email protected]9e743cd2010-03-16 07:03:5315#include "net/base/net_log.h"
[email protected]ff579d42009-06-24 15:47:0216#include "net/base/net_errors.h"
17#include "net/socket/client_socket_handle.h"
18
19using base::TimeDelta;
20
21namespace {
22
23// The timeout value, in seconds, used to clean up idle sockets that can't be
24// reused.
25//
26// Note: It's important to close idle sockets that have received data as soon
27// as possible because the received data may cause BSOD on Windows XP under
28// some conditions. See http://crbug.com/4606.
29const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
30
[email protected]06d94042010-08-25 01:45:2231// Indicate whether or not we should establish a new TCP connection after a
32// certain timeout has passed without receiving an ACK.
33bool g_connect_backup_jobs_enabled = true;
34
[email protected]ff579d42009-06-24 15:47:0235} // namespace
36
37namespace net {
38
[email protected]2ab05b52009-07-01 23:57:5839ConnectJob::ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3440 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3041 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5342 const BoundNetLog& net_log)
[email protected]2ab05b52009-07-01 23:57:5843 : group_name_(group_name),
[email protected]974ebd62009-08-03 23:14:3444 timeout_duration_(timeout_duration),
[email protected]2ab05b52009-07-01 23:57:5845 delegate_(delegate),
[email protected]a2006ece2010-04-23 16:44:0246 net_log_(net_log),
47 idle_(true) {
[email protected]2ab05b52009-07-01 23:57:5848 DCHECK(!group_name.empty());
[email protected]2ab05b52009-07-01 23:57:5849 DCHECK(delegate);
[email protected]06650c52010-06-03 00:49:1750 net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL);
[email protected]2ab05b52009-07-01 23:57:5851}
52
[email protected]fd7b7c92009-08-20 19:38:3053ConnectJob::~ConnectJob() {
[email protected]06650c52010-06-03 00:49:1754 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL);
[email protected]fd7b7c92009-08-20 19:38:3055}
[email protected]2ab05b52009-07-01 23:57:5856
[email protected]974ebd62009-08-03 23:14:3457int ConnectJob::Connect() {
58 if (timeout_duration_ != base::TimeDelta())
59 timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout);
[email protected]fd7b7c92009-08-20 19:38:3060
[email protected]a2006ece2010-04-23 16:44:0261 idle_ = false;
[email protected]fd7b7c92009-08-20 19:38:3062
[email protected]06650c52010-06-03 00:49:1763 LogConnectStart();
64
[email protected]fd7b7c92009-08-20 19:38:3065 int rv = ConnectInternal();
66
67 if (rv != ERR_IO_PENDING) {
[email protected]06650c52010-06-03 00:49:1768 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:3069 delegate_ = NULL;
[email protected]fd7b7c92009-08-20 19:38:3070 }
71
72 return rv;
73}
74
[email protected]06650c52010-06-03 00:49:1775void ConnectJob::set_socket(ClientSocket* socket) {
76 if (socket) {
77 net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET,
78 new NetLogSourceParameter("source_dependency",
79 socket->NetLog().source()));
80 }
81 socket_.reset(socket);
82}
83
[email protected]fd7b7c92009-08-20 19:38:3084void ConnectJob::NotifyDelegateOfCompletion(int rv) {
85 // The delegate will delete |this|.
86 Delegate *delegate = delegate_;
87 delegate_ = NULL;
88
[email protected]06650c52010-06-03 00:49:1789 LogConnectCompletion(rv);
[email protected]fd7b7c92009-08-20 19:38:3090 delegate->OnConnectJobComplete(rv, this);
[email protected]974ebd62009-08-03 23:14:3491}
92
[email protected]a796bcec2010-03-22 17:17:2693void ConnectJob::ResetTimer(base::TimeDelta remaining_time) {
94 timer_.Stop();
95 timer_.Start(remaining_time, this, &ConnectJob::OnTimeout);
96}
97
[email protected]06650c52010-06-03 00:49:1798void ConnectJob::LogConnectStart() {
99 net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT,
100 new NetLogStringParameter("group_name", group_name_));
101}
102
103void ConnectJob::LogConnectCompletion(int net_error) {
104 scoped_refptr<NetLog::EventParameters> params;
105 if (net_error != OK)
106 params = new NetLogIntegerParameter("net_error", net_error);
107 net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, params);
108}
109
[email protected]974ebd62009-08-03 23:14:34110void ConnectJob::OnTimeout() {
[email protected]6e713f02009-08-06 02:56:40111 // Make sure the socket is NULL before calling into |delegate|.
112 set_socket(NULL);
[email protected]fd7b7c92009-08-20 19:38:30113
[email protected]ec11be62010-04-28 19:28:09114 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT, NULL);
[email protected]fd7b7c92009-08-20 19:38:30115
116 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
[email protected]974ebd62009-08-03 23:14:34117}
118
[email protected]d80a4322009-08-14 07:07:49119namespace internal {
120
[email protected]fd4fe0b2010-02-08 23:02:15121ClientSocketPoolBaseHelper::Request::Request(
122 ClientSocketHandle* handle,
123 CompletionCallback* callback,
124 RequestPriority priority,
[email protected]9e743cd2010-03-16 07:03:53125 const BoundNetLog& net_log)
[email protected]fd4fe0b2010-02-08 23:02:15126 : handle_(handle), callback_(callback), priority_(priority),
[email protected]9e743cd2010-03-16 07:03:53127 net_log_(net_log) {}
[email protected]fd4fe0b2010-02-08 23:02:15128
129ClientSocketPoolBaseHelper::Request::~Request() {}
130
[email protected]d80a4322009-08-14 07:07:49131ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
[email protected]211d2172009-07-22 15:48:53132 int max_sockets,
[email protected]ff579d42009-06-24 15:47:02133 int max_sockets_per_group,
[email protected]9bf28db2009-08-29 01:35:16134 base::TimeDelta unused_idle_socket_timeout,
135 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38136 ConnectJobFactory* connect_job_factory)
[email protected]ff579d42009-06-24 15:47:02137 : idle_socket_count_(0),
[email protected]211d2172009-07-22 15:48:53138 connecting_socket_count_(0),
139 handed_out_socket_count_(0),
140 max_sockets_(max_sockets),
[email protected]ff579d42009-06-24 15:47:02141 max_sockets_per_group_(max_sockets_per_group),
[email protected]9bf28db2009-08-29 01:35:16142 unused_idle_socket_timeout_(unused_idle_socket_timeout),
143 used_idle_socket_timeout_(used_idle_socket_timeout),
[email protected]100d5fb92009-12-21 21:08:35144 connect_job_factory_(connect_job_factory),
[email protected]06d94042010-08-25 01:45:22145 connect_backup_jobs_enabled_(false),
[email protected]09d6ecb02010-07-22 20:10:45146 pool_generation_number_(0),
147 in_destructor_(false) {
[email protected]211d2172009-07-22 15:48:53148 DCHECK_LE(0, max_sockets_per_group);
149 DCHECK_LE(max_sockets_per_group, max_sockets);
[email protected]a554a8262010-05-20 00:13:52150
[email protected]66761b952010-06-25 21:30:38151 NetworkChangeNotifier::AddObserver(this);
[email protected]211d2172009-07-22 15:48:53152}
[email protected]ff579d42009-06-24 15:47:02153
[email protected]d80a4322009-08-14 07:07:49154ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
[email protected]09d6ecb02010-07-22 20:10:45155 in_destructor_ = true;
[email protected]4d3b05d2010-01-27 21:27:29156 CancelAllConnectJobs();
157
[email protected]ff579d42009-06-24 15:47:02158 // Clean up any idle sockets. Assert that we have no remaining active
159 // sockets or pending requests. They should have all been cleaned up prior
160 // to the manager being destroyed.
161 CloseIdleSockets();
[email protected]6b624c62010-03-14 08:37:32162 CHECK(group_map_.empty());
[email protected]05ea9ff2010-07-15 19:08:21163 DCHECK(pending_callback_map_.empty());
[email protected]4d3b05d2010-01-27 21:27:29164 DCHECK_EQ(0, connecting_socket_count_);
[email protected]a554a8262010-05-20 00:13:52165
[email protected]66761b952010-06-25 21:30:38166 NetworkChangeNotifier::RemoveObserver(this);
[email protected]ff579d42009-06-24 15:47:02167}
168
169// InsertRequestIntoQueue inserts the request into the queue based on
170// priority. Highest priorities are closest to the front. Older requests are
171// prioritized over requests of equal priority.
172//
173// static
[email protected]d80a4322009-08-14 07:07:49174void ClientSocketPoolBaseHelper::InsertRequestIntoQueue(
175 const Request* r, RequestQueue* pending_requests) {
[email protected]ff579d42009-06-24 15:47:02176 RequestQueue::iterator it = pending_requests->begin();
[email protected]ac790b42009-12-02 04:31:31177 while (it != pending_requests->end() && r->priority() >= (*it)->priority())
[email protected]ff579d42009-06-24 15:47:02178 ++it;
179 pending_requests->insert(it, r);
180}
181
[email protected]fd7b7c92009-08-20 19:38:30182// static
183const ClientSocketPoolBaseHelper::Request*
184ClientSocketPoolBaseHelper::RemoveRequestFromQueue(
185 RequestQueue::iterator it, RequestQueue* pending_requests) {
186 const Request* req = *it;
[email protected]fd7b7c92009-08-20 19:38:30187 pending_requests->erase(it);
188 return req;
189}
190
[email protected]d80a4322009-08-14 07:07:49191int ClientSocketPoolBaseHelper::RequestSocket(
[email protected]ff579d42009-06-24 15:47:02192 const std::string& group_name,
[email protected]d80a4322009-08-14 07:07:49193 const Request* request) {
[email protected]ec11be62010-04-28 19:28:09194 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]aed99ef02010-08-26 14:04:32195 Group* group = GetOrCreateGroup(group_name);
[email protected]eb5a99382010-07-11 03:18:26196
[email protected]fd4fe0b2010-02-08 23:02:15197 int rv = RequestSocketInternal(group_name, request);
[email protected]e7e99322010-05-04 23:30:17198 if (rv != ERR_IO_PENDING) {
[email protected]ec11be62010-04-28 19:28:09199 request->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]05ea9ff2010-07-15 19:08:21200 CHECK(!request->handle()->is_initialized());
[email protected]e7e99322010-05-04 23:30:17201 delete request;
202 } else {
[email protected]aed99ef02010-08-26 14:04:32203 InsertRequestIntoQueue(request, group->mutable_pending_requests());
[email protected]e7e99322010-05-04 23:30:17204 }
[email protected]fd4fe0b2010-02-08 23:02:15205 return rv;
206}
207
208int ClientSocketPoolBaseHelper::RequestSocketInternal(
209 const std::string& group_name,
210 const Request* request) {
[email protected]d80a4322009-08-14 07:07:49211 DCHECK_GE(request->priority(), 0);
212 CompletionCallback* const callback = request->callback();
213 CHECK(callback);
214 ClientSocketHandle* const handle = request->handle();
215 CHECK(handle);
[email protected]aed99ef02010-08-26 14:04:32216 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02217
[email protected]65552102010-04-09 22:58:10218 // Try to reuse a socket.
[email protected]aed99ef02010-08-26 14:04:32219 if (AssignIdleSocketToGroup(request, group))
[email protected]eb5a99382010-07-11 03:18:26220 return OK;
[email protected]65552102010-04-09 22:58:10221
[email protected]43a21b82010-06-10 21:30:54222 // Can we make another active socket now?
[email protected]aed99ef02010-08-26 14:04:32223 if (!group->HasAvailableSocketSlot(max_sockets_per_group_)) {
[email protected]43a21b82010-06-10 21:30:54224 request->net_log().AddEvent(
225 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL);
226 return ERR_IO_PENDING;
227 }
228
229 if (ReachedMaxSocketsLimit()) {
230 if (idle_socket_count() > 0) {
231 CloseOneIdleSocket();
232 } else {
233 // We could check if we really have a stalled group here, but it requires
234 // a scan of all groups, so just flip a flag here, and do the check later.
[email protected]43a21b82010-06-10 21:30:54235 request->net_log().AddEvent(
236 NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL);
237 return ERR_IO_PENDING;
238 }
239 }
240
[email protected]ff579d42009-06-24 15:47:02241 // We couldn't find a socket to reuse, so allocate and connect a new one.
[email protected]2ab05b52009-07-01 23:57:58242 scoped_ptr<ConnectJob> connect_job(
[email protected]06650c52010-06-03 00:49:17243 connect_job_factory_->NewConnectJob(group_name, *request, this));
[email protected]ff579d42009-06-24 15:47:02244
[email protected]2ab05b52009-07-01 23:57:58245 int rv = connect_job->Connect();
246 if (rv == OK) {
[email protected]06650c52010-06-03 00:49:17247 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]2ab05b52009-07-01 23:57:58248 HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */,
[email protected]aed99ef02010-08-26 14:04:32249 handle, base::TimeDelta(), group, request->net_log());
[email protected]2ab05b52009-07-01 23:57:58250 } else if (rv == ERR_IO_PENDING) {
[email protected]6b624c62010-03-14 08:37:32251 // If we don't have any sockets in this group, set a timer for potentially
252 // creating a new one. If the SYN is lost, this backup socket may complete
253 // before the slow socket, improving end user latency.
[email protected]aed99ef02010-08-26 14:04:32254 if (group->IsEmpty() && !group->HasBackupJob() &&
255 connect_backup_jobs_enabled_)
256 group->StartBackupSocketTimer(group_name, this);
[email protected]6b624c62010-03-14 08:37:32257
[email protected]211d2172009-07-22 15:48:53258 connecting_socket_count_++;
259
[email protected]aed99ef02010-08-26 14:04:32260 group->AddJob(connect_job.release());
[email protected]a2006ece2010-04-23 16:44:02261 } else {
[email protected]06650c52010-06-03 00:49:17262 LogBoundConnectJobToRequest(connect_job->net_log().source(), request);
[email protected]e60e47a2010-07-14 03:37:18263 connect_job->GetAdditionalErrorState(handle);
[email protected]e772db3f2010-07-12 18:11:13264 ClientSocket* error_socket = connect_job->ReleaseSocket();
265 if (error_socket) {
266 HandOutSocket(error_socket, false /* not reused */, handle,
[email protected]aed99ef02010-08-26 14:04:32267 base::TimeDelta(), group, request->net_log());
268 } else if (group->IsEmpty()) {
269 RemoveGroup(group_name);
[email protected]05ea9ff2010-07-15 19:08:21270 }
[email protected]2ab05b52009-07-01 23:57:58271 }
[email protected]ff579d42009-06-24 15:47:02272
[email protected]2ab05b52009-07-01 23:57:58273 return rv;
[email protected]ff579d42009-06-24 15:47:02274}
275
[email protected]05ea9ff2010-07-15 19:08:21276bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup(
[email protected]aed99ef02010-08-26 14:04:32277 const Request* request, Group* group) {
[email protected]eb5a99382010-07-11 03:18:26278 // Iterate through the list of idle sockets until we find one or exhaust
279 // the list.
[email protected]aed99ef02010-08-26 14:04:32280 while (!group->idle_sockets().empty()) {
281 IdleSocket idle_socket = group->idle_sockets().back();
282 group->mutable_idle_sockets()->pop_back();
[email protected]eb5a99382010-07-11 03:18:26283 DecrementIdleCount();
284 if (idle_socket.socket->IsConnectedAndIdle()) {
285 // We found one we can reuse!
286 base::TimeDelta idle_time =
287 base::TimeTicks::Now() - idle_socket.start_time;
288 HandOutSocket(
289 idle_socket.socket, idle_socket.used, request->handle(), idle_time,
290 group, request->net_log());
291 return true;
292 }
293 delete idle_socket.socket;
294 }
295 return false;
296}
297
[email protected]06650c52010-06-03 00:49:17298// static
299void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest(
300 const NetLog::Source& connect_job_source, const Request* request) {
301 request->net_log().AddEvent(
302 NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB,
303 new NetLogSourceParameter("source_dependency", connect_job_source));
304}
305
[email protected]d80a4322009-08-14 07:07:49306void ClientSocketPoolBaseHelper::CancelRequest(
[email protected]05ea9ff2010-07-15 19:08:21307 const std::string& group_name, ClientSocketHandle* handle) {
308 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle);
309 if (callback_it != pending_callback_map_.end()) {
310 int result = callback_it->second.result;
311 pending_callback_map_.erase(callback_it);
312 ClientSocket* socket = handle->release_socket();
313 if (socket) {
314 if (result != OK)
315 socket->Disconnect();
316 ReleaseSocket(handle->group_name(), socket, handle->id());
317 }
318 return;
319 }
[email protected]b6501d3d2010-06-03 23:53:34320
[email protected]ff579d42009-06-24 15:47:02321 CHECK(ContainsKey(group_map_, group_name));
322
[email protected]aed99ef02010-08-26 14:04:32323 Group* group = GetOrCreateGroup(group_name);
[email protected]ff579d42009-06-24 15:47:02324
[email protected]ff579d42009-06-24 15:47:02325 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32326 RequestQueue::iterator it = group->mutable_pending_requests()->begin();
327 for (; it != group->pending_requests().end(); ++it) {
[email protected]d80a4322009-08-14 07:07:49328 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32329 const Request* req =
330 RemoveRequestFromQueue(it, group->mutable_pending_requests());
[email protected]ec11be62010-04-28 19:28:09331 req->net_log().AddEvent(NetLog::TYPE_CANCELLED, NULL);
332 req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]fd7b7c92009-08-20 19:38:30333 delete req;
[email protected]eb5a99382010-07-11 03:18:26334
335 // We let the job run, unless we're at the socket limit.
[email protected]aed99ef02010-08-26 14:04:32336 if (group->jobs().size() && ReachedMaxSocketsLimit()) {
337 RemoveConnectJob(*group->jobs().begin(), group);
[email protected]eb5a99382010-07-11 03:18:26338 CheckForStalledSocketGroups();
[email protected]974ebd62009-08-03 23:14:34339 }
[email protected]4baaf9d2010-08-31 15:15:44340
341 // If there are no more requests, we kill the backup timer.
342 if (group->pending_requests().empty())
343 group->CleanupBackupJob();
[email protected]eb5a99382010-07-11 03:18:26344 break;
[email protected]ff579d42009-06-24 15:47:02345 }
346 }
[email protected]ff579d42009-06-24 15:47:02347}
348
[email protected]2abfe90a2010-08-25 17:49:51349bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const {
350 return ContainsKey(group_map_, group_name);
351}
352
[email protected]d80a4322009-08-14 07:07:49353void ClientSocketPoolBaseHelper::CloseIdleSockets() {
[email protected]ff579d42009-06-24 15:47:02354 CleanupIdleSockets(true);
[email protected]06f92462010-08-31 19:24:14355 DCHECK_EQ(0, idle_socket_count_);
[email protected]ff579d42009-06-24 15:47:02356}
357
[email protected]d80a4322009-08-14 07:07:49358int ClientSocketPoolBaseHelper::IdleSocketCountInGroup(
[email protected]ff579d42009-06-24 15:47:02359 const std::string& group_name) const {
360 GroupMap::const_iterator i = group_map_.find(group_name);
361 CHECK(i != group_map_.end());
362
[email protected]aed99ef02010-08-26 14:04:32363 return i->second->idle_sockets().size();
[email protected]ff579d42009-06-24 15:47:02364}
365
[email protected]d80a4322009-08-14 07:07:49366LoadState ClientSocketPoolBaseHelper::GetLoadState(
[email protected]ff579d42009-06-24 15:47:02367 const std::string& group_name,
368 const ClientSocketHandle* handle) const {
[email protected]05ea9ff2010-07-15 19:08:21369 if (ContainsKey(pending_callback_map_, handle))
370 return LOAD_STATE_CONNECTING;
371
[email protected]ff579d42009-06-24 15:47:02372 if (!ContainsKey(group_map_, group_name)) {
373 NOTREACHED() << "ClientSocketPool does not contain group: " << group_name
374 << " for handle: " << handle;
375 return LOAD_STATE_IDLE;
376 }
377
378 // Can't use operator[] since it is non-const.
[email protected]aed99ef02010-08-26 14:04:32379 const Group& group = *group_map_.find(group_name)->second;
[email protected]ff579d42009-06-24 15:47:02380
[email protected]ff579d42009-06-24 15:47:02381 // Search pending_requests for matching handle.
[email protected]aed99ef02010-08-26 14:04:32382 RequestQueue::const_iterator it = group.pending_requests().begin();
383 for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) {
[email protected]d80a4322009-08-14 07:07:49384 if ((*it)->handle() == handle) {
[email protected]aed99ef02010-08-26 14:04:32385 if (i < group.jobs().size()) {
[email protected]5fc08e32009-07-15 17:09:57386 LoadState max_state = LOAD_STATE_IDLE;
[email protected]aed99ef02010-08-26 14:04:32387 for (ConnectJobSet::const_iterator job_it = group.jobs().begin();
388 job_it != group.jobs().end(); ++job_it) {
[email protected]46451352009-09-01 14:54:21389 max_state = std::max(max_state, (*job_it)->GetLoadState());
[email protected]5fc08e32009-07-15 17:09:57390 }
391 return max_state;
392 } else {
393 // TODO(wtc): Add a state for being on the wait list.
394 // See http://www.crbug.com/5077.
395 return LOAD_STATE_IDLE;
396 }
[email protected]ff579d42009-06-24 15:47:02397 }
398 }
399
400 NOTREACHED();
401 return LOAD_STATE_IDLE;
402}
403
[email protected]59d7a5a2010-08-30 16:44:27404Value* ClientSocketPoolBaseHelper::GetInfoAsValue(
405 const std::string& name, const std::string& type) const {
406 DictionaryValue* dict = new DictionaryValue();
407 dict->SetString("name", name);
408 dict->SetString("type", type);
409 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_);
410 dict->SetInteger("connecting_socket_count", connecting_socket_count_);
411 dict->SetInteger("idle_socket_count", idle_socket_count_);
412 dict->SetInteger("max_socket_count", max_sockets_);
413 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_);
414 dict->SetInteger("pool_generation_number", pool_generation_number_);
415
416 if (group_map_.empty())
417 return dict;
418
419 DictionaryValue* all_groups_dict = new DictionaryValue();
420 for (GroupMap::const_iterator it = group_map_.begin();
421 it != group_map_.end(); it++) {
422 const Group* group = it->second;
423 DictionaryValue* group_dict = new DictionaryValue();
424
425 group_dict->SetInteger("pending_request_count",
426 group->pending_requests().size());
427 if (!group->pending_requests().empty()) {
428 group_dict->SetInteger("top_pending_priority",
429 group->TopPendingPriority());
430 }
431
432 group_dict->SetInteger("active_socket_count", group->active_socket_count());
433 group_dict->SetInteger("idle_socket_count", group->idle_sockets().size());
434 group_dict->SetInteger("connect_job_count", group->jobs().size());
435
436 group_dict->SetBoolean("is_stalled",
437 group->IsStalled(max_sockets_per_group_));
438 group_dict->SetBoolean("has_backup_job", group->HasBackupJob());
439
440 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict);
441 }
442 dict->Set("groups", all_groups_dict);
443 return dict;
444}
445
[email protected]d80a4322009-08-14 07:07:49446bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
[email protected]9bf28db2009-08-29 01:35:16447 base::TimeTicks now,
448 base::TimeDelta timeout) const {
449 bool timed_out = (now - start_time) >= timeout;
[email protected]5fc08e32009-07-15 17:09:57450 return timed_out ||
451 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
[email protected]ff579d42009-06-24 15:47:02452}
453
[email protected]d80a4322009-08-14 07:07:49454void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
[email protected]ff579d42009-06-24 15:47:02455 if (idle_socket_count_ == 0)
456 return;
457
[email protected]09d6ecb02010-07-22 20:10:45458 // Deleting an SSL socket may remove the last reference to an
459 // HttpNetworkSession (in an incognito session), triggering the destruction
460 // of pools, potentially causing a recursive call to this function. Hold a
461 // reference to |this| to prevent that.
462 scoped_refptr<ClientSocketPoolBaseHelper> protect_this;
463 if (!in_destructor_)
464 protect_this = this;
465
[email protected]ff579d42009-06-24 15:47:02466 // Current time value. Retrieving it once at the function start rather than
467 // inside the inner loop, since it shouldn't change by any meaningful amount.
468 base::TimeTicks now = base::TimeTicks::Now();
469
470 GroupMap::iterator i = group_map_.begin();
471 while (i != group_map_.end()) {
[email protected]aed99ef02010-08-26 14:04:32472 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02473
[email protected]aed99ef02010-08-26 14:04:32474 std::deque<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin();
475 while (j != group->idle_sockets().end()) {
[email protected]9bf28db2009-08-29 01:35:16476 base::TimeDelta timeout =
477 j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_;
478 if (force || j->ShouldCleanup(now, timeout)) {
[email protected]ff579d42009-06-24 15:47:02479 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32480 j = group->mutable_idle_sockets()->erase(j);
[email protected]ff579d42009-06-24 15:47:02481 DecrementIdleCount();
482 } else {
483 ++j;
484 }
485 }
486
487 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32488 if (group->IsEmpty()) {
489 RemoveGroup(i++);
[email protected]ff579d42009-06-24 15:47:02490 } else {
491 ++i;
492 }
493 }
494}
495
[email protected]aed99ef02010-08-26 14:04:32496ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup(
497 const std::string& group_name) {
498 GroupMap::iterator it = group_map_.find(group_name);
499 if (it != group_map_.end())
500 return it->second;
501 Group* group = new Group;
502 group_map_[group_name] = group;
503 return group;
504}
505
506void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) {
507 GroupMap::iterator it = group_map_.find(group_name);
508 CHECK(it != group_map_.end());
509
510 RemoveGroup(it);
511}
512
513void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) {
514 delete it->second;
515 group_map_.erase(it);
516}
517
[email protected]06d94042010-08-25 01:45:22518// static
519void ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) {
520 g_connect_backup_jobs_enabled = enabled;
521}
522
523void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
524 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
525}
526
[email protected]d80a4322009-08-14 07:07:49527void ClientSocketPoolBaseHelper::IncrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02528 if (++idle_socket_count_ == 1)
529 timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this,
[email protected]d80a4322009-08-14 07:07:49530 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
[email protected]ff579d42009-06-24 15:47:02531}
532
[email protected]d80a4322009-08-14 07:07:49533void ClientSocketPoolBaseHelper::DecrementIdleCount() {
[email protected]ff579d42009-06-24 15:47:02534 if (--idle_socket_count_ == 0)
535 timer_.Stop();
536}
537
[email protected]eb5a99382010-07-11 03:18:26538void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
539 ClientSocket* socket,
540 int id) {
[email protected]ff579d42009-06-24 15:47:02541 GroupMap::iterator i = group_map_.find(group_name);
542 CHECK(i != group_map_.end());
543
[email protected]aed99ef02010-08-26 14:04:32544 Group* group = i->second;
[email protected]ff579d42009-06-24 15:47:02545
[email protected]b1f031dd2010-03-02 23:19:33546 CHECK_GT(handed_out_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53547 handed_out_socket_count_--;
548
[email protected]aed99ef02010-08-26 14:04:32549 CHECK_GT(group->active_socket_count(), 0);
550 group->DecrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02551
[email protected]a7e38572010-06-07 18:22:24552 const bool can_reuse = socket->IsConnectedAndIdle() &&
553 id == pool_generation_number_;
[email protected]ff579d42009-06-24 15:47:02554 if (can_reuse) {
[email protected]eb5a99382010-07-11 03:18:26555 // Add it to the idle list.
[email protected]aed99ef02010-08-26 14:04:32556 AddIdleSocket(socket, true /* used socket */, group);
557 OnAvailableSocketSlot(group_name, group);
[email protected]ff579d42009-06-24 15:47:02558 } else {
559 delete socket;
560 }
[email protected]05ea9ff2010-07-15 19:08:21561
[email protected]eb5a99382010-07-11 03:18:26562 CheckForStalledSocketGroups();
563}
[email protected]ff579d42009-06-24 15:47:02564
[email protected]eb5a99382010-07-11 03:18:26565void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() {
566 // If we have idle sockets, see if we can give one to the top-stalled group.
567 std::string top_group_name;
568 Group* top_group = NULL;
[email protected]05ea9ff2010-07-15 19:08:21569 if (!FindTopStalledGroup(&top_group, &top_group_name))
[email protected]eb5a99382010-07-11 03:18:26570 return;
[email protected]4f2abec2010-02-03 18:10:16571
[email protected]eb5a99382010-07-11 03:18:26572 if (ReachedMaxSocketsLimit()) {
573 if (idle_socket_count() > 0) {
574 CloseOneIdleSocket();
[email protected]d7027bb2010-05-10 18:58:54575 } else {
[email protected]eb5a99382010-07-11 03:18:26576 // We can't activate more sockets since we're already at our global
577 // limit.
[email protected]4f2abec2010-02-03 18:10:16578 return;
[email protected]d7027bb2010-05-10 18:58:54579 }
[email protected]4f2abec2010-02-03 18:10:16580 }
[email protected]eb5a99382010-07-11 03:18:26581
582 // Note: we don't loop on waking stalled groups. If the stalled group is at
583 // its limit, may be left with other stalled groups that could be
[email protected]05ea9ff2010-07-15 19:08:21584 // woken. This isn't optimal, but there is no starvation, so to avoid
[email protected]eb5a99382010-07-11 03:18:26585 // the looping we leave it at this.
[email protected]05ea9ff2010-07-15 19:08:21586 OnAvailableSocketSlot(top_group_name, top_group);
[email protected]ff579d42009-06-24 15:47:02587}
588
[email protected]211d2172009-07-22 15:48:53589// Search for the highest priority pending request, amongst the groups that
590// are not at the |max_sockets_per_group_| limit. Note: for requests with
591// the same priority, the winner is based on group hash ordering (and not
592// insertion order).
[email protected]05ea9ff2010-07-15 19:08:21593bool ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group,
594 std::string* group_name) {
[email protected]211d2172009-07-22 15:48:53595 Group* top_group = NULL;
596 const std::string* top_group_name = NULL;
[email protected]05ea9ff2010-07-15 19:08:21597 bool has_stalled_group = false;
[email protected]211d2172009-07-22 15:48:53598 for (GroupMap::iterator i = group_map_.begin();
599 i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32600 Group* curr_group = i->second;
601 const RequestQueue& queue = curr_group->pending_requests();
[email protected]211d2172009-07-22 15:48:53602 if (queue.empty())
603 continue;
[email protected]aed99ef02010-08-26 14:04:32604 if (curr_group->IsStalled(max_sockets_per_group_)) {
[email protected]05ea9ff2010-07-15 19:08:21605 has_stalled_group = true;
[email protected]6427fe22010-04-16 22:27:41606 bool has_higher_priority = !top_group ||
[email protected]aed99ef02010-08-26 14:04:32607 curr_group->TopPendingPriority() < top_group->TopPendingPriority();
[email protected]6427fe22010-04-16 22:27:41608 if (has_higher_priority) {
[email protected]aed99ef02010-08-26 14:04:32609 top_group = curr_group;
[email protected]6427fe22010-04-16 22:27:41610 top_group_name = &i->first;
611 }
[email protected]211d2172009-07-22 15:48:53612 }
613 }
[email protected]05ea9ff2010-07-15 19:08:21614
[email protected]211d2172009-07-22 15:48:53615 if (top_group) {
616 *group = top_group;
617 *group_name = *top_group_name;
618 }
[email protected]05ea9ff2010-07-15 19:08:21619 return has_stalled_group;
[email protected]211d2172009-07-22 15:48:53620}
621
[email protected]d80a4322009-08-14 07:07:49622void ClientSocketPoolBaseHelper::OnConnectJobComplete(
623 int result, ConnectJob* job) {
[email protected]2ab05b52009-07-01 23:57:58624 DCHECK_NE(ERR_IO_PENDING, result);
625 const std::string group_name = job->group_name();
[email protected]ff579d42009-06-24 15:47:02626 GroupMap::iterator group_it = group_map_.find(group_name);
627 CHECK(group_it != group_map_.end());
[email protected]aed99ef02010-08-26 14:04:32628 Group* group = group_it->second;
[email protected]ff579d42009-06-24 15:47:02629
[email protected]5fc08e32009-07-15 17:09:57630 scoped_ptr<ClientSocket> socket(job->ReleaseSocket());
[email protected]ff579d42009-06-24 15:47:02631
[email protected]9e743cd2010-03-16 07:03:53632 BoundNetLog job_log = job->net_log();
[email protected]5fc08e32009-07-15 17:09:57633
[email protected]1350e832010-08-26 16:22:39634 // ConnectJobs may hold references to pools which may hold references back to
635 // this pool, so RemoveConnectJob() may eventually lead to something calling
636 // Release() on |this| which deletes it in the middle of this function. Hold
637 // a self-reference to prevent deletion of |this|.
638 const scoped_refptr<ClientSocketPoolBaseHelper> self(this);
639
[email protected]4d3b05d2010-01-27 21:27:29640 if (result == OK) {
641 DCHECK(socket.get());
[email protected]aed99ef02010-08-26 14:04:32642 RemoveConnectJob(job, group);
643 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29644 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]aed99ef02010-08-26 14:04:32645 group->mutable_pending_requests()->begin(),
646 group->mutable_pending_requests()));
[email protected]06650c52010-06-03 00:49:17647 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]4d3b05d2010-01-27 21:27:29648 HandOutSocket(
649 socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32650 base::TimeDelta(), group, r->net_log());
[email protected]06650c52010-06-03 00:49:17651 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL);
[email protected]05ea9ff2010-07-15 19:08:21652 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]5fc08e32009-07-15 17:09:57653 } else {
[email protected]aed99ef02010-08-26 14:04:32654 AddIdleSocket(socket.release(), false /* unused socket */, group);
655 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21656 CheckForStalledSocketGroups();
[email protected]5fc08e32009-07-15 17:09:57657 }
[email protected]94c20472010-01-14 08:14:36658 } else {
[email protected]e772db3f2010-07-12 18:11:13659 // If we got a socket, it must contain error information so pass that
660 // up so that the caller can retrieve it.
661 bool handed_out_socket = false;
[email protected]aed99ef02010-08-26 14:04:32662 if (!group->pending_requests().empty()) {
[email protected]4d3b05d2010-01-27 21:27:29663 scoped_ptr<const Request> r(RemoveRequestFromQueue(
[email protected]aed99ef02010-08-26 14:04:32664 group->mutable_pending_requests()->begin(),
665 group->mutable_pending_requests()));
[email protected]06650c52010-06-03 00:49:17666 LogBoundConnectJobToRequest(job_log.source(), r.get());
[email protected]e60e47a2010-07-14 03:37:18667 job->GetAdditionalErrorState(r->handle());
[email protected]aed99ef02010-08-26 14:04:32668 RemoveConnectJob(job, group);
[email protected]e772db3f2010-07-12 18:11:13669 if (socket.get()) {
670 handed_out_socket = true;
671 HandOutSocket(socket.release(), false /* unused socket */, r->handle(),
[email protected]aed99ef02010-08-26 14:04:32672 base::TimeDelta(), group, r->net_log());
[email protected]e772db3f2010-07-12 18:11:13673 }
[email protected]06650c52010-06-03 00:49:17674 r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL,
675 new NetLogIntegerParameter("net_error", result));
[email protected]05ea9ff2010-07-15 19:08:21676 InvokeUserCallbackLater(r->handle(), r->callback(), result);
[email protected]e60e47a2010-07-14 03:37:18677 } else {
[email protected]aed99ef02010-08-26 14:04:32678 RemoveConnectJob(job, group);
[email protected]4d3b05d2010-01-27 21:27:29679 }
[email protected]05ea9ff2010-07-15 19:08:21680 if (!handed_out_socket) {
[email protected]aed99ef02010-08-26 14:04:32681 OnAvailableSocketSlot(group_name, group);
[email protected]05ea9ff2010-07-15 19:08:21682 CheckForStalledSocketGroups();
683 }
[email protected]ff579d42009-06-24 15:47:02684 }
[email protected]ff579d42009-06-24 15:47:02685}
686
[email protected]66761b952010-06-25 21:30:38687void ClientSocketPoolBaseHelper::OnIPAddressChanged() {
688 Flush();
689}
690
[email protected]a7e38572010-06-07 18:22:24691void ClientSocketPoolBaseHelper::Flush() {
692 pool_generation_number_++;
[email protected]06f92462010-08-31 19:24:14693 CancelAllConnectJobs();
[email protected]a554a8262010-05-20 00:13:52694 CloseIdleSockets();
[email protected]06f92462010-08-31 19:24:14695 AbortAllRequests();
[email protected]a554a8262010-05-20 00:13:52696}
697
[email protected]25eea382010-07-10 23:55:26698void ClientSocketPoolBaseHelper::RemoveConnectJob(const ConnectJob* job,
[email protected]4d3b05d2010-01-27 21:27:29699 Group* group) {
[email protected]b1f031dd2010-03-02 23:19:33700 CHECK_GT(connecting_socket_count_, 0);
[email protected]211d2172009-07-22 15:48:53701 connecting_socket_count_--;
702
[email protected]25eea382010-07-10 23:55:26703 DCHECK(group);
[email protected]aed99ef02010-08-26 14:04:32704 DCHECK(ContainsKey(group->jobs(), job));
705 group->RemoveJob(job);
[email protected]25eea382010-07-10 23:55:26706
707 // If we've got no more jobs for this group, then we no longer need a
708 // backup job either.
[email protected]aed99ef02010-08-26 14:04:32709 if (group->jobs().empty())
[email protected]25eea382010-07-10 23:55:26710 group->CleanupBackupJob();
711
[email protected]8ae03f42010-07-07 19:08:10712 DCHECK(job);
713 delete job;
[email protected]2ab05b52009-07-01 23:57:58714}
[email protected]ff579d42009-06-24 15:47:02715
[email protected]8ae03f42010-07-07 19:08:10716void ClientSocketPoolBaseHelper::OnAvailableSocketSlot(
[email protected]05ea9ff2010-07-15 19:08:21717 const std::string& group_name, Group* group) {
[email protected]2abfe90a2010-08-25 17:49:51718 DCHECK(ContainsKey(group_map_, group_name));
[email protected]05ea9ff2010-07-15 19:08:21719 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32720 RemoveGroup(group_name);
721 else if (!group->pending_requests().empty())
[email protected]2abfe90a2010-08-25 17:49:51722 ProcessPendingRequest(group_name, group);
[email protected]8ae03f42010-07-07 19:08:10723}
[email protected]06650c52010-06-03 00:49:17724
[email protected]8ae03f42010-07-07 19:08:10725void ClientSocketPoolBaseHelper::ProcessPendingRequest(
[email protected]05ea9ff2010-07-15 19:08:21726 const std::string& group_name, Group* group) {
727 int rv = RequestSocketInternal(group_name,
[email protected]aed99ef02010-08-26 14:04:32728 *group->pending_requests().begin());
[email protected]05ea9ff2010-07-15 19:08:21729 if (rv != ERR_IO_PENDING) {
730 scoped_ptr<const Request> request(RemoveRequestFromQueue(
[email protected]aed99ef02010-08-26 14:04:32731 group->mutable_pending_requests()->begin(),
732 group->mutable_pending_requests()));
[email protected]2abfe90a2010-08-25 17:49:51733 if (group->IsEmpty())
[email protected]aed99ef02010-08-26 14:04:32734 RemoveGroup(group_name);
[email protected]8ae03f42010-07-07 19:08:10735
[email protected]05ea9ff2010-07-15 19:08:21736 scoped_refptr<NetLog::EventParameters> params;
737 if (rv != OK)
738 params = new NetLogIntegerParameter("net_error", rv);
739 request->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, params);
740 InvokeUserCallbackLater(
741 request->handle(), request->callback(), rv);
[email protected]2ab05b52009-07-01 23:57:58742 }
743}
744
[email protected]d80a4322009-08-14 07:07:49745void ClientSocketPoolBaseHelper::HandOutSocket(
[email protected]2ab05b52009-07-01 23:57:58746 ClientSocket* socket,
747 bool reused,
748 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29749 base::TimeDelta idle_time,
[email protected]fd4fe0b2010-02-08 23:02:15750 Group* group,
[email protected]9e743cd2010-03-16 07:03:53751 const BoundNetLog& net_log) {
[email protected]2ab05b52009-07-01 23:57:58752 DCHECK(socket);
753 handle->set_socket(socket);
754 handle->set_is_reused(reused);
[email protected]f9d285c2009-08-17 19:54:29755 handle->set_idle_time(idle_time);
[email protected]a7e38572010-06-07 18:22:24756 handle->set_pool_id(pool_generation_number_);
[email protected]211d2172009-07-22 15:48:53757
[email protected]d13f51b2010-04-27 23:20:45758 if (reused) {
[email protected]ec11be62010-04-28 19:28:09759 net_log.AddEvent(
[email protected]d13f51b2010-04-27 23:20:45760 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET,
[email protected]ec11be62010-04-28 19:28:09761 new NetLogIntegerParameter(
762 "idle_ms", static_cast<int>(idle_time.InMilliseconds())));
[email protected]fd4fe0b2010-02-08 23:02:15763 }
[email protected]d13f51b2010-04-27 23:20:45764
[email protected]06650c52010-06-03 00:49:17765 net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET,
766 new NetLogSourceParameter(
767 "source_dependency", socket->NetLog().source()));
[email protected]fd4fe0b2010-02-08 23:02:15768
[email protected]211d2172009-07-22 15:48:53769 handed_out_socket_count_++;
[email protected]aed99ef02010-08-26 14:04:32770 group->IncrementActiveSocketCount();
[email protected]ff579d42009-06-24 15:47:02771}
772
[email protected]d80a4322009-08-14 07:07:49773void ClientSocketPoolBaseHelper::AddIdleSocket(
[email protected]5fc08e32009-07-15 17:09:57774 ClientSocket* socket, bool used, Group* group) {
775 DCHECK(socket);
776 IdleSocket idle_socket;
777 idle_socket.socket = socket;
778 idle_socket.start_time = base::TimeTicks::Now();
779 idle_socket.used = used;
780
[email protected]aed99ef02010-08-26 14:04:32781 group->mutable_idle_sockets()->push_back(idle_socket);
[email protected]5fc08e32009-07-15 17:09:57782 IncrementIdleCount();
783}
784
[email protected]d80a4322009-08-14 07:07:49785void ClientSocketPoolBaseHelper::CancelAllConnectJobs() {
[email protected]74d75e0b2010-08-23 20:39:54786 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
[email protected]aed99ef02010-08-26 14:04:32787 Group* group = i->second;
788 connecting_socket_count_ -= group->jobs().size();
789 group->RemoveAllJobs();
[email protected]6b624c62010-03-14 08:37:32790
[email protected]5fc08e32009-07-15 17:09:57791 // Delete group if no longer needed.
[email protected]aed99ef02010-08-26 14:04:32792 if (group->IsEmpty()) {
[email protected]06f92462010-08-31 19:24:14793 // RemoveGroup() will call .erase() which will invalidate the iterator,
794 // but i will already have been incremented to a valid iterator before
795 // RemoveGroup() is called.
796 RemoveGroup(i++);
797 } else {
798 ++i;
799 }
800 }
801 DCHECK_EQ(0, connecting_socket_count_);
802}
803
804void ClientSocketPoolBaseHelper::AbortAllRequests() {
805 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) {
806 Group* group = i->second;
807
808 RequestQueue pending_requests;
809 pending_requests.swap(*group->mutable_pending_requests());
810 for (RequestQueue::iterator it2 = pending_requests.begin();
811 it2 != pending_requests.end(); ++it2) {
812 const Request* request = *it2;
813 InvokeUserCallbackLater(
814 request->handle(), request->callback(), ERR_ABORTED);
815 }
816
817 // Delete group if no longer needed.
818 if (group->IsEmpty()) {
819 // RemoveGroup() will call .erase() which will invalidate the iterator,
820 // but i will already have been incremented to a valid iterator before
821 // RemoveGroup() is called.
[email protected]aed99ef02010-08-26 14:04:32822 RemoveGroup(i++);
[email protected]74d75e0b2010-08-23 20:39:54823 } else {
824 ++i;
[email protected]5fc08e32009-07-15 17:09:57825 }
826 }
827}
828
[email protected]d80a4322009-08-14 07:07:49829bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const {
[email protected]211d2172009-07-22 15:48:53830 // Each connecting socket will eventually connect and be handed out.
[email protected]43a21b82010-06-10 21:30:54831 int total = handed_out_socket_count_ + connecting_socket_count_ +
832 idle_socket_count();
[email protected]211d2172009-07-22 15:48:53833 DCHECK_LE(total, max_sockets_);
[email protected]c901f6d2010-04-27 17:48:28834 if (total < max_sockets_)
835 return false;
836 LOG(WARNING) << "ReachedMaxSocketsLimit: " << total << "/" << max_sockets_;
837 return true;
[email protected]211d2172009-07-22 15:48:53838}
839
[email protected]43a21b82010-06-10 21:30:54840void ClientSocketPoolBaseHelper::CloseOneIdleSocket() {
841 CHECK_GT(idle_socket_count(), 0);
842
843 for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) {
[email protected]aed99ef02010-08-26 14:04:32844 Group* group = i->second;
[email protected]43a21b82010-06-10 21:30:54845
[email protected]aed99ef02010-08-26 14:04:32846 if (!group->idle_sockets().empty()) {
847 std::deque<IdleSocket>::iterator j =
848 group->mutable_idle_sockets()->begin();
[email protected]43a21b82010-06-10 21:30:54849 delete j->socket;
[email protected]aed99ef02010-08-26 14:04:32850 group->mutable_idle_sockets()->erase(j);
[email protected]43a21b82010-06-10 21:30:54851 DecrementIdleCount();
[email protected]aed99ef02010-08-26 14:04:32852 if (group->IsEmpty())
853 RemoveGroup(i);
[email protected]43a21b82010-06-10 21:30:54854
855 return;
856 }
857 }
858
859 LOG(DFATAL) << "No idle socket found to close!.";
860}
861
[email protected]05ea9ff2010-07-15 19:08:21862void ClientSocketPoolBaseHelper::InvokeUserCallbackLater(
863 ClientSocketHandle* handle, CompletionCallback* callback, int rv) {
864 CHECK(!ContainsKey(pending_callback_map_, handle));
865 pending_callback_map_[handle] = CallbackResultPair(callback, rv);
866 MessageLoop::current()->PostTask(
867 FROM_HERE,
868 NewRunnableMethod(
869 this,
870 &ClientSocketPoolBaseHelper::InvokeUserCallback,
871 handle));
872}
873
874void ClientSocketPoolBaseHelper::InvokeUserCallback(
875 ClientSocketHandle* handle) {
876 PendingCallbackMap::iterator it = pending_callback_map_.find(handle);
877
878 // Exit if the request has already been cancelled.
879 if (it == pending_callback_map_.end())
880 return;
881
882 CHECK(!handle->is_initialized());
883 CompletionCallback* callback = it->second.callback;
884 int result = it->second.result;
885 pending_callback_map_.erase(it);
886 callback->Run(result);
887}
888
[email protected]aed99ef02010-08-26 14:04:32889ClientSocketPoolBaseHelper::Group::Group()
890 : active_socket_count_(0),
891 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {}
892
893ClientSocketPoolBaseHelper::Group::~Group() {
894 CleanupBackupJob();
895}
896
897void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer(
898 const std::string& group_name,
899 ClientSocketPoolBaseHelper* pool) {
900 // Only allow one timer pending to create a backup socket.
901 if (!method_factory_.empty())
902 return;
903
904 MessageLoop::current()->PostDelayedTask(
905 FROM_HERE,
906 method_factory_.NewRunnableMethod(
907 &Group::OnBackupSocketTimerFired, group_name, pool),
908 pool->ConnectRetryIntervalMs());
909}
910
911void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired(
912 std::string group_name,
913 ClientSocketPoolBaseHelper* pool) {
914 // If there are no more jobs pending, there is no work to do.
915 // If we've done our cleanups correctly, this should not happen.
916 if (jobs_.empty()) {
917 NOTREACHED();
918 return;
919 }
920
921 // If our backup job is waiting on DNS, or if we can't create any sockets
922 // right now due to limits, just reset the timer.
923 if (pool->ReachedMaxSocketsLimit() ||
924 !HasAvailableSocketSlot(pool->max_sockets_per_group_) ||
925 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) {
926 StartBackupSocketTimer(group_name, pool);
927 return;
928 }
929
[email protected]4baaf9d2010-08-31 15:15:44930 if (pending_requests_.empty()) {
931 LOG(DFATAL) << "No pending request for backup job.";
932 return;
933 }
934
[email protected]aed99ef02010-08-26 14:04:32935 ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob(
936 group_name, **pending_requests_.begin(), pool);
937 backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED, NULL);
938 SIMPLE_STATS_COUNTER("socket.backup_created");
939 int rv = backup_job->Connect();
940 pool->connecting_socket_count_++;
941 AddJob(backup_job);
942 if (rv != ERR_IO_PENDING)
943 pool->OnConnectJobComplete(rv, backup_job);
944}
945
946void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() {
947 // Delete active jobs.
948 STLDeleteElements(&jobs_);
949
950 // Cancel pending backup job.
951 method_factory_.RevokeAll();
952}
953
[email protected]d80a4322009-08-14 07:07:49954} // namespace internal
955
[email protected]ff579d42009-06-24 15:47:02956} // namespace net