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