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