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