[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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" |
| 8 | #include "base/message_loop.h" |
| 9 | #include "base/stl_util-inl.h" |
| 10 | #include "base/time.h" |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 11 | #include "net/base/load_log.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 12 | #include "net/base/net_errors.h" |
| 13 | #include "net/socket/client_socket_handle.h" |
| 14 | |
| 15 | using base::TimeDelta; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | // The timeout value, in seconds, used to clean up idle sockets that can't be |
| 20 | // reused. |
| 21 | // |
| 22 | // Note: It's important to close idle sockets that have received data as soon |
| 23 | // as possible because the received data may cause BSOD on Windows XP under |
| 24 | // some conditions. See http://crbug.com/4606. |
| 25 | const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. |
| 26 | |
[email protected] | 60c4c41 | 2009-11-06 19:59:36 | [diff] [blame] | 27 | // The maximum size of the ConnectJob's LoadLog. |
| 28 | const int kMaxNumLoadLogEntries = 50; |
| 29 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 30 | } // namespace |
| 31 | |
| 32 | namespace net { |
| 33 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 34 | ConnectJob::ConnectJob(const std::string& group_name, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 35 | base::TimeDelta timeout_duration, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 36 | Delegate* delegate, |
| 37 | LoadLog* load_log) |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 38 | : group_name_(group_name), |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 39 | timeout_duration_(timeout_duration), |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 40 | delegate_(delegate), |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 41 | load_log_(load_log) { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 42 | DCHECK(!group_name.empty()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 43 | DCHECK(delegate); |
| 44 | } |
| 45 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 46 | ConnectJob::~ConnectJob() { |
| 47 | if (delegate_) { |
| 48 | // If the delegate was not NULLed, then NotifyDelegateOfCompletion has |
| 49 | // not been called yet (hence we are cancelling). |
| 50 | LoadLog::AddEvent(load_log_, LoadLog::TYPE_CANCELLED); |
| 51 | LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB); |
| 52 | } |
| 53 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 54 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 55 | int ConnectJob::Connect() { |
| 56 | if (timeout_duration_ != base::TimeDelta()) |
| 57 | timer_.Start(timeout_duration_, this, &ConnectJob::OnTimeout); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 58 | |
| 59 | LoadLog::BeginEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB); |
| 60 | |
| 61 | int rv = ConnectInternal(); |
| 62 | |
| 63 | if (rv != ERR_IO_PENDING) { |
| 64 | delegate_ = NULL; |
| 65 | LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB); |
| 66 | } |
| 67 | |
| 68 | return rv; |
| 69 | } |
| 70 | |
| 71 | void ConnectJob::NotifyDelegateOfCompletion(int rv) { |
| 72 | // The delegate will delete |this|. |
| 73 | Delegate *delegate = delegate_; |
| 74 | delegate_ = NULL; |
| 75 | |
| 76 | LoadLog::EndEvent(load_log_, LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB); |
| 77 | |
| 78 | delegate->OnConnectJobComplete(rv, this); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void ConnectJob::OnTimeout() { |
[email protected] | 6e713f0 | 2009-08-06 02:56:40 | [diff] [blame] | 82 | // Make sure the socket is NULL before calling into |delegate|. |
| 83 | set_socket(NULL); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 84 | |
| 85 | LoadLog::AddEvent(load_log_, |
| 86 | LoadLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT); |
| 87 | |
| 88 | NotifyDelegateOfCompletion(ERR_TIMED_OUT); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 91 | namespace internal { |
| 92 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 93 | ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 94 | int max_sockets, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 95 | int max_sockets_per_group, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 96 | base::TimeDelta unused_idle_socket_timeout, |
| 97 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 98 | ConnectJobFactory* connect_job_factory, |
| 99 | const scoped_refptr<NetworkChangeNotifier>& network_change_notifier) |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 100 | : idle_socket_count_(0), |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 101 | connecting_socket_count_(0), |
| 102 | handed_out_socket_count_(0), |
| 103 | max_sockets_(max_sockets), |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 104 | max_sockets_per_group_(max_sockets_per_group), |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 105 | unused_idle_socket_timeout_(unused_idle_socket_timeout), |
| 106 | used_idle_socket_timeout_(used_idle_socket_timeout), |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 107 | may_have_stalled_group_(false), |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 108 | connect_job_factory_(connect_job_factory), |
| 109 | network_change_notifier_(network_change_notifier) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 110 | DCHECK_LE(0, max_sockets_per_group); |
| 111 | DCHECK_LE(max_sockets_per_group, max_sockets); |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 112 | |
| 113 | if (network_change_notifier_) |
| 114 | network_change_notifier_->AddObserver(this); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 115 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 116 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 117 | ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 118 | CancelAllConnectJobs(); |
| 119 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 120 | // Clean up any idle sockets. Assert that we have no remaining active |
| 121 | // sockets or pending requests. They should have all been cleaned up prior |
| 122 | // to the manager being destroyed. |
| 123 | CloseIdleSockets(); |
| 124 | DCHECK(group_map_.empty()); |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 125 | DCHECK_EQ(0, connecting_socket_count_); |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 126 | |
| 127 | if (network_change_notifier_) |
| 128 | network_change_notifier_->RemoveObserver(this); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | // InsertRequestIntoQueue inserts the request into the queue based on |
| 132 | // priority. Highest priorities are closest to the front. Older requests are |
| 133 | // prioritized over requests of equal priority. |
| 134 | // |
| 135 | // static |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 136 | void ClientSocketPoolBaseHelper::InsertRequestIntoQueue( |
| 137 | const Request* r, RequestQueue* pending_requests) { |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 138 | LoadLog::BeginEvent(r->load_log(), |
| 139 | LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE); |
| 140 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 141 | RequestQueue::iterator it = pending_requests->begin(); |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 142 | while (it != pending_requests->end() && r->priority() >= (*it)->priority()) |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 143 | ++it; |
| 144 | pending_requests->insert(it, r); |
| 145 | } |
| 146 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 147 | // static |
| 148 | const ClientSocketPoolBaseHelper::Request* |
| 149 | ClientSocketPoolBaseHelper::RemoveRequestFromQueue( |
| 150 | RequestQueue::iterator it, RequestQueue* pending_requests) { |
| 151 | const Request* req = *it; |
| 152 | |
| 153 | LoadLog::EndEvent(req->load_log(), |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 154 | LoadLog::TYPE_SOCKET_POOL_WAITING_IN_QUEUE); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 155 | |
| 156 | pending_requests->erase(it); |
| 157 | return req; |
| 158 | } |
| 159 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 160 | int ClientSocketPoolBaseHelper::RequestSocket( |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 161 | const std::string& group_name, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 162 | const Request* request) { |
| 163 | DCHECK_GE(request->priority(), 0); |
| 164 | CompletionCallback* const callback = request->callback(); |
| 165 | CHECK(callback); |
| 166 | ClientSocketHandle* const handle = request->handle(); |
| 167 | CHECK(handle); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 168 | Group& group = group_map_[group_name]; |
| 169 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 170 | // Can we make another active socket now? |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 171 | if (ReachedMaxSocketsLimit() || |
| 172 | !group.HasAvailableSocketSlot(max_sockets_per_group_)) { |
| 173 | if (ReachedMaxSocketsLimit()) { |
| 174 | // We could check if we really have a stalled group here, but it requires |
| 175 | // a scan of all groups, so just flip a flag here, and do the check later. |
| 176 | may_have_stalled_group_ = true; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 177 | |
| 178 | LoadLog::AddEvent(request->load_log(), |
| 179 | LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS); |
| 180 | } else { |
| 181 | LoadLog::AddEvent(request->load_log(), |
| 182 | LoadLog::TYPE_SOCKET_POOL_STALLED_MAX_SOCKETS_PER_GROUP); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 183 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 184 | InsertRequestIntoQueue(request, &group.pending_requests); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 185 | return ERR_IO_PENDING; |
| 186 | } |
| 187 | |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 188 | // Try to reuse a socket. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 189 | while (!group.idle_sockets.empty()) { |
| 190 | IdleSocket idle_socket = group.idle_sockets.back(); |
| 191 | group.idle_sockets.pop_back(); |
| 192 | DecrementIdleCount(); |
| 193 | if (idle_socket.socket->IsConnectedAndIdle()) { |
| 194 | // We found one we can reuse! |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 195 | base::TimeDelta idle_time = |
| 196 | base::TimeTicks::Now() - idle_socket.start_time; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 197 | HandOutSocket( |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 198 | idle_socket.socket, idle_socket.used, handle, idle_time, &group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 199 | return OK; |
| 200 | } |
| 201 | delete idle_socket.socket; |
| 202 | } |
| 203 | |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 204 | // See if we already have enough connect jobs or sockets that will be released |
| 205 | // soon. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 206 | if (group.HasReleasingSockets()) { |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 207 | InsertRequestIntoQueue(request, &group.pending_requests); |
| 208 | return ERR_IO_PENDING; |
| 209 | } |
| 210 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 211 | // We couldn't find a socket to reuse, so allocate and connect a new one. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 212 | scoped_refptr<LoadLog> job_load_log = new LoadLog(kMaxNumLoadLogEntries); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 213 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 214 | scoped_ptr<ConnectJob> connect_job( |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 215 | connect_job_factory_->NewConnectJob(group_name, *request, this, |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 216 | job_load_log)); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 217 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 218 | int rv = connect_job->Connect(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 219 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 220 | if (rv != ERR_IO_PENDING && request->load_log()) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 221 | request->load_log()->Append(job_load_log); |
| 222 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 223 | if (rv == OK) { |
| 224 | HandOutSocket(connect_job->ReleaseSocket(), false /* not reused */, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 225 | handle, base::TimeDelta(), &group); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 226 | } else if (rv == ERR_IO_PENDING) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 227 | connecting_socket_count_++; |
| 228 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 229 | ConnectJob* job = connect_job.release(); |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 230 | InsertRequestIntoQueue(request, &group.pending_requests); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 231 | group.jobs.insert(job); |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 232 | } else if (group.IsEmpty()) { |
| 233 | group_map_.erase(group_name); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 234 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 235 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 236 | return rv; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 239 | void ClientSocketPoolBaseHelper::CancelRequest( |
| 240 | const std::string& group_name, const ClientSocketHandle* handle) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 241 | CHECK(ContainsKey(group_map_, group_name)); |
| 242 | |
| 243 | Group& group = group_map_[group_name]; |
| 244 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 245 | // Search pending_requests for matching handle. |
| 246 | RequestQueue::iterator it = group.pending_requests.begin(); |
| 247 | for (; it != group.pending_requests.end(); ++it) { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 248 | if ((*it)->handle() == handle) { |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 249 | const Request* req = RemoveRequestFromQueue(it, &group.pending_requests); |
| 250 | LoadLog::AddEvent(req->load_log(), LoadLog::TYPE_CANCELLED); |
| 251 | LoadLog::EndEvent(req->load_log(), LoadLog::TYPE_SOCKET_POOL); |
| 252 | delete req; |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 253 | if (group.jobs.size() > group.pending_requests.size() + 1) { |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 254 | // TODO(willchan): Cancel the job in the earliest LoadState. |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 255 | RemoveConnectJob(*group.jobs.begin(), &group); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 256 | OnAvailableSocketSlot(group_name, &group); |
| 257 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 258 | return; |
| 259 | } |
| 260 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 263 | void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, |
| 264 | ClientSocket* socket) { |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 265 | Group& group = group_map_[group_name]; |
| 266 | group.num_releasing_sockets++; |
| 267 | DCHECK_LE(group.num_releasing_sockets, group.active_socket_count); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 268 | // Run this asynchronously to allow the caller to finish before we let |
| 269 | // another to begin doing work. This also avoids nasty recursion issues. |
| 270 | // NOTE: We cannot refer to the handle argument after this method returns. |
| 271 | MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 272 | this, &ClientSocketPoolBaseHelper::DoReleaseSocket, group_name, socket)); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 273 | } |
| 274 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 275 | void ClientSocketPoolBaseHelper::CloseIdleSockets() { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 276 | CleanupIdleSockets(true); |
| 277 | } |
| 278 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 279 | int ClientSocketPoolBaseHelper::IdleSocketCountInGroup( |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 280 | const std::string& group_name) const { |
| 281 | GroupMap::const_iterator i = group_map_.find(group_name); |
| 282 | CHECK(i != group_map_.end()); |
| 283 | |
| 284 | return i->second.idle_sockets.size(); |
| 285 | } |
| 286 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 287 | LoadState ClientSocketPoolBaseHelper::GetLoadState( |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 288 | const std::string& group_name, |
| 289 | const ClientSocketHandle* handle) const { |
| 290 | if (!ContainsKey(group_map_, group_name)) { |
| 291 | NOTREACHED() << "ClientSocketPool does not contain group: " << group_name |
| 292 | << " for handle: " << handle; |
| 293 | return LOAD_STATE_IDLE; |
| 294 | } |
| 295 | |
| 296 | // Can't use operator[] since it is non-const. |
| 297 | const Group& group = group_map_.find(group_name)->second; |
| 298 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 299 | // Search pending_requests for matching handle. |
| 300 | RequestQueue::const_iterator it = group.pending_requests.begin(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 301 | for (size_t i = 0; it != group.pending_requests.end(); ++it, ++i) { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 302 | if ((*it)->handle() == handle) { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 303 | if (i < group.jobs.size()) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 304 | LoadState max_state = LOAD_STATE_IDLE; |
| 305 | for (ConnectJobSet::const_iterator job_it = group.jobs.begin(); |
| 306 | job_it != group.jobs.end(); ++job_it) { |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 307 | max_state = std::max(max_state, (*job_it)->GetLoadState()); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 308 | } |
| 309 | return max_state; |
| 310 | } else { |
| 311 | // TODO(wtc): Add a state for being on the wait list. |
| 312 | // See http://www.crbug.com/5077. |
| 313 | return LOAD_STATE_IDLE; |
| 314 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
| 318 | NOTREACHED(); |
| 319 | return LOAD_STATE_IDLE; |
| 320 | } |
| 321 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 322 | bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup( |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 323 | base::TimeTicks now, |
| 324 | base::TimeDelta timeout) const { |
| 325 | bool timed_out = (now - start_time) >= timeout; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 326 | return timed_out || |
| 327 | !(used ? socket->IsConnectedAndIdle() : socket->IsConnected()); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 328 | } |
| 329 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 330 | void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 331 | if (idle_socket_count_ == 0) |
| 332 | return; |
| 333 | |
| 334 | // Current time value. Retrieving it once at the function start rather than |
| 335 | // inside the inner loop, since it shouldn't change by any meaningful amount. |
| 336 | base::TimeTicks now = base::TimeTicks::Now(); |
| 337 | |
| 338 | GroupMap::iterator i = group_map_.begin(); |
| 339 | while (i != group_map_.end()) { |
| 340 | Group& group = i->second; |
| 341 | |
| 342 | std::deque<IdleSocket>::iterator j = group.idle_sockets.begin(); |
| 343 | while (j != group.idle_sockets.end()) { |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 344 | base::TimeDelta timeout = |
| 345 | j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_; |
| 346 | if (force || j->ShouldCleanup(now, timeout)) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 347 | delete j->socket; |
| 348 | j = group.idle_sockets.erase(j); |
| 349 | DecrementIdleCount(); |
| 350 | } else { |
| 351 | ++j; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // Delete group if no longer needed. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 356 | if (group.IsEmpty()) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 357 | group_map_.erase(i++); |
| 358 | } else { |
| 359 | ++i; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 364 | void ClientSocketPoolBaseHelper::IncrementIdleCount() { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 365 | if (++idle_socket_count_ == 1) |
| 366 | timer_.Start(TimeDelta::FromSeconds(kCleanupInterval), this, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 367 | &ClientSocketPoolBaseHelper::OnCleanupTimerFired); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 370 | void ClientSocketPoolBaseHelper::DecrementIdleCount() { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 371 | if (--idle_socket_count_ == 0) |
| 372 | timer_.Stop(); |
| 373 | } |
| 374 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 375 | void ClientSocketPoolBaseHelper::DoReleaseSocket(const std::string& group_name, |
| 376 | ClientSocket* socket) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 377 | GroupMap::iterator i = group_map_.find(group_name); |
| 378 | CHECK(i != group_map_.end()); |
| 379 | |
| 380 | Group& group = i->second; |
| 381 | |
[email protected] | 5edbf8d | 2010-01-13 18:44:11 | [diff] [blame] | 382 | group.num_releasing_sockets--; |
| 383 | DCHECK_GE(group.num_releasing_sockets, 0); |
| 384 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 385 | CHECK(handed_out_socket_count_ > 0); |
| 386 | handed_out_socket_count_--; |
| 387 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 388 | CHECK(group.active_socket_count > 0); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 389 | group.active_socket_count--; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 390 | |
| 391 | const bool can_reuse = socket->IsConnectedAndIdle(); |
| 392 | if (can_reuse) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 393 | AddIdleSocket(socket, true /* used socket */, &group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 394 | } else { |
| 395 | delete socket; |
| 396 | } |
| 397 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 398 | OnAvailableSocketSlot(group_name, &group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 401 | // Search for the highest priority pending request, amongst the groups that |
| 402 | // are not at the |max_sockets_per_group_| limit. Note: for requests with |
| 403 | // the same priority, the winner is based on group hash ordering (and not |
| 404 | // insertion order). |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 405 | int ClientSocketPoolBaseHelper::FindTopStalledGroup(Group** group, |
| 406 | std::string* group_name) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 407 | Group* top_group = NULL; |
| 408 | const std::string* top_group_name = NULL; |
| 409 | int stalled_group_count = 0; |
| 410 | for (GroupMap::iterator i = group_map_.begin(); |
| 411 | i != group_map_.end(); ++i) { |
| 412 | Group& group = i->second; |
| 413 | const RequestQueue& queue = group.pending_requests; |
| 414 | if (queue.empty()) |
| 415 | continue; |
| 416 | bool has_slot = group.HasAvailableSocketSlot(max_sockets_per_group_); |
| 417 | if (has_slot) |
| 418 | stalled_group_count++; |
| 419 | bool has_higher_priority = !top_group || |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 420 | group.TopPendingPriority() < top_group->TopPendingPriority(); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 421 | if (has_slot && has_higher_priority) { |
| 422 | top_group = &group; |
| 423 | top_group_name = &i->first; |
| 424 | } |
| 425 | } |
| 426 | if (top_group) { |
| 427 | *group = top_group; |
| 428 | *group_name = *top_group_name; |
| 429 | } |
| 430 | return stalled_group_count; |
| 431 | } |
| 432 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 433 | void ClientSocketPoolBaseHelper::OnConnectJobComplete( |
| 434 | int result, ConnectJob* job) { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 435 | DCHECK_NE(ERR_IO_PENDING, result); |
| 436 | const std::string group_name = job->group_name(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 437 | GroupMap::iterator group_it = group_map_.find(group_name); |
| 438 | CHECK(group_it != group_map_.end()); |
| 439 | Group& group = group_it->second; |
| 440 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 441 | scoped_ptr<ClientSocket> socket(job->ReleaseSocket()); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 442 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 443 | scoped_refptr<LoadLog> job_load_log(job->load_log()); |
| 444 | RemoveConnectJob(job, &group); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 445 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 446 | LoadLog::EndEvent(job_load_log, LoadLog::TYPE_SOCKET_POOL); |
| 447 | |
| 448 | if (result == OK) { |
| 449 | DCHECK(socket.get()); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 450 | if (!group.pending_requests.empty()) { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 451 | scoped_ptr<const Request> r(RemoveRequestFromQueue( |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 452 | group.pending_requests.begin(), &group.pending_requests)); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 453 | if (r->load_log()) |
| 454 | r->load_log()->Append(job_load_log); |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 455 | HandOutSocket( |
| 456 | socket.release(), false /* unused socket */, r->handle(), |
| 457 | base::TimeDelta(), &group); |
| 458 | r->callback()->Run(result); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 459 | } else { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 460 | AddIdleSocket(socket.release(), false /* unused socket */, &group); |
| 461 | OnAvailableSocketSlot(group_name, &group); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 462 | } |
[email protected] | 94c2047 | 2010-01-14 08:14:36 | [diff] [blame] | 463 | } else { |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 464 | DCHECK(!socket.get()); |
| 465 | if (!group.pending_requests.empty()) { |
| 466 | scoped_ptr<const Request> r(RemoveRequestFromQueue( |
| 467 | group.pending_requests.begin(), &group.pending_requests)); |
| 468 | if (r->load_log()) |
| 469 | r->load_log()->Append(job_load_log); |
| 470 | r->callback()->Run(result); |
| 471 | } |
| 472 | MaybeOnAvailableSocketSlot(group_name); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 473 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 476 | void ClientSocketPoolBaseHelper::OnIPAddressChanged() { |
| 477 | CloseIdleSockets(); |
| 478 | } |
| 479 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 480 | void ClientSocketPoolBaseHelper::RemoveConnectJob(const ConnectJob *job, |
| 481 | Group* group) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 482 | CHECK(connecting_socket_count_ > 0); |
| 483 | connecting_socket_count_--; |
| 484 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 485 | DCHECK(job); |
| 486 | delete job; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 487 | |
| 488 | if (group) { |
| 489 | DCHECK(ContainsKey(group->jobs, job)); |
| 490 | group->jobs.erase(job); |
| 491 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 492 | } |
| 493 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 494 | void ClientSocketPoolBaseHelper::MaybeOnAvailableSocketSlot( |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 495 | const std::string& group_name) { |
| 496 | GroupMap::iterator it = group_map_.find(group_name); |
| 497 | if (it != group_map_.end()) { |
| 498 | Group& group = it->second; |
| 499 | if (group.HasAvailableSocketSlot(max_sockets_per_group_)) |
| 500 | OnAvailableSocketSlot(group_name, &group); |
| 501 | } |
| 502 | } |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 503 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 504 | void ClientSocketPoolBaseHelper::OnAvailableSocketSlot( |
| 505 | const std::string& group_name, Group* group) { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 506 | if (may_have_stalled_group_) { |
| 507 | std::string top_group_name; |
[email protected] | bed37d44 | 2009-08-20 19:58:20 | [diff] [blame] | 508 | Group* top_group = NULL; |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 509 | int stalled_group_count = FindTopStalledGroup(&top_group, &top_group_name); |
| 510 | if (stalled_group_count <= 1) |
| 511 | may_have_stalled_group_ = false; |
| 512 | if (stalled_group_count >= 1) |
| 513 | ProcessPendingRequest(top_group_name, top_group); |
| 514 | } else if (!group->pending_requests.empty()) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 515 | ProcessPendingRequest(group_name, group); |
| 516 | // |group| may no longer be valid after this point. Be careful not to |
| 517 | // access it again. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 518 | } else if (group->IsEmpty()) { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 519 | // Delete |group| if no longer needed. |group| will no longer be valid. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 520 | group_map_.erase(group_name); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 524 | void ClientSocketPoolBaseHelper::ProcessPendingRequest( |
| 525 | const std::string& group_name, Group* group) { |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 526 | scoped_ptr<const Request> r(RemoveRequestFromQueue( |
| 527 | group->pending_requests.begin(), &group->pending_requests)); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 528 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 529 | int rv = RequestSocket(group_name, r.get()); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 530 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 531 | if (rv != ERR_IO_PENDING) { |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 532 | LoadLog::EndEvent(r->load_log(), LoadLog::TYPE_SOCKET_POOL); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 533 | r->callback()->Run(rv); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 534 | if (rv != OK) { |
| 535 | // |group| may be invalid after the callback, we need to search |
| 536 | // |group_map_| again. |
| 537 | MaybeOnAvailableSocketSlot(group_name); |
| 538 | } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 539 | } else { |
| 540 | r.release(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 544 | void ClientSocketPoolBaseHelper::HandOutSocket( |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 545 | ClientSocket* socket, |
| 546 | bool reused, |
| 547 | ClientSocketHandle* handle, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 548 | base::TimeDelta idle_time, |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 549 | Group* group) { |
| 550 | DCHECK(socket); |
| 551 | handle->set_socket(socket); |
| 552 | handle->set_is_reused(reused); |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 553 | handle->set_idle_time(idle_time); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 554 | |
| 555 | handed_out_socket_count_++; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 556 | group->active_socket_count++; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 557 | } |
| 558 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 559 | void ClientSocketPoolBaseHelper::AddIdleSocket( |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 560 | ClientSocket* socket, bool used, Group* group) { |
| 561 | DCHECK(socket); |
| 562 | IdleSocket idle_socket; |
| 563 | idle_socket.socket = socket; |
| 564 | idle_socket.start_time = base::TimeTicks::Now(); |
| 565 | idle_socket.used = used; |
| 566 | |
| 567 | group->idle_sockets.push_back(idle_socket); |
| 568 | IncrementIdleCount(); |
| 569 | } |
| 570 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 571 | void ClientSocketPoolBaseHelper::CancelAllConnectJobs() { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 572 | for (GroupMap::iterator i = group_map_.begin(); i != group_map_.end();) { |
| 573 | Group& group = i->second; |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame^] | 574 | connecting_socket_count_ -= group.jobs.size(); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 575 | STLDeleteElements(&group.jobs); |
| 576 | |
| 577 | // Delete group if no longer needed. |
| 578 | if (group.IsEmpty()) { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 579 | group_map_.erase(i++); |
| 580 | } else { |
| 581 | ++i; |
| 582 | } |
| 583 | } |
| 584 | } |
| 585 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 586 | bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const { |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 587 | // Each connecting socket will eventually connect and be handed out. |
| 588 | int total = handed_out_socket_count_ + connecting_socket_count_; |
| 589 | DCHECK_LE(total, max_sockets_); |
| 590 | return total == max_sockets_; |
| 591 | } |
| 592 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 593 | } // namespace internal |
| 594 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 595 | } // namespace net |