[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/socket/client_socket_pool_base.h" |
| 6 | |
| 7 | #include "base/compiler_specific.h" |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 8 | #include "base/format_macros.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 9 | #include "base/message_loop.h" |
[email protected] | 835d7c8 | 2010-10-14 04:38:38 | [diff] [blame] | 10 | #include "base/metrics/stats_counters.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 11 | #include "base/stl_util-inl.h" |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 12 | #include "base/string_util.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 13 | #include "base/time.h" |
[email protected] | 9349cfb | 2010-08-31 18:00:53 | [diff] [blame] | 14 | #include "base/values.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 15 | #include "net/base/net_log.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 16 | #include "net/base/net_errors.h" |
| 17 | #include "net/socket/client_socket_handle.h" |
| 18 | |
| 19 | using base::TimeDelta; |
| 20 | |
| 21 | namespace { |
| 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. |
| 29 | const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. |
| 30 | |
[email protected] | c847c2a | 2011-04-08 13:56:14 | [diff] [blame] | 31 | // Indicate whether or not we should establish a new transport layer connection |
| 32 | // after a certain timeout has passed without receiving an ACK. |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 33 | bool g_connect_backup_jobs_enabled = true; |
| 34 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 35 | } // namespace |
| 36 | |
| 37 | namespace net { |
| 38 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 39 | ConnectJob::ConnectJob(const std::string& group_name, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 40 | base::TimeDelta timeout_duration, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 41 | Delegate* delegate, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 42 | const BoundNetLog& net_log) |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 43 | : group_name_(group_name), |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 44 | timeout_duration_(timeout_duration), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 45 | delegate_(delegate), |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 46 | net_log_(net_log), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 47 | idle_(true), |
| 48 | preconnect_state_(NOT_PRECONNECT) { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 49 | DCHECK(!group_name.empty()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 50 | DCHECK(delegate); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 51 | net_log.BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 54 | ConnectJob::~ConnectJob() { |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 55 | net_log().EndEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB, NULL); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 56 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 57 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 58 | void ConnectJob::Initialize(bool is_preconnect) { |
| 59 | if (is_preconnect) |
| 60 | preconnect_state_ = UNUSED_PRECONNECT; |
| 61 | else |
| 62 | preconnect_state_ = NOT_PRECONNECT; |
| 63 | } |
| 64 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 65 | int ConnectJob::Connect() { |
| 66 | if (timeout_duration_ != base::TimeDelta()) |
| 67 | timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 68 | |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 69 | idle_ = false; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 70 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 71 | LogConnectStart(); |
| 72 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 73 | int rv = ConnectInternal(); |
| 74 | |
| 75 | if (rv != ERR_IO_PENDING) { |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 76 | LogConnectCompletion(rv); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 77 | delegate_ = NULL; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | return rv; |
| 81 | } |
| 82 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 83 | void ConnectJob::UseForNormalRequest() { |
| 84 | DCHECK_EQ(UNUSED_PRECONNECT, preconnect_state_); |
| 85 | preconnect_state_ = USED_PRECONNECT; |
| 86 | } |
| 87 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 88 | void ConnectJob::set_socket(StreamSocket* socket) { |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 89 | if (socket) { |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 90 | net_log().AddEvent(NetLog::TYPE_CONNECT_JOB_SET_SOCKET, make_scoped_refptr( |
| 91 | new NetLogSourceParameter("source_dependency", |
| 92 | socket->NetLog().source()))); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 93 | } |
| 94 | socket_.reset(socket); |
| 95 | } |
| 96 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 97 | void ConnectJob::NotifyDelegateOfCompletion(int rv) { |
| 98 | // The delegate will delete |this|. |
| 99 | Delegate *delegate = delegate_; |
| 100 | delegate_ = NULL; |
| 101 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 102 | LogConnectCompletion(rv); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 103 | delegate->OnConnectJobComplete(rv, this); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 104 | } |
| 105 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 106 | void ConnectJob::ResetTimer(base::TimeDelta remaining_time) { |
| 107 | timer_.Stop(); |
| 108 | timer_.Start(remaining_time, this, &ConnectJob::OnTimeout); |
| 109 | } |
| 110 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 111 | void ConnectJob::LogConnectStart() { |
| 112 | net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 113 | make_scoped_refptr(new NetLogStringParameter("group_name", group_name_))); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void ConnectJob::LogConnectCompletion(int net_error) { |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 117 | net_log().EndEventWithNetErrorCode( |
| 118 | NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_CONNECT, net_error); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 119 | } |
| 120 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 121 | void ConnectJob::OnTimeout() { |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 122 | // Make sure the socket is NULL before calling into |delegate|. |
| 123 | set_socket(NULL); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 124 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 125 | net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT, NULL); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 126 | |
| 127 | NotifyDelegateOfCompletion(ERR_TIMED_OUT); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 130 | namespace internal { |
| 131 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 132 | ClientSocketPoolBaseHelper::Request::Request( |
| 133 | ClientSocketHandle* handle, |
| 134 | CompletionCallback* callback, |
| 135 | RequestPriority priority, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 136 | bool ignore_limits, |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 137 | Flags flags, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 138 | const BoundNetLog& net_log) |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 139 | : handle_(handle), |
| 140 | callback_(callback), |
| 141 | priority_(priority), |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 142 | ignore_limits_(ignore_limits), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 143 | flags_(flags), |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 144 | net_log_(net_log) {} |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 145 | |
| 146 | ClientSocketPoolBaseHelper::Request::~Request() {} |
| 147 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 148 | ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 149 | int max_sockets, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 150 | int max_sockets_per_group, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 151 | base::TimeDelta unused_idle_socket_timeout, |
| 152 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 153 | ConnectJobFactory* connect_job_factory) |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 154 | : idle_socket_count_(0), |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 155 | connecting_socket_count_(0), |
| 156 | handed_out_socket_count_(0), |
| 157 | max_sockets_(max_sockets), |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 158 | max_sockets_per_group_(max_sockets_per_group), |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 159 | unused_idle_socket_timeout_(unused_idle_socket_timeout), |
| 160 | used_idle_socket_timeout_(used_idle_socket_timeout), |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 161 | connect_job_factory_(connect_job_factory), |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 162 | connect_backup_jobs_enabled_(false), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 163 | pool_generation_number_(0), |
| 164 | method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 165 | DCHECK_LE(0, max_sockets_per_group); |
| 166 | DCHECK_LE(max_sockets_per_group, max_sockets); |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 167 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 168 | NetworkChangeNotifier::AddIPAddressObserver(this); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 169 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 170 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 171 | ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 172 | // Clean up any idle sockets and pending connect jobs. Assert that we have no |
| 173 | // remaining active sockets or pending requests. They should have all been |
| 174 | // cleaned up prior to |this| being destroyed. |
| 175 | Flush(); |
| 176 | DCHECK(group_map_.empty()); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 177 | DCHECK(pending_callback_map_.empty()); |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 178 | DCHECK_EQ(0, connecting_socket_count_); |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 179 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 180 | NetworkChangeNotifier::RemoveIPAddressObserver(this); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | // InsertRequestIntoQueue inserts the request into the queue based on |
| 184 | // priority. Highest priorities are closest to the front. Older requests are |
| 185 | // prioritized over requests of equal priority. |
| 186 | // |
| 187 | // static |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 188 | void ClientSocketPoolBaseHelper::InsertRequestIntoQueue( |
| 189 | const Request* r, RequestQueue* pending_requests) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 190 | RequestQueue::iterator it = pending_requests->begin(); |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 191 | while (it != pending_requests->end() && r->priority() >= (*it)->priority()) |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 192 | ++it; |
| 193 | pending_requests->insert(it, r); |
| 194 | } |
| 195 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 196 | // static |
| 197 | const ClientSocketPoolBaseHelper::Request* |
| 198 | ClientSocketPoolBaseHelper::RemoveRequestFromQueue( |
[email protected] | 417da10 | 2011-03-11 17:45:05 | [diff] [blame] | 199 | const RequestQueue::iterator& it, Group* group) { |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 200 | const Request* req = *it; |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 201 | group->mutable_pending_requests()->erase(it); |
| 202 | // If there are no more requests, we kill the backup timer. |
| 203 | if (group->pending_requests().empty()) |
| 204 | group->CleanupBackupJob(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 205 | return req; |
| 206 | } |
| 207 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 208 | int ClientSocketPoolBaseHelper::RequestSocket( |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 209 | const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 210 | const Request* request) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 211 | CHECK(request->callback()); |
| 212 | CHECK(request->handle()); |
| 213 | |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 214 | request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 215 | Group* group = GetOrCreateGroup(group_name); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 216 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 217 | int rv = RequestSocketInternal(group_name, request); |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 218 | if (rv != ERR_IO_PENDING) { |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 219 | request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 220 | CHECK(!request->handle()->is_initialized()); |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 221 | delete request; |
| 222 | } else { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 223 | InsertRequestIntoQueue(request, group->mutable_pending_requests()); |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 224 | } |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 225 | return rv; |
| 226 | } |
| 227 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 228 | void ClientSocketPoolBaseHelper::RequestSockets( |
| 229 | const std::string& group_name, |
| 230 | const Request& request, |
| 231 | int num_sockets) { |
| 232 | DCHECK(!request.callback()); |
| 233 | DCHECK(!request.handle()); |
| 234 | |
| 235 | if (num_sockets > max_sockets_per_group_) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 236 | num_sockets = max_sockets_per_group_; |
| 237 | } |
| 238 | |
| 239 | request.net_log().BeginEvent( |
| 240 | NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 241 | make_scoped_refptr(new NetLogIntegerParameter( |
| 242 | "num_sockets", num_sockets))); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 243 | |
| 244 | Group* group = GetOrCreateGroup(group_name); |
| 245 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 246 | // RequestSocketsInternal() may delete the group. |
| 247 | bool deleted_group = false; |
| 248 | |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 249 | int rv = OK; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 250 | for (int num_iterations_left = num_sockets; |
| 251 | group->NumActiveSocketSlots() < num_sockets && |
| 252 | num_iterations_left > 0 ; num_iterations_left--) { |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 253 | rv = RequestSocketInternal(group_name, &request); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 254 | if (rv < 0 && rv != ERR_IO_PENDING) { |
| 255 | // We're encountering a synchronous error. Give up. |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 256 | if (!ContainsKey(group_map_, group_name)) |
| 257 | deleted_group = true; |
| 258 | break; |
| 259 | } |
| 260 | if (!ContainsKey(group_map_, group_name)) { |
| 261 | // Unexpected. The group should only be getting deleted on synchronous |
| 262 | // error. |
| 263 | NOTREACHED(); |
| 264 | deleted_group = true; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 265 | break; |
| 266 | } |
| 267 | } |
| 268 | |
[email protected] | 3c819f52 | 2010-12-02 02:03:12 | [diff] [blame] | 269 | if (!deleted_group && group->IsEmpty()) |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 270 | RemoveGroup(group_name); |
| 271 | |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 272 | if (rv == ERR_IO_PENDING) |
| 273 | rv = OK; |
| 274 | request.net_log().EndEventWithNetErrorCode( |
| 275 | NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 276 | } |
| 277 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 278 | int ClientSocketPoolBaseHelper::RequestSocketInternal( |
| 279 | const std::string& group_name, |
| 280 | const Request* request) { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 281 | DCHECK_GE(request->priority(), 0); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 282 | ClientSocketHandle* const handle = request->handle(); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 283 | const bool preconnecting = !handle; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 284 | Group* group = GetOrCreateGroup(group_name); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 285 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 286 | if (!(request->flags() & NO_IDLE_SOCKETS)) { |
| 287 | // Try to reuse a socket. |
| 288 | if (AssignIdleSocketToGroup(request, group)) |
| 289 | return OK; |
| 290 | } |
| 291 | |
| 292 | if (!preconnecting && group->TryToUsePreconnectConnectJob()) |
| 293 | return ERR_IO_PENDING; |
[email protected] | 6555210 | 2010-04-09 22:58:10 | [diff] [blame] | 294 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 295 | // Can we make another active socket now? |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 296 | if (!group->HasAvailableSocketSlot(max_sockets_per_group_) && |
| 297 | !request->ignore_limits()) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 298 | request->net_log().AddEvent( |
| 299 | NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP, NULL); |
| 300 | return ERR_IO_PENDING; |
| 301 | } |
| 302 | |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 303 | if (ReachedMaxSocketsLimit() && !request->ignore_limits()) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 304 | if (idle_socket_count() > 0) { |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 305 | bool closed = CloseOneIdleSocketExceptInGroup(group); |
| 306 | if (preconnecting && !closed) |
| 307 | return ERR_PRECONNECT_MAX_SOCKET_LIMIT; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 308 | } else { |
| 309 | // We could check if we really have a stalled group here, but it requires |
| 310 | // a scan of all groups, so just flip a flag here, and do the check later. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 311 | request->net_log().AddEvent( |
| 312 | NetLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS, NULL); |
| 313 | return ERR_IO_PENDING; |
| 314 | } |
| 315 | } |
| 316 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 317 | // We couldn't find a socket to reuse, so allocate and connect a new one. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 318 | scoped_ptr<ConnectJob> connect_job( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 319 | connect_job_factory_->NewConnectJob(group_name, *request, this)); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 320 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 321 | connect_job->Initialize(preconnecting); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 322 | int rv = connect_job->Connect(); |
| 323 | if (rv == OK) { |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 324 | LogBoundConnectJobToRequest(connect_job->net_log().source(), request); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 325 | if (!preconnecting) { |
| 326 | HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */, |
| 327 | handle, base::TimeDelta(), group, request->net_log()); |
| 328 | } else { |
| 329 | AddIdleSocket(connect_job->ReleaseSocket(), group); |
| 330 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 331 | } else if (rv == ERR_IO_PENDING) { |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 332 | // If we don't have any sockets in this group, set a timer for potentially |
| 333 | // creating a new one. If the SYN is lost, this backup socket may complete |
| 334 | // before the slow socket, improving end user latency. |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 335 | if (connect_backup_jobs_enabled_ && |
| 336 | group->IsEmpty() && !group->HasBackupJob() && |
| 337 | handle) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 338 | group->StartBackupSocketTimer(group_name, this); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 339 | } |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 340 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 341 | connecting_socket_count_++; |
| 342 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 343 | group->AddJob(connect_job.release()); |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 344 | } else { |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 345 | LogBoundConnectJobToRequest(connect_job->net_log().source(), request); |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 346 | StreamSocket* error_socket = NULL; |
[email protected] | fd2e53e | 2011-01-14 20:40:52 | [diff] [blame] | 347 | if (!preconnecting) { |
| 348 | DCHECK(handle); |
| 349 | connect_job->GetAdditionalErrorState(handle); |
| 350 | error_socket = connect_job->ReleaseSocket(); |
| 351 | } |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 352 | if (error_socket) { |
| 353 | HandOutSocket(error_socket, false /* not reused */, handle, |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 354 | base::TimeDelta(), group, request->net_log()); |
| 355 | } else if (group->IsEmpty()) { |
| 356 | RemoveGroup(group_name); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 357 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 358 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 359 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 360 | return rv; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 361 | } |
| 362 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 363 | bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 364 | const Request* request, Group* group) { |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 365 | std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
| 366 | std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
| 367 | |
| 368 | // Iterate through the idle sockets forwards (oldest to newest) |
| 369 | // * Delete any disconnected ones. |
| 370 | // * If we find a used idle socket, assign to |idle_socket|. At the end, |
| 371 | // the |idle_socket_it| will be set to the newest used idle socket. |
| 372 | for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); |
| 373 | it != idle_sockets->end();) { |
| 374 | if (!it->socket->IsConnectedAndIdle()) { |
| 375 | DecrementIdleCount(); |
| 376 | delete it->socket; |
| 377 | it = idle_sockets->erase(it); |
| 378 | continue; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 379 | } |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 380 | |
| 381 | if (it->socket->WasEverUsed()) { |
| 382 | // We found one we can reuse! |
| 383 | idle_socket_it = it; |
| 384 | } |
| 385 | |
| 386 | ++it; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 387 | } |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 388 | |
| 389 | // If we haven't found an idle socket, that means there are no used idle |
| 390 | // sockets. Pick the oldest (first) idle socket (FIFO). |
| 391 | |
| 392 | if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) |
| 393 | idle_socket_it = idle_sockets->begin(); |
| 394 | |
| 395 | if (idle_socket_it != idle_sockets->end()) { |
| 396 | DecrementIdleCount(); |
| 397 | base::TimeDelta idle_time = |
| 398 | base::TimeTicks::Now() - idle_socket_it->start_time; |
| 399 | IdleSocket idle_socket = *idle_socket_it; |
| 400 | idle_sockets->erase(idle_socket_it); |
| 401 | HandOutSocket( |
| 402 | idle_socket.socket, |
| 403 | idle_socket.socket->WasEverUsed(), |
| 404 | request->handle(), |
| 405 | idle_time, |
| 406 | group, |
| 407 | request->net_log()); |
| 408 | return true; |
| 409 | } |
| 410 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 414 | // static |
| 415 | void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest( |
| 416 | const NetLog::Source& connect_job_source, const Request* request) { |
| 417 | request->net_log().AddEvent( |
| 418 | NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 419 | make_scoped_refptr(new NetLogSourceParameter( |
| 420 | "source_dependency", connect_job_source))); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 423 | void ClientSocketPoolBaseHelper::CancelRequest( |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 424 | const std::string& group_name, ClientSocketHandle* handle) { |
| 425 | PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle); |
| 426 | if (callback_it != pending_callback_map_.end()) { |
| 427 | int result = callback_it->second.result; |
| 428 | pending_callback_map_.erase(callback_it); |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 429 | StreamSocket* socket = handle->release_socket(); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 430 | if (socket) { |
| 431 | if (result != OK) |
| 432 | socket->Disconnect(); |
| 433 | ReleaseSocket(handle->group_name(), socket, handle->id()); |
| 434 | } |
| 435 | return; |
| 436 | } |
[email protected] | b6501d3d | 2010-06-03 23:53:34 | [diff] [blame] | 437 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 438 | CHECK(ContainsKey(group_map_, group_name)); |
| 439 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 440 | Group* group = GetOrCreateGroup(group_name); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 441 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 442 | // Search pending_requests for matching handle. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 443 | RequestQueue::iterator it = group->mutable_pending_requests()->begin(); |
| 444 | for (; it != group->pending_requests().end(); ++it) { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 445 | if ((*it)->handle() == handle) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 446 | scoped_ptr<const Request> req(RemoveRequestFromQueue(it, group)); |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 447 | req->net_log().AddEvent(NetLog::TYPE_CANCELLED, NULL); |
| 448 | req->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 449 | |
| 450 | // We let the job run, unless we're at the socket limit. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 451 | if (group->jobs().size() && ReachedMaxSocketsLimit()) { |
| 452 | RemoveConnectJob(*group->jobs().begin(), group); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 453 | CheckForStalledSocketGroups(); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 454 | } |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 455 | break; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 456 | } |
| 457 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 460 | bool ClientSocketPoolBaseHelper::HasGroup(const std::string& group_name) const { |
| 461 | return ContainsKey(group_map_, group_name); |
| 462 | } |
| 463 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 464 | void ClientSocketPoolBaseHelper::CloseIdleSockets() { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 465 | CleanupIdleSockets(true); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 466 | DCHECK_EQ(0, idle_socket_count_); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 467 | } |
| 468 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 469 | int ClientSocketPoolBaseHelper::IdleSocketCountInGroup( |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 470 | const std::string& group_name) const { |
| 471 | GroupMap::const_iterator i = group_map_.find(group_name); |
| 472 | CHECK(i != group_map_.end()); |
| 473 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 474 | return i->second->idle_sockets().size(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 475 | } |
| 476 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 477 | LoadState ClientSocketPoolBaseHelper::GetLoadState( |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 478 | const std::string& group_name, |
| 479 | const ClientSocketHandle* handle) const { |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 480 | if (ContainsKey(pending_callback_map_, handle)) |
| 481 | return LOAD_STATE_CONNECTING; |
| 482 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 483 | if (!ContainsKey(group_map_, group_name)) { |
| 484 | NOTREACHED() << "ClientSocketPool does not contain group: " << group_name |
| 485 | << " for handle: " << handle; |
| 486 | return LOAD_STATE_IDLE; |
| 487 | } |
| 488 | |
| 489 | // Can't use operator[] since it is non-const. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 490 | const Group& group = *group_map_.find(group_name)->second; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 491 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 492 | // Search pending_requests for matching handle. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 493 | RequestQueue::const_iterator it = group.pending_requests().begin(); |
| 494 | for (size_t i = 0; it != group.pending_requests().end(); ++it, ++i) { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 495 | if ((*it)->handle() == handle) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 496 | if (i < group.jobs().size()) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 497 | LoadState max_state = LOAD_STATE_IDLE; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 498 | for (ConnectJobSet::const_iterator job_it = group.jobs().begin(); |
| 499 | job_it != group.jobs().end(); ++job_it) { |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 500 | max_state = std::max(max_state, (*job_it)->GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 501 | } |
| 502 | return max_state; |
| 503 | } else { |
| 504 | // TODO(wtc): Add a state for being on the wait list. |
| 505 | // See http://www.crbug.com/5077. |
| 506 | return LOAD_STATE_IDLE; |
| 507 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | |
| 511 | NOTREACHED(); |
| 512 | return LOAD_STATE_IDLE; |
| 513 | } |
| 514 | |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 515 | DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue( |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 516 | const std::string& name, const std::string& type) const { |
| 517 | DictionaryValue* dict = new DictionaryValue(); |
| 518 | dict->SetString("name", name); |
| 519 | dict->SetString("type", type); |
| 520 | dict->SetInteger("handed_out_socket_count", handed_out_socket_count_); |
| 521 | dict->SetInteger("connecting_socket_count", connecting_socket_count_); |
| 522 | dict->SetInteger("idle_socket_count", idle_socket_count_); |
| 523 | dict->SetInteger("max_socket_count", max_sockets_); |
| 524 | dict->SetInteger("max_sockets_per_group", max_sockets_per_group_); |
| 525 | dict->SetInteger("pool_generation_number", pool_generation_number_); |
| 526 | |
| 527 | if (group_map_.empty()) |
| 528 | return dict; |
| 529 | |
| 530 | DictionaryValue* all_groups_dict = new DictionaryValue(); |
| 531 | for (GroupMap::const_iterator it = group_map_.begin(); |
| 532 | it != group_map_.end(); it++) { |
| 533 | const Group* group = it->second; |
| 534 | DictionaryValue* group_dict = new DictionaryValue(); |
| 535 | |
| 536 | group_dict->SetInteger("pending_request_count", |
| 537 | group->pending_requests().size()); |
| 538 | if (!group->pending_requests().empty()) { |
| 539 | group_dict->SetInteger("top_pending_priority", |
| 540 | group->TopPendingPriority()); |
| 541 | } |
| 542 | |
| 543 | group_dict->SetInteger("active_socket_count", group->active_socket_count()); |
[email protected] | 0496f9a3 | 2010-09-30 16:08:07 | [diff] [blame] | 544 | |
| 545 | ListValue* idle_socket_list = new ListValue(); |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 546 | std::list<IdleSocket>::const_iterator idle_socket; |
[email protected] | 0496f9a3 | 2010-09-30 16:08:07 | [diff] [blame] | 547 | for (idle_socket = group->idle_sockets().begin(); |
| 548 | idle_socket != group->idle_sockets().end(); |
| 549 | idle_socket++) { |
| 550 | int source_id = idle_socket->socket->NetLog().source().id; |
| 551 | idle_socket_list->Append(Value::CreateIntegerValue(source_id)); |
| 552 | } |
| 553 | group_dict->Set("idle_sockets", idle_socket_list); |
| 554 | |
| 555 | ListValue* connect_jobs_list = new ListValue(); |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 556 | std::set<ConnectJob*>::const_iterator job = group->jobs().begin(); |
[email protected] | 0496f9a3 | 2010-09-30 16:08:07 | [diff] [blame] | 557 | for (job = group->jobs().begin(); job != group->jobs().end(); job++) { |
| 558 | int source_id = (*job)->net_log().source().id; |
| 559 | connect_jobs_list->Append(Value::CreateIntegerValue(source_id)); |
| 560 | } |
| 561 | group_dict->Set("connect_jobs", connect_jobs_list); |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 562 | |
| 563 | group_dict->SetBoolean("is_stalled", |
| 564 | group->IsStalled(max_sockets_per_group_)); |
| 565 | group_dict->SetBoolean("has_backup_job", group->HasBackupJob()); |
| 566 | |
| 567 | all_groups_dict->SetWithoutPathExpansion(it->first, group_dict); |
| 568 | } |
| 569 | dict->Set("groups", all_groups_dict); |
| 570 | return dict; |
| 571 | } |
| 572 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 573 | bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup( |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 574 | base::TimeTicks now, |
| 575 | base::TimeDelta timeout) const { |
| 576 | bool timed_out = (now - start_time) >= timeout; |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 577 | if (timed_out) |
| 578 | return true; |
| 579 | if (socket->WasEverUsed()) |
| 580 | return !socket->IsConnectedAndIdle(); |
| 581 | return !socket->IsConnected(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 582 | } |
| 583 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 584 | void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 585 | if (idle_socket_count_ == 0) |
| 586 | return; |
| 587 | |
| 588 | // Current time value. Retrieving it once at the function start rather than |
| 589 | // inside the inner loop, since it shouldn't change by any meaningful amount. |
| 590 | base::TimeTicks now = base::TimeTicks::Now(); |
| 591 | |
| 592 | GroupMap::iterator i = group_map_.begin(); |
| 593 | while (i != group_map_.end()) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 594 | Group* group = i->second; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 595 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 596 | std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin(); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 597 | while (j != group->idle_sockets().end()) { |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 598 | base::TimeDelta timeout = |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 599 | j->socket->WasEverUsed() ? |
| 600 | used_idle_socket_timeout_ : unused_idle_socket_timeout_; |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 601 | if (force || j->ShouldCleanup(now, timeout)) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 602 | delete j->socket; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 603 | j = group->mutable_idle_sockets()->erase(j); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 604 | DecrementIdleCount(); |
| 605 | } else { |
| 606 | ++j; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | // Delete group if no longer needed. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 611 | if (group->IsEmpty()) { |
| 612 | RemoveGroup(i++); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 613 | } else { |
| 614 | ++i; |
| 615 | } |
| 616 | } |
| 617 | } |
| 618 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 619 | ClientSocketPoolBaseHelper::Group* ClientSocketPoolBaseHelper::GetOrCreateGroup( |
| 620 | const std::string& group_name) { |
| 621 | GroupMap::iterator it = group_map_.find(group_name); |
| 622 | if (it != group_map_.end()) |
| 623 | return it->second; |
| 624 | Group* group = new Group; |
| 625 | group_map_[group_name] = group; |
| 626 | return group; |
| 627 | } |
| 628 | |
| 629 | void ClientSocketPoolBaseHelper::RemoveGroup(const std::string& group_name) { |
| 630 | GroupMap::iterator it = group_map_.find(group_name); |
| 631 | CHECK(it != group_map_.end()); |
| 632 | |
| 633 | RemoveGroup(it); |
| 634 | } |
| 635 | |
| 636 | void ClientSocketPoolBaseHelper::RemoveGroup(GroupMap::iterator it) { |
| 637 | delete it->second; |
| 638 | group_map_.erase(it); |
| 639 | } |
| 640 | |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 641 | // static |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 642 | bool ClientSocketPoolBaseHelper::connect_backup_jobs_enabled() { |
| 643 | return g_connect_backup_jobs_enabled; |
| 644 | } |
| 645 | |
| 646 | // static |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 647 | bool ClientSocketPoolBaseHelper::set_connect_backup_jobs_enabled(bool enabled) { |
| 648 | bool old_value = g_connect_backup_jobs_enabled; |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 649 | g_connect_backup_jobs_enabled = enabled; |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 650 | return old_value; |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
| 654 | connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
| 655 | } |
| 656 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 657 | void ClientSocketPoolBaseHelper::IncrementIdleCount() { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 658 | if (++idle_socket_count_ == 1) |
| 659 | timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 660 | &ClientSocketPoolBaseHelper::OnCleanupTimerFired); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 661 | } |
| 662 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 663 | void ClientSocketPoolBaseHelper::DecrementIdleCount() { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 664 | if (--idle_socket_count_ == 0) |
| 665 | timer_.Stop(); |
| 666 | } |
| 667 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 668 | void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 669 | StreamSocket* socket, |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 670 | int id) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 671 | GroupMap::iterator i = group_map_.find(group_name); |
| 672 | CHECK(i != group_map_.end()); |
| 673 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 674 | Group* group = i->second; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 675 | |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 676 | CHECK_GT(handed_out_socket_count_, 0); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 677 | handed_out_socket_count_--; |
| 678 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 679 | CHECK_GT(group->active_socket_count(), 0); |
| 680 | group->DecrementActiveSocketCount(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 681 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 682 | const bool can_reuse = socket->IsConnectedAndIdle() && |
| 683 | id == pool_generation_number_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 684 | if (can_reuse) { |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 685 | // Add it to the idle list. |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 686 | AddIdleSocket(socket, group); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 687 | OnAvailableSocketSlot(group_name, group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 688 | } else { |
| 689 | delete socket; |
| 690 | } |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 691 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 692 | CheckForStalledSocketGroups(); |
| 693 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 694 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 695 | void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() { |
| 696 | // If we have idle sockets, see if we can give one to the top-stalled group. |
| 697 | std::string top_group_name; |
| 698 | Group* top_group = NULL; |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 699 | if (!FindTopStalledGroup(&top_group, &top_group_name)) |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 700 | return; |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 701 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 702 | if (ReachedMaxSocketsLimit()) { |
| 703 | if (idle_socket_count() > 0) { |
| 704 | CloseOneIdleSocket(); |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 705 | } else { |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 706 | // We can't activate more sockets since we're already at our global |
| 707 | // limit. |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 708 | return; |
[email protected] | d7027bb | 2010-05-10 18:58:54 | [diff] [blame] | 709 | } |
[email protected] | 4f2abec | 2010-02-03 18:10:16 | [diff] [blame] | 710 | } |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 711 | |
| 712 | // Note: we don't loop on waking stalled groups. If the stalled group is at |
| 713 | // its limit, may be left with other stalled groups that could be |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 714 | // woken. This isn't optimal, but there is no starvation, so to avoid |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 715 | // the looping we leave it at this. |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 716 | OnAvailableSocketSlot(top_group_name, top_group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 717 | } |
| 718 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 719 | // Search for the highest priority pending request, amongst the groups that |
| 720 | // are not at the |max_sockets_per_group_| limit. Note: for requests with |
| 721 | // the same priority, the winner is based on group hash ordering (and not |
| 722 | // insertion order). |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 723 | bool ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group, |
| 724 | std::string* group_name) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 725 | Group* top_group = NULL; |
| 726 | const std::string* top_group_name = NULL; |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 727 | bool has_stalled_group = false; |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 728 | for (GroupMap::iterator i = group_map_.begin(); |
| 729 | i != group_map_.end(); ++i) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 730 | Group* curr_group = i->second; |
| 731 | const RequestQueue& queue = curr_group->pending_requests(); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 732 | if (queue.empty()) |
| 733 | continue; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 734 | if (curr_group->IsStalled(max_sockets_per_group_)) { |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 735 | has_stalled_group = true; |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 736 | bool has_higher_priority = !top_group || |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 737 | curr_group->TopPendingPriority() < top_group->TopPendingPriority(); |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 738 | if (has_higher_priority) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 739 | top_group = curr_group; |
[email protected] | 6427fe2 | 2010-04-16 22:27:41 | [diff] [blame] | 740 | top_group_name = &i->first; |
| 741 | } |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 742 | } |
| 743 | } |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 744 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 745 | if (top_group) { |
| 746 | *group = top_group; |
| 747 | *group_name = *top_group_name; |
| 748 | } |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 749 | return has_stalled_group; |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 750 | } |
| 751 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 752 | void ClientSocketPoolBaseHelper::OnConnectJobComplete( |
| 753 | int result, ConnectJob* job) { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 754 | DCHECK_NE(ERR_IO_PENDING, result); |
| 755 | const std::string group_name = job->group_name(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 756 | GroupMap::iterator group_it = group_map_.find(group_name); |
| 757 | CHECK(group_it != group_map_.end()); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 758 | Group* group = group_it->second; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 759 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 760 | scoped_ptr<StreamSocket> socket(job->ReleaseSocket()); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 761 | |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 762 | BoundNetLog job_log = job->net_log(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 763 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 764 | if (result == OK) { |
| 765 | DCHECK(socket.get()); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 766 | RemoveConnectJob(job, group); |
| 767 | if (!group->pending_requests().empty()) { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 768 | scoped_ptr<const Request> r(RemoveRequestFromQueue( |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 769 | group->mutable_pending_requests()->begin(), group)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 770 | LogBoundConnectJobToRequest(job_log.source(), r.get()); |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 771 | HandOutSocket( |
| 772 | socket.release(), false /* unused socket */, r->handle(), |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 773 | base::TimeDelta(), group, r->net_log()); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 774 | r->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL, NULL); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 775 | InvokeUserCallbackLater(r->handle(), r->callback(), result); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 776 | } else { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 777 | AddIdleSocket(socket.release(), group); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 778 | OnAvailableSocketSlot(group_name, group); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 779 | CheckForStalledSocketGroups(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 780 | } |
[email protected] | 94c2047 | 2010-01-14 08:14:36 | [diff] [blame] | 781 | } else { |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 782 | // If we got a socket, it must contain error information so pass that |
| 783 | // up so that the caller can retrieve it. |
| 784 | bool handed_out_socket = false; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 785 | if (!group->pending_requests().empty()) { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 786 | scoped_ptr<const Request> r(RemoveRequestFromQueue( |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 787 | group->mutable_pending_requests()->begin(), group)); |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 788 | LogBoundConnectJobToRequest(job_log.source(), r.get()); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 789 | job->GetAdditionalErrorState(r->handle()); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 790 | RemoveConnectJob(job, group); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 791 | if (socket.get()) { |
| 792 | handed_out_socket = true; |
| 793 | HandOutSocket(socket.release(), false /* unused socket */, r->handle(), |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 794 | base::TimeDelta(), group, r->net_log()); |
[email protected] | e772db3f | 2010-07-12 18:11:13 | [diff] [blame] | 795 | } |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 796 | r->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, |
| 797 | result); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 798 | InvokeUserCallbackLater(r->handle(), r->callback(), result); |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 799 | } else { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 800 | RemoveConnectJob(job, group); |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 801 | } |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 802 | if (!handed_out_socket) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 803 | OnAvailableSocketSlot(group_name, group); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 804 | CheckForStalledSocketGroups(); |
| 805 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 806 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 807 | } |
| 808 | |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 809 | void ClientSocketPoolBaseHelper::OnIPAddressChanged() { |
| 810 | Flush(); |
| 811 | } |
| 812 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 813 | void ClientSocketPoolBaseHelper::Flush() { |
| 814 | pool_generation_number_++; |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 815 | CancelAllConnectJobs(); |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 816 | CloseIdleSockets(); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 817 | AbortAllRequests(); |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 818 | } |
| 819 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 820 | void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job, |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 821 | Group* group) { |
[email protected] | b1f031dd | 2010-03-02 23:19:33 | [diff] [blame] | 822 | CHECK_GT(connecting_socket_count_, 0); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 823 | connecting_socket_count_--; |
| 824 | |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 825 | DCHECK(group); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 826 | DCHECK(ContainsKey(group->jobs(), job)); |
| 827 | group->RemoveJob(job); |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 828 | |
| 829 | // If we've got no more jobs for this group, then we no longer need a |
| 830 | // backup job either. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 831 | if (group->jobs().empty()) |
[email protected] | 25eea38 | 2010-07-10 23:55:26 | [diff] [blame] | 832 | group->CleanupBackupJob(); |
| 833 | |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 834 | DCHECK(job); |
| 835 | delete job; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 836 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 837 | |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 838 | void ClientSocketPoolBaseHelper::OnAvailableSocketSlot( |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 839 | const std::string& group_name, Group* group) { |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 840 | DCHECK(ContainsKey(group_map_, group_name)); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 841 | if (group->IsEmpty()) |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 842 | RemoveGroup(group_name); |
| 843 | else if (!group->pending_requests().empty()) |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 844 | ProcessPendingRequest(group_name, group); |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 845 | } |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 846 | |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 847 | void ClientSocketPoolBaseHelper::ProcessPendingRequest( |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 848 | const std::string& group_name, Group* group) { |
| 849 | int rv = RequestSocketInternal(group_name, |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 850 | *group->pending_requests().begin()); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 851 | if (rv != ERR_IO_PENDING) { |
| 852 | scoped_ptr<const Request> request(RemoveRequestFromQueue( |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 853 | group->mutable_pending_requests()->begin(), group)); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 854 | if (group->IsEmpty()) |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 855 | RemoveGroup(group_name); |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 856 | |
[email protected] | d7fd178 | 2011-02-08 19:16:43 | [diff] [blame] | 857 | request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 858 | InvokeUserCallbackLater( |
| 859 | request->handle(), request->callback(), rv); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 863 | void ClientSocketPoolBaseHelper::HandOutSocket( |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 864 | StreamSocket* socket, |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 865 | bool reused, |
| 866 | ClientSocketHandle* handle, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 867 | base::TimeDelta idle_time, |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 868 | Group* group, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 869 | const BoundNetLog& net_log) { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 870 | DCHECK(socket); |
| 871 | handle->set_socket(socket); |
| 872 | handle->set_is_reused(reused); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 873 | handle->set_idle_time(idle_time); |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 874 | handle->set_pool_id(pool_generation_number_); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 875 | |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 876 | if (reused) { |
[email protected] | ec11be6 | 2010-04-28 19:28:09 | [diff] [blame] | 877 | net_log.AddEvent( |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 878 | NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET, |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 879 | make_scoped_refptr(new NetLogIntegerParameter( |
| 880 | "idle_ms", static_cast<int>(idle_time.InMilliseconds())))); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 881 | } |
[email protected] | d13f51b | 2010-04-27 23:20:45 | [diff] [blame] | 882 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 883 | net_log.AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, |
[email protected] | 00cd9c4 | 2010-11-02 20:15:57 | [diff] [blame] | 884 | make_scoped_refptr(new NetLogSourceParameter( |
| 885 | "source_dependency", socket->NetLog().source()))); |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 886 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 887 | handed_out_socket_count_++; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 888 | group->IncrementActiveSocketCount(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 889 | } |
| 890 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 891 | void ClientSocketPoolBaseHelper::AddIdleSocket( |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame^] | 892 | StreamSocket* socket, Group* group) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 893 | DCHECK(socket); |
| 894 | IdleSocket idle_socket; |
| 895 | idle_socket.socket = socket; |
| 896 | idle_socket.start_time = base::TimeTicks::Now(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 897 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 898 | group->mutable_idle_sockets()->push_back(idle_socket); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 899 | IncrementIdleCount(); |
| 900 | } |
| 901 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 902 | void ClientSocketPoolBaseHelper::CancelAllConnectJobs() { |
[email protected] | 74d75e0b | 2010-08-23 20:39:54 | [diff] [blame] | 903 | for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 904 | Group* group = i->second; |
| 905 | connecting_socket_count_ -= group->jobs().size(); |
| 906 | group->RemoveAllJobs(); |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 907 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 908 | // Delete group if no longer needed. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 909 | if (group->IsEmpty()) { |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 910 | // RemoveGroup() will call .erase() which will invalidate the iterator, |
| 911 | // but i will already have been incremented to a valid iterator before |
| 912 | // RemoveGroup() is called. |
| 913 | RemoveGroup(i++); |
| 914 | } else { |
| 915 | ++i; |
| 916 | } |
| 917 | } |
| 918 | DCHECK_EQ(0, connecting_socket_count_); |
| 919 | } |
| 920 | |
| 921 | void ClientSocketPoolBaseHelper::AbortAllRequests() { |
| 922 | for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) { |
| 923 | Group* group = i->second; |
| 924 | |
| 925 | RequestQueue pending_requests; |
| 926 | pending_requests.swap(*group->mutable_pending_requests()); |
| 927 | for (RequestQueue::iterator it2 = pending_requests.begin(); |
| 928 | it2 != pending_requests.end(); ++it2) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 929 | scoped_ptr<const Request> request(*it2); |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 930 | InvokeUserCallbackLater( |
| 931 | request->handle(), request->callback(), ERR_ABORTED); |
| 932 | } |
| 933 | |
| 934 | // Delete group if no longer needed. |
| 935 | if (group->IsEmpty()) { |
| 936 | // RemoveGroup() will call .erase() which will invalidate the iterator, |
| 937 | // but i will already have been incremented to a valid iterator before |
| 938 | // RemoveGroup() is called. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 939 | RemoveGroup(i++); |
[email protected] | 74d75e0b | 2010-08-23 20:39:54 | [diff] [blame] | 940 | } else { |
| 941 | ++i; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
| 945 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 946 | bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 947 | // Each connecting socket will eventually connect and be handed out. |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 948 | int total = handed_out_socket_count_ + connecting_socket_count_ + |
| 949 | idle_socket_count(); |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 950 | // There can be more sockets than the limit since some requests can ignore |
| 951 | // the limit |
[email protected] | c901f6d | 2010-04-27 17:48:28 | [diff] [blame] | 952 | if (total < max_sockets_) |
| 953 | return false; |
[email protected] | c901f6d | 2010-04-27 17:48:28 | [diff] [blame] | 954 | return true; |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 955 | } |
| 956 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 957 | void ClientSocketPoolBaseHelper::CloseOneIdleSocket() { |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 958 | CloseOneIdleSocketExceptInGroup(NULL); |
| 959 | } |
| 960 | |
| 961 | bool ClientSocketPoolBaseHelper::CloseOneIdleSocketExceptInGroup( |
| 962 | const Group* exception_group) { |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 963 | CHECK_GT(idle_socket_count(), 0); |
| 964 | |
| 965 | for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end(); ++i) { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 966 | Group* group = i->second; |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 967 | if (exception_group == group) |
| 968 | continue; |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 969 | std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 970 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 971 | if (!idle_sockets->empty()) { |
| 972 | delete idle_sockets->front().socket; |
| 973 | idle_sockets->pop_front(); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 974 | DecrementIdleCount(); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 975 | if (group->IsEmpty()) |
| 976 | RemoveGroup(i); |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 977 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 978 | return true; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 982 | if (!exception_group) |
| 983 | LOG(DFATAL) << "No idle socket found to close!."; |
| 984 | |
| 985 | return false; |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 986 | } |
| 987 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 988 | void ClientSocketPoolBaseHelper::InvokeUserCallbackLater( |
| 989 | ClientSocketHandle* handle, CompletionCallback* callback, int rv) { |
| 990 | CHECK(!ContainsKey(pending_callback_map_, handle)); |
| 991 | pending_callback_map_[handle] = CallbackResultPair(callback, rv); |
| 992 | MessageLoop::current()->PostTask( |
| 993 | FROM_HERE, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 994 | method_factory_.NewRunnableMethod( |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 995 | &ClientSocketPoolBaseHelper::InvokeUserCallback, |
| 996 | handle)); |
| 997 | } |
| 998 | |
| 999 | void ClientSocketPoolBaseHelper::InvokeUserCallback( |
| 1000 | ClientSocketHandle* handle) { |
| 1001 | PendingCallbackMap::iterator it = pending_callback_map_.find(handle); |
| 1002 | |
| 1003 | // Exit if the request has already been cancelled. |
| 1004 | if (it == pending_callback_map_.end()) |
| 1005 | return; |
| 1006 | |
| 1007 | CHECK(!handle->is_initialized()); |
| 1008 | CompletionCallback* callback = it->second.callback; |
| 1009 | int result = it->second.result; |
| 1010 | pending_callback_map_.erase(it); |
| 1011 | callback->Run(result); |
| 1012 | } |
| 1013 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 1014 | ClientSocketPoolBaseHelper::Group::Group() |
| 1015 | : active_socket_count_(0), |
| 1016 | ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) {} |
| 1017 | |
| 1018 | ClientSocketPoolBaseHelper::Group::~Group() { |
| 1019 | CleanupBackupJob(); |
| 1020 | } |
| 1021 | |
| 1022 | void ClientSocketPoolBaseHelper::Group::StartBackupSocketTimer( |
| 1023 | const std::string& group_name, |
| 1024 | ClientSocketPoolBaseHelper* pool) { |
| 1025 | // Only allow one timer pending to create a backup socket. |
| 1026 | if (!method_factory_.empty()) |
| 1027 | return; |
| 1028 | |
| 1029 | MessageLoop::current()->PostDelayedTask( |
| 1030 | FROM_HERE, |
| 1031 | method_factory_.NewRunnableMethod( |
| 1032 | &Group::OnBackupSocketTimerFired, group_name, pool), |
| 1033 | pool->ConnectRetryIntervalMs()); |
| 1034 | } |
| 1035 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 1036 | bool ClientSocketPoolBaseHelper::Group::TryToUsePreconnectConnectJob() { |
| 1037 | for (std::set<ConnectJob*>::iterator it = jobs_.begin(); |
| 1038 | it != jobs_.end(); ++it) { |
| 1039 | ConnectJob* job = *it; |
| 1040 | if (job->is_unused_preconnect()) { |
| 1041 | job->UseForNormalRequest(); |
| 1042 | return true; |
| 1043 | } |
| 1044 | } |
| 1045 | return false; |
| 1046 | } |
| 1047 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 1048 | void ClientSocketPoolBaseHelper::Group::OnBackupSocketTimerFired( |
| 1049 | std::string group_name, |
| 1050 | ClientSocketPoolBaseHelper* pool) { |
| 1051 | // If there are no more jobs pending, there is no work to do. |
| 1052 | // If we've done our cleanups correctly, this should not happen. |
| 1053 | if (jobs_.empty()) { |
| 1054 | NOTREACHED(); |
| 1055 | return; |
| 1056 | } |
| 1057 | |
| 1058 | // If our backup job is waiting on DNS, or if we can't create any sockets |
| 1059 | // right now due to limits, just reset the timer. |
| 1060 | if (pool->ReachedMaxSocketsLimit() || |
| 1061 | !HasAvailableSocketSlot(pool->max_sockets_per_group_) || |
| 1062 | (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) { |
| 1063 | StartBackupSocketTimer(group_name, pool); |
| 1064 | return; |
| 1065 | } |
| 1066 | |
[email protected] | 4baaf9d | 2010-08-31 15:15:44 | [diff] [blame] | 1067 | if (pending_requests_.empty()) { |
| 1068 | LOG(DFATAL) << "No pending request for backup job."; |
| 1069 | return; |
| 1070 | } |
| 1071 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 1072 | ConnectJob* backup_job = pool->connect_job_factory_->NewConnectJob( |
| 1073 | group_name, **pending_requests_.begin(), pool); |
| 1074 | backup_job->net_log().AddEvent(NetLog::TYPE_SOCKET_BACKUP_CREATED, NULL); |
| 1075 | SIMPLE_STATS_COUNTER("socket.backup_created"); |
| 1076 | int rv = backup_job->Connect(); |
| 1077 | pool->connecting_socket_count_++; |
| 1078 | AddJob(backup_job); |
| 1079 | if (rv != ERR_IO_PENDING) |
| 1080 | pool->OnConnectJobComplete(rv, backup_job); |
| 1081 | } |
| 1082 | |
| 1083 | void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() { |
| 1084 | // Delete active jobs. |
| 1085 | STLDeleteElements(&jobs_); |
| 1086 | |
| 1087 | // Cancel pending backup job. |
| 1088 | method_factory_.RevokeAll(); |
| 1089 | } |
| 1090 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 1091 | } // namespace internal |
| 1092 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 1093 | } // namespace net |