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