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