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