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