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