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