OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 // Make sure the socket is NULL before calling into |delegate|. | 120 // Make sure the socket is NULL before calling into |delegate|. |
121 SetSocket(scoped_ptr<StreamSocket>()); | 121 SetSocket(scoped_ptr<StreamSocket>()); |
122 | 122 |
123 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT); | 123 net_log_.AddEvent(NetLog::TYPE_SOCKET_POOL_CONNECT_JOB_TIMED_OUT); |
124 | 124 |
125 NotifyDelegateOfCompletion(ERR_TIMED_OUT); | 125 NotifyDelegateOfCompletion(ERR_TIMED_OUT); |
126 } | 126 } |
127 | 127 |
128 namespace internal { | 128 namespace internal { |
129 | 129 |
130 ClientSocketPoolBaseHelper::Request::Request( | 130 ClientSocketPoolBaseHelper::Request::Request(ClientSocketHandle* handle, |
131 ClientSocketHandle* handle, | 131 const CompletionCallback& callback, |
132 const CompletionCallback& callback, | 132 RequestPriority priority, |
133 RequestPriority priority, | 133 bool ignore_limits, |
134 bool ignore_limits, | 134 Flags flags, |
135 Flags flags, | 135 const BoundNetLog& net_log) |
136 const BoundNetLog& net_log) | |
137 : handle_(handle), | 136 : handle_(handle), |
138 callback_(callback), | 137 callback_(callback), |
139 priority_(priority), | 138 priority_(priority), |
140 ignore_limits_(ignore_limits), | 139 ignore_limits_(ignore_limits), |
141 flags_(flags), | 140 flags_(flags), |
142 net_log_(net_log) { | 141 net_log_(net_log) { |
143 if (ignore_limits_) | 142 if (ignore_limits_) |
144 DCHECK_EQ(priority_, MAXIMUM_PRIORITY); | 143 DCHECK_EQ(priority_, MAXIMUM_PRIORITY); |
145 } | 144 } |
146 | 145 |
147 ClientSocketPoolBaseHelper::Request::~Request() {} | 146 ClientSocketPoolBaseHelper::Request::~Request() { |
| 147 } |
148 | 148 |
149 ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( | 149 ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( |
150 HigherLayeredPool* pool, | 150 HigherLayeredPool* pool, |
151 int max_sockets, | 151 int max_sockets, |
152 int max_sockets_per_group, | 152 int max_sockets_per_group, |
153 base::TimeDelta unused_idle_socket_timeout, | 153 base::TimeDelta unused_idle_socket_timeout, |
154 base::TimeDelta used_idle_socket_timeout, | 154 base::TimeDelta used_idle_socket_timeout, |
155 ConnectJobFactory* connect_job_factory) | 155 ConnectJobFactory* connect_job_factory) |
156 : idle_socket_count_(0), | 156 : idle_socket_count_(0), |
157 connecting_socket_count_(0), | 157 connecting_socket_count_(0), |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 ++it) { | 190 ++it) { |
191 (*it)->RemoveHigherLayeredPool(pool_); | 191 (*it)->RemoveHigherLayeredPool(pool_); |
192 } | 192 } |
193 } | 193 } |
194 | 194 |
195 ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair() | 195 ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair() |
196 : result(OK) { | 196 : result(OK) { |
197 } | 197 } |
198 | 198 |
199 ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair( | 199 ClientSocketPoolBaseHelper::CallbackResultPair::CallbackResultPair( |
200 const CompletionCallback& callback_in, int result_in) | 200 const CompletionCallback& callback_in, |
201 : callback(callback_in), | 201 int result_in) |
202 result(result_in) { | 202 : callback(callback_in), result(result_in) { |
203 } | 203 } |
204 | 204 |
205 ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() {} | 205 ClientSocketPoolBaseHelper::CallbackResultPair::~CallbackResultPair() { |
| 206 } |
206 | 207 |
207 bool ClientSocketPoolBaseHelper::IsStalled() const { | 208 bool ClientSocketPoolBaseHelper::IsStalled() const { |
208 // If a lower layer pool is stalled, consider |this| stalled as well. | 209 // If a lower layer pool is stalled, consider |this| stalled as well. |
209 for (std::set<LowerLayeredPool*>::const_iterator it = lower_pools_.begin(); | 210 for (std::set<LowerLayeredPool*>::const_iterator it = lower_pools_.begin(); |
210 it != lower_pools_.end(); | 211 it != lower_pools_.end(); |
211 ++it) { | 212 ++it) { |
212 if ((*it)->IsStalled()) | 213 if ((*it)->IsStalled()) |
213 return true; | 214 return true; |
214 } | 215 } |
215 | 216 |
216 // If fewer than |max_sockets_| are in use, then clearly |this| is not | 217 // If fewer than |max_sockets_| are in use, then clearly |this| is not |
217 // stalled. | 218 // stalled. |
218 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_) | 219 if ((handed_out_socket_count_ + connecting_socket_count_) < max_sockets_) |
219 return false; | 220 return false; |
220 // So in order to be stalled, |this| must be using at least |max_sockets_| AND | 221 // So in order to be stalled, |this| must be using at least |max_sockets_| AND |
221 // |this| must have a request that is actually stalled on the global socket | 222 // |this| must have a request that is actually stalled on the global socket |
222 // limit. To find such a request, look for a group that has more requests | 223 // limit. To find such a request, look for a group that has more requests |
223 // than jobs AND where the number of sockets is less than | 224 // than jobs AND where the number of sockets is less than |
224 // |max_sockets_per_group_|. (If the number of sockets is equal to | 225 // |max_sockets_per_group_|. (If the number of sockets is equal to |
225 // |max_sockets_per_group_|, then the request is stalled on the group limit, | 226 // |max_sockets_per_group_|, then the request is stalled on the group limit, |
226 // which does not count.) | 227 // which does not count.) |
227 for (GroupMap::const_iterator it = group_map_.begin(); | 228 for (GroupMap::const_iterator it = group_map_.begin(); it != group_map_.end(); |
228 it != group_map_.end(); ++it) { | 229 ++it) { |
229 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) | 230 if (it->second->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) |
230 return true; | 231 return true; |
231 } | 232 } |
232 return false; | 233 return false; |
233 } | 234 } |
234 | 235 |
235 void ClientSocketPoolBaseHelper::AddLowerLayeredPool( | 236 void ClientSocketPoolBaseHelper::AddLowerLayeredPool( |
236 LowerLayeredPool* lower_pool) { | 237 LowerLayeredPool* lower_pool) { |
237 DCHECK(pool_); | 238 DCHECK(pool_); |
238 CHECK(!ContainsKey(lower_pools_, lower_pool)); | 239 CHECK(!ContainsKey(lower_pools_, lower_pool)); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 base::MessageLoop::current()->PostTask( | 283 base::MessageLoop::current()->PostTask( |
283 FROM_HERE, | 284 FROM_HERE, |
284 base::Bind( | 285 base::Bind( |
285 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools, | 286 &ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools, |
286 weak_factory_.GetWeakPtr())); | 287 weak_factory_.GetWeakPtr())); |
287 } | 288 } |
288 } | 289 } |
289 return rv; | 290 return rv; |
290 } | 291 } |
291 | 292 |
292 void ClientSocketPoolBaseHelper::RequestSockets( | 293 void ClientSocketPoolBaseHelper::RequestSockets(const std::string& group_name, |
293 const std::string& group_name, | 294 const Request& request, |
294 const Request& request, | 295 int num_sockets) { |
295 int num_sockets) { | |
296 DCHECK(request.callback().is_null()); | 296 DCHECK(request.callback().is_null()); |
297 DCHECK(!request.handle()); | 297 DCHECK(!request.handle()); |
298 | 298 |
299 // Cleanup any timed out idle sockets if no timer is used. | 299 // Cleanup any timed out idle sockets if no timer is used. |
300 if (!use_cleanup_timer_) | 300 if (!use_cleanup_timer_) |
301 CleanupIdleSockets(false); | 301 CleanupIdleSockets(false); |
302 | 302 |
303 if (num_sockets > max_sockets_per_group_) { | 303 if (num_sockets > max_sockets_per_group_) { |
304 num_sockets = max_sockets_per_group_; | 304 num_sockets = max_sockets_per_group_; |
305 } | 305 } |
306 | 306 |
307 request.net_log().BeginEvent( | 307 request.net_log().BeginEvent( |
308 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, | 308 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, |
309 NetLog::IntegerCallback("num_sockets", num_sockets)); | 309 NetLog::IntegerCallback("num_sockets", num_sockets)); |
310 | 310 |
311 Group* group = GetOrCreateGroup(group_name); | 311 Group* group = GetOrCreateGroup(group_name); |
312 | 312 |
313 // RequestSocketsInternal() may delete the group. | 313 // RequestSocketsInternal() may delete the group. |
314 bool deleted_group = false; | 314 bool deleted_group = false; |
315 | 315 |
316 int rv = OK; | 316 int rv = OK; |
317 for (int num_iterations_left = num_sockets; | 317 for (int num_iterations_left = num_sockets; |
318 group->NumActiveSocketSlots() < num_sockets && | 318 group->NumActiveSocketSlots() < num_sockets && num_iterations_left > 0; |
319 num_iterations_left > 0 ; num_iterations_left--) { | 319 num_iterations_left--) { |
320 rv = RequestSocketInternal(group_name, request); | 320 rv = RequestSocketInternal(group_name, request); |
321 if (rv < 0 && rv != ERR_IO_PENDING) { | 321 if (rv < 0 && rv != ERR_IO_PENDING) { |
322 // We're encountering a synchronous error. Give up. | 322 // We're encountering a synchronous error. Give up. |
323 if (!ContainsKey(group_map_, group_name)) | 323 if (!ContainsKey(group_map_, group_name)) |
324 deleted_group = true; | 324 deleted_group = true; |
325 break; | 325 break; |
326 } | 326 } |
327 if (!ContainsKey(group_map_, group_name)) { | 327 if (!ContainsKey(group_map_, group_name)) { |
328 // Unexpected. The group should only be getting deleted on synchronous | 328 // Unexpected. The group should only be getting deleted on synchronous |
329 // error. | 329 // error. |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 | 393 |
394 // We couldn't find a socket to reuse, and there's space to allocate one, | 394 // We couldn't find a socket to reuse, and there's space to allocate one, |
395 // so allocate and connect a new one. | 395 // so allocate and connect a new one. |
396 scoped_ptr<ConnectJob> connect_job( | 396 scoped_ptr<ConnectJob> connect_job( |
397 connect_job_factory_->NewConnectJob(group_name, request, this)); | 397 connect_job_factory_->NewConnectJob(group_name, request, this)); |
398 | 398 |
399 int rv = connect_job->Connect(); | 399 int rv = connect_job->Connect(); |
400 if (rv == OK) { | 400 if (rv == OK) { |
401 LogBoundConnectJobToRequest(connect_job->net_log().source(), request); | 401 LogBoundConnectJobToRequest(connect_job->net_log().source(), request); |
402 if (!preconnecting) { | 402 if (!preconnecting) { |
403 HandOutSocket(connect_job->PassSocket(), ClientSocketHandle::UNUSED, | 403 HandOutSocket(connect_job->PassSocket(), |
404 connect_job->connect_timing(), handle, base::TimeDelta(), | 404 ClientSocketHandle::UNUSED, |
405 group, request.net_log()); | 405 connect_job->connect_timing(), |
| 406 handle, |
| 407 base::TimeDelta(), |
| 408 group, |
| 409 request.net_log()); |
406 } else { | 410 } else { |
407 AddIdleSocket(connect_job->PassSocket(), group); | 411 AddIdleSocket(connect_job->PassSocket(), group); |
408 } | 412 } |
409 } else if (rv == ERR_IO_PENDING) { | 413 } else if (rv == ERR_IO_PENDING) { |
410 // If we don't have any sockets in this group, set a timer for potentially | 414 // If we don't have any sockets in this group, set a timer for potentially |
411 // creating a new one. If the SYN is lost, this backup socket may complete | 415 // creating a new one. If the SYN is lost, this backup socket may complete |
412 // before the slow socket, improving end user latency. | 416 // before the slow socket, improving end user latency. |
413 if (connect_backup_jobs_enabled_ && group->IsEmpty()) { | 417 if (connect_backup_jobs_enabled_ && group->IsEmpty()) { |
414 group->StartBackupJobTimer(group_name, this); | 418 group->StartBackupJobTimer(group_name, this); |
415 } | 419 } |
416 | 420 |
417 connecting_socket_count_++; | 421 connecting_socket_count_++; |
418 | 422 |
419 group->AddJob(connect_job.Pass(), preconnecting); | 423 group->AddJob(connect_job.Pass(), preconnecting); |
420 } else { | 424 } else { |
421 LogBoundConnectJobToRequest(connect_job->net_log().source(), request); | 425 LogBoundConnectJobToRequest(connect_job->net_log().source(), request); |
422 scoped_ptr<StreamSocket> error_socket; | 426 scoped_ptr<StreamSocket> error_socket; |
423 if (!preconnecting) { | 427 if (!preconnecting) { |
424 DCHECK(handle); | 428 DCHECK(handle); |
425 connect_job->GetAdditionalErrorState(handle); | 429 connect_job->GetAdditionalErrorState(handle); |
426 error_socket = connect_job->PassSocket(); | 430 error_socket = connect_job->PassSocket(); |
427 } | 431 } |
428 if (error_socket) { | 432 if (error_socket) { |
429 HandOutSocket(error_socket.Pass(), ClientSocketHandle::UNUSED, | 433 HandOutSocket(error_socket.Pass(), |
430 connect_job->connect_timing(), handle, base::TimeDelta(), | 434 ClientSocketHandle::UNUSED, |
431 group, request.net_log()); | 435 connect_job->connect_timing(), |
| 436 handle, |
| 437 base::TimeDelta(), |
| 438 group, |
| 439 request.net_log()); |
432 } else if (group->IsEmpty()) { | 440 } else if (group->IsEmpty()) { |
433 RemoveGroup(group_name); | 441 RemoveGroup(group_name); |
434 } | 442 } |
435 } | 443 } |
436 | 444 |
437 return rv; | 445 return rv; |
438 } | 446 } |
439 | 447 |
440 bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest( | 448 bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest( |
441 const Request& request, Group* group) { | 449 const Request& request, |
| 450 Group* group) { |
442 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); | 451 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
443 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); | 452 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
444 | 453 |
445 // Iterate through the idle sockets forwards (oldest to newest) | 454 // Iterate through the idle sockets forwards (oldest to newest) |
446 // * Delete any disconnected ones. | 455 // * Delete any disconnected ones. |
447 // * If we find a used idle socket, assign to |idle_socket|. At the end, | 456 // * If we find a used idle socket, assign to |idle_socket|. At the end, |
448 // the |idle_socket_it| will be set to the newest used idle socket. | 457 // the |idle_socket_it| will be set to the newest used idle socket. |
449 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); | 458 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); |
450 it != idle_sockets->end();) { | 459 it != idle_sockets->end();) { |
451 if (!it->IsUsable()) { | 460 if (!it->IsUsable()) { |
(...skipping 20 matching lines...) Expand all Loading... |
472 if (idle_socket_it != idle_sockets->end()) { | 481 if (idle_socket_it != idle_sockets->end()) { |
473 DecrementIdleCount(); | 482 DecrementIdleCount(); |
474 base::TimeDelta idle_time = | 483 base::TimeDelta idle_time = |
475 base::TimeTicks::Now() - idle_socket_it->start_time; | 484 base::TimeTicks::Now() - idle_socket_it->start_time; |
476 IdleSocket idle_socket = *idle_socket_it; | 485 IdleSocket idle_socket = *idle_socket_it; |
477 idle_sockets->erase(idle_socket_it); | 486 idle_sockets->erase(idle_socket_it); |
478 // TODO(davidben): If |idle_time| is under some low watermark, consider | 487 // TODO(davidben): If |idle_time| is under some low watermark, consider |
479 // treating as UNUSED rather than UNUSED_IDLE. This will avoid | 488 // treating as UNUSED rather than UNUSED_IDLE. This will avoid |
480 // HttpNetworkTransaction retrying on some errors. | 489 // HttpNetworkTransaction retrying on some errors. |
481 ClientSocketHandle::SocketReuseType reuse_type = | 490 ClientSocketHandle::SocketReuseType reuse_type = |
482 idle_socket.socket->WasEverUsed() ? | 491 idle_socket.socket->WasEverUsed() ? ClientSocketHandle::REUSED_IDLE |
483 ClientSocketHandle::REUSED_IDLE : | 492 : ClientSocketHandle::UNUSED_IDLE; |
484 ClientSocketHandle::UNUSED_IDLE; | 493 HandOutSocket(scoped_ptr<StreamSocket>(idle_socket.socket), |
485 HandOutSocket( | 494 reuse_type, |
486 scoped_ptr<StreamSocket>(idle_socket.socket), | 495 LoadTimingInfo::ConnectTiming(), |
487 reuse_type, | 496 request.handle(), |
488 LoadTimingInfo::ConnectTiming(), | 497 idle_time, |
489 request.handle(), | 498 group, |
490 idle_time, | 499 request.net_log()); |
491 group, | |
492 request.net_log()); | |
493 return true; | 500 return true; |
494 } | 501 } |
495 | 502 |
496 return false; | 503 return false; |
497 } | 504 } |
498 | 505 |
499 // static | 506 // static |
500 void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest( | 507 void ClientSocketPoolBaseHelper::LogBoundConnectJobToRequest( |
501 const NetLog::Source& connect_job_source, const Request& request) { | 508 const NetLog::Source& connect_job_source, |
| 509 const Request& request) { |
502 request.net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, | 510 request.net_log().AddEvent(NetLog::TYPE_SOCKET_POOL_BOUND_TO_CONNECT_JOB, |
503 connect_job_source.ToEventParametersCallback()); | 511 connect_job_source.ToEventParametersCallback()); |
504 } | 512 } |
505 | 513 |
506 void ClientSocketPoolBaseHelper::CancelRequest( | 514 void ClientSocketPoolBaseHelper::CancelRequest(const std::string& group_name, |
507 const std::string& group_name, ClientSocketHandle* handle) { | 515 ClientSocketHandle* handle) { |
508 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle); | 516 PendingCallbackMap::iterator callback_it = pending_callback_map_.find(handle); |
509 if (callback_it != pending_callback_map_.end()) { | 517 if (callback_it != pending_callback_map_.end()) { |
510 int result = callback_it->second.result; | 518 int result = callback_it->second.result; |
511 pending_callback_map_.erase(callback_it); | 519 pending_callback_map_.erase(callback_it); |
512 scoped_ptr<StreamSocket> socket = handle->PassSocket(); | 520 scoped_ptr<StreamSocket> socket = handle->PassSocket(); |
513 if (socket) { | 521 if (socket) { |
514 if (result != OK) | 522 if (result != OK) |
515 socket->Disconnect(); | 523 socket->Disconnect(); |
516 ReleaseSocket(handle->group_name(), socket.Pass(), handle->id()); | 524 ReleaseSocket(handle->group_name(), socket.Pass(), handle->id()); |
517 } | 525 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
569 } | 577 } |
570 | 578 |
571 // Can't use operator[] since it is non-const. | 579 // Can't use operator[] since it is non-const. |
572 const Group& group = *group_map_.find(group_name)->second; | 580 const Group& group = *group_map_.find(group_name)->second; |
573 | 581 |
574 if (group.HasConnectJobForHandle(handle)) { | 582 if (group.HasConnectJobForHandle(handle)) { |
575 // Just return the state of the farthest along ConnectJob for the first | 583 // Just return the state of the farthest along ConnectJob for the first |
576 // group.jobs().size() pending requests. | 584 // group.jobs().size() pending requests. |
577 LoadState max_state = LOAD_STATE_IDLE; | 585 LoadState max_state = LOAD_STATE_IDLE; |
578 for (ConnectJobSet::const_iterator job_it = group.jobs().begin(); | 586 for (ConnectJobSet::const_iterator job_it = group.jobs().begin(); |
579 job_it != group.jobs().end(); ++job_it) { | 587 job_it != group.jobs().end(); |
| 588 ++job_it) { |
580 max_state = std::max(max_state, (*job_it)->GetLoadState()); | 589 max_state = std::max(max_state, (*job_it)->GetLoadState()); |
581 } | 590 } |
582 return max_state; | 591 return max_state; |
583 } | 592 } |
584 | 593 |
585 if (group.IsStalledOnPoolMaxSockets(max_sockets_per_group_)) | 594 if (group.IsStalledOnPoolMaxSockets(max_sockets_per_group_)) |
586 return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL; | 595 return LOAD_STATE_WAITING_FOR_STALLED_SOCKET_POOL; |
587 return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET; | 596 return LOAD_STATE_WAITING_FOR_AVAILABLE_SOCKET; |
588 } | 597 } |
589 | 598 |
590 base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue( | 599 base::DictionaryValue* ClientSocketPoolBaseHelper::GetInfoAsValue( |
591 const std::string& name, const std::string& type) const { | 600 const std::string& name, |
| 601 const std::string& type) const { |
592 base::DictionaryValue* dict = new base::DictionaryValue(); | 602 base::DictionaryValue* dict = new base::DictionaryValue(); |
593 dict->SetString("name", name); | 603 dict->SetString("name", name); |
594 dict->SetString("type", type); | 604 dict->SetString("type", type); |
595 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_); | 605 dict->SetInteger("handed_out_socket_count", handed_out_socket_count_); |
596 dict->SetInteger("connecting_socket_count", connecting_socket_count_); | 606 dict->SetInteger("connecting_socket_count", connecting_socket_count_); |
597 dict->SetInteger("idle_socket_count", idle_socket_count_); | 607 dict->SetInteger("idle_socket_count", idle_socket_count_); |
598 dict->SetInteger("max_socket_count", max_sockets_); | 608 dict->SetInteger("max_socket_count", max_sockets_); |
599 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_); | 609 dict->SetInteger("max_sockets_per_group", max_sockets_per_group_); |
600 dict->SetInteger("pool_generation_number", pool_generation_number_); | 610 dict->SetInteger("pool_generation_number", pool_generation_number_); |
601 | 611 |
602 if (group_map_.empty()) | 612 if (group_map_.empty()) |
603 return dict; | 613 return dict; |
604 | 614 |
605 base::DictionaryValue* all_groups_dict = new base::DictionaryValue(); | 615 base::DictionaryValue* all_groups_dict = new base::DictionaryValue(); |
606 for (GroupMap::const_iterator it = group_map_.begin(); | 616 for (GroupMap::const_iterator it = group_map_.begin(); it != group_map_.end(); |
607 it != group_map_.end(); it++) { | 617 it++) { |
608 const Group* group = it->second; | 618 const Group* group = it->second; |
609 base::DictionaryValue* group_dict = new base::DictionaryValue(); | 619 base::DictionaryValue* group_dict = new base::DictionaryValue(); |
610 | 620 |
611 group_dict->SetInteger("pending_request_count", | 621 group_dict->SetInteger("pending_request_count", |
612 group->pending_request_count()); | 622 group->pending_request_count()); |
613 if (group->has_pending_requests()) { | 623 if (group->has_pending_requests()) { |
614 group_dict->SetString( | 624 group_dict->SetString( |
615 "top_pending_priority", | 625 "top_pending_priority", |
616 RequestPriorityToString(group->TopPendingPriority())); | 626 RequestPriorityToString(group->TopPendingPriority())); |
617 } | 627 } |
(...skipping 11 matching lines...) Expand all Loading... |
629 group_dict->Set("idle_sockets", idle_socket_list); | 639 group_dict->Set("idle_sockets", idle_socket_list); |
630 | 640 |
631 base::ListValue* connect_jobs_list = new base::ListValue(); | 641 base::ListValue* connect_jobs_list = new base::ListValue(); |
632 std::set<ConnectJob*>::const_iterator job = group->jobs().begin(); | 642 std::set<ConnectJob*>::const_iterator job = group->jobs().begin(); |
633 for (job = group->jobs().begin(); job != group->jobs().end(); job++) { | 643 for (job = group->jobs().begin(); job != group->jobs().end(); job++) { |
634 int source_id = (*job)->net_log().source().id; | 644 int source_id = (*job)->net_log().source().id; |
635 connect_jobs_list->Append(new base::FundamentalValue(source_id)); | 645 connect_jobs_list->Append(new base::FundamentalValue(source_id)); |
636 } | 646 } |
637 group_dict->Set("connect_jobs", connect_jobs_list); | 647 group_dict->Set("connect_jobs", connect_jobs_list); |
638 | 648 |
639 group_dict->SetBoolean("is_stalled", | 649 group_dict->SetBoolean( |
640 group->IsStalledOnPoolMaxSockets( | 650 "is_stalled", group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)); |
641 max_sockets_per_group_)); | |
642 group_dict->SetBoolean("backup_job_timer_is_running", | 651 group_dict->SetBoolean("backup_job_timer_is_running", |
643 group->BackupJobTimerIsRunning()); | 652 group->BackupJobTimerIsRunning()); |
644 | 653 |
645 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict); | 654 all_groups_dict->SetWithoutPathExpansion(it->first, group_dict); |
646 } | 655 } |
647 dict->Set("groups", all_groups_dict); | 656 dict->Set("groups", all_groups_dict); |
648 return dict; | 657 return dict; |
649 } | 658 } |
650 | 659 |
651 bool ClientSocketPoolBaseHelper::IdleSocket::IsUsable() const { | 660 bool ClientSocketPoolBaseHelper::IdleSocket::IsUsable() const { |
(...skipping 18 matching lines...) Expand all Loading... |
670 // Current time value. Retrieving it once at the function start rather than | 679 // Current time value. Retrieving it once at the function start rather than |
671 // inside the inner loop, since it shouldn't change by any meaningful amount. | 680 // inside the inner loop, since it shouldn't change by any meaningful amount. |
672 base::TimeTicks now = base::TimeTicks::Now(); | 681 base::TimeTicks now = base::TimeTicks::Now(); |
673 | 682 |
674 GroupMap::iterator i = group_map_.begin(); | 683 GroupMap::iterator i = group_map_.begin(); |
675 while (i != group_map_.end()) { | 684 while (i != group_map_.end()) { |
676 Group* group = i->second; | 685 Group* group = i->second; |
677 | 686 |
678 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin(); | 687 std::list<IdleSocket>::iterator j = group->mutable_idle_sockets()->begin(); |
679 while (j != group->idle_sockets().end()) { | 688 while (j != group->idle_sockets().end()) { |
680 base::TimeDelta timeout = | 689 base::TimeDelta timeout = j->socket->WasEverUsed() |
681 j->socket->WasEverUsed() ? | 690 ? used_idle_socket_timeout_ |
682 used_idle_socket_timeout_ : unused_idle_socket_timeout_; | 691 : unused_idle_socket_timeout_; |
683 if (force || j->ShouldCleanup(now, timeout)) { | 692 if (force || j->ShouldCleanup(now, timeout)) { |
684 delete j->socket; | 693 delete j->socket; |
685 j = group->mutable_idle_sockets()->erase(j); | 694 j = group->mutable_idle_sockets()->erase(j); |
686 DecrementIdleCount(); | 695 DecrementIdleCount(); |
687 } else { | 696 } else { |
688 ++j; | 697 ++j; |
689 } | 698 } |
690 } | 699 } |
691 | 700 |
692 // Delete group if no longer needed. | 701 // Delete group if no longer needed. |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 } | 761 } |
753 | 762 |
754 // static | 763 // static |
755 bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) { | 764 bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) { |
756 bool old_value = g_cleanup_timer_enabled; | 765 bool old_value = g_cleanup_timer_enabled; |
757 g_cleanup_timer_enabled = enabled; | 766 g_cleanup_timer_enabled = enabled; |
758 return old_value; | 767 return old_value; |
759 } | 768 } |
760 | 769 |
761 void ClientSocketPoolBaseHelper::StartIdleSocketTimer() { | 770 void ClientSocketPoolBaseHelper::StartIdleSocketTimer() { |
762 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, | 771 timer_.Start(FROM_HERE, |
| 772 TimeDelta::FromSeconds(kCleanupInterval), |
| 773 this, |
763 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); | 774 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); |
764 } | 775 } |
765 | 776 |
766 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, | 777 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, |
767 scoped_ptr<StreamSocket> socket, | 778 scoped_ptr<StreamSocket> socket, |
768 int id) { | 779 int id) { |
769 GroupMap::iterator i = group_map_.find(group_name); | 780 GroupMap::iterator i = group_map_.find(group_name); |
770 CHECK(i != group_map_.end()); | 781 CHECK(i != group_map_.end()); |
771 | 782 |
772 Group* group = i->second; | 783 Group* group = i->second; |
773 | 784 |
774 CHECK_GT(handed_out_socket_count_, 0); | 785 CHECK_GT(handed_out_socket_count_, 0); |
775 handed_out_socket_count_--; | 786 handed_out_socket_count_--; |
776 | 787 |
777 CHECK_GT(group->active_socket_count(), 0); | 788 CHECK_GT(group->active_socket_count(), 0); |
778 group->DecrementActiveSocketCount(); | 789 group->DecrementActiveSocketCount(); |
779 | 790 |
780 const bool can_reuse = socket->IsConnectedAndIdle() && | 791 const bool can_reuse = |
781 id == pool_generation_number_; | 792 socket->IsConnectedAndIdle() && id == pool_generation_number_; |
782 if (can_reuse) { | 793 if (can_reuse) { |
783 // Add it to the idle list. | 794 // Add it to the idle list. |
784 AddIdleSocket(socket.Pass(), group); | 795 AddIdleSocket(socket.Pass(), group); |
785 OnAvailableSocketSlot(group_name, group); | 796 OnAvailableSocketSlot(group_name, group); |
786 } else { | 797 } else { |
787 socket.reset(); | 798 socket.reset(); |
788 } | 799 } |
789 | 800 |
790 CheckForStalledSocketGroups(); | 801 CheckForStalledSocketGroups(); |
791 } | 802 } |
792 | 803 |
793 void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() { | 804 void ClientSocketPoolBaseHelper::CheckForStalledSocketGroups() { |
794 // If we have idle sockets, see if we can give one to the top-stalled group. | 805 // If we have idle sockets, see if we can give one to the top-stalled group. |
795 std::string top_group_name; | 806 std::string top_group_name; |
796 Group* top_group = NULL; | 807 Group* top_group = NULL; |
797 if (!FindTopStalledGroup(&top_group, &top_group_name)) { | 808 if (!FindTopStalledGroup(&top_group, &top_group_name)) { |
798 // There may still be a stalled group in a lower level pool. | 809 // There may still be a stalled group in a lower level pool. |
799 for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin(); | 810 for (std::set<LowerLayeredPool*>::iterator it = lower_pools_.begin(); |
800 it != lower_pools_.end(); | 811 it != lower_pools_.end(); |
801 ++it) { | 812 ++it) { |
802 if ((*it)->IsStalled()) { | 813 if ((*it)->IsStalled()) { |
803 CloseOneIdleSocket(); | 814 CloseOneIdleSocket(); |
804 break; | 815 break; |
805 } | 816 } |
806 } | 817 } |
807 return; | 818 return; |
808 } | 819 } |
809 | 820 |
810 if (ReachedMaxSocketsLimit()) { | 821 if (ReachedMaxSocketsLimit()) { |
811 if (idle_socket_count() > 0) { | 822 if (idle_socket_count() > 0) { |
812 CloseOneIdleSocket(); | 823 CloseOneIdleSocket(); |
813 } else { | 824 } else { |
814 // We can't activate more sockets since we're already at our global | 825 // We can't activate more sockets since we're already at our global |
815 // limit. | 826 // limit. |
(...skipping 12 matching lines...) Expand all Loading... |
828 // are not at the |max_sockets_per_group_| limit. Note: for requests with | 839 // are not at the |max_sockets_per_group_| limit. Note: for requests with |
829 // the same priority, the winner is based on group hash ordering (and not | 840 // the same priority, the winner is based on group hash ordering (and not |
830 // insertion order). | 841 // insertion order). |
831 bool ClientSocketPoolBaseHelper::FindTopStalledGroup( | 842 bool ClientSocketPoolBaseHelper::FindTopStalledGroup( |
832 Group** group, | 843 Group** group, |
833 std::string* group_name) const { | 844 std::string* group_name) const { |
834 CHECK((group && group_name) || (!group && !group_name)); | 845 CHECK((group && group_name) || (!group && !group_name)); |
835 Group* top_group = NULL; | 846 Group* top_group = NULL; |
836 const std::string* top_group_name = NULL; | 847 const std::string* top_group_name = NULL; |
837 bool has_stalled_group = false; | 848 bool has_stalled_group = false; |
838 for (GroupMap::const_iterator i = group_map_.begin(); | 849 for (GroupMap::const_iterator i = group_map_.begin(); i != group_map_.end(); |
839 i != group_map_.end(); ++i) { | 850 ++i) { |
840 Group* curr_group = i->second; | 851 Group* curr_group = i->second; |
841 if (!curr_group->has_pending_requests()) | 852 if (!curr_group->has_pending_requests()) |
842 continue; | 853 continue; |
843 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) { | 854 if (curr_group->IsStalledOnPoolMaxSockets(max_sockets_per_group_)) { |
844 if (!group) | 855 if (!group) |
845 return true; | 856 return true; |
846 has_stalled_group = true; | 857 has_stalled_group = true; |
847 bool has_higher_priority = !top_group || | 858 bool has_higher_priority = |
| 859 !top_group || |
848 curr_group->TopPendingPriority() > top_group->TopPendingPriority(); | 860 curr_group->TopPendingPriority() > top_group->TopPendingPriority(); |
849 if (has_higher_priority) { | 861 if (has_higher_priority) { |
850 top_group = curr_group; | 862 top_group = curr_group; |
851 top_group_name = &i->first; | 863 top_group_name = &i->first; |
852 } | 864 } |
853 } | 865 } |
854 } | 866 } |
855 | 867 |
856 if (top_group) { | 868 if (top_group) { |
857 CHECK(group); | 869 CHECK(group); |
858 *group = top_group; | 870 *group = top_group; |
859 *group_name = *top_group_name; | 871 *group_name = *top_group_name; |
860 } else { | 872 } else { |
861 CHECK(!has_stalled_group); | 873 CHECK(!has_stalled_group); |
862 } | 874 } |
863 return has_stalled_group; | 875 return has_stalled_group; |
864 } | 876 } |
865 | 877 |
866 void ClientSocketPoolBaseHelper::OnConnectJobComplete( | 878 void ClientSocketPoolBaseHelper::OnConnectJobComplete(int result, |
867 int result, ConnectJob* job) { | 879 ConnectJob* job) { |
868 DCHECK_NE(ERR_IO_PENDING, result); | 880 DCHECK_NE(ERR_IO_PENDING, result); |
869 const std::string group_name = job->group_name(); | 881 const std::string group_name = job->group_name(); |
870 GroupMap::iterator group_it = group_map_.find(group_name); | 882 GroupMap::iterator group_it = group_map_.find(group_name); |
871 CHECK(group_it != group_map_.end()); | 883 CHECK(group_it != group_map_.end()); |
872 Group* group = group_it->second; | 884 Group* group = group_it->second; |
873 | 885 |
874 scoped_ptr<StreamSocket> socket = job->PassSocket(); | 886 scoped_ptr<StreamSocket> socket = job->PassSocket(); |
875 | 887 |
876 // Copies of these are needed because |job| may be deleted before they are | 888 // Copies of these are needed because |job| may be deleted before they are |
877 // accessed. | 889 // accessed. |
878 BoundNetLog job_log = job->net_log(); | 890 BoundNetLog job_log = job->net_log(); |
879 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing(); | 891 LoadTimingInfo::ConnectTiming connect_timing = job->connect_timing(); |
880 | 892 |
881 // RemoveConnectJob(job, _) must be called by all branches below; | 893 // RemoveConnectJob(job, _) must be called by all branches below; |
882 // otherwise, |job| will be leaked. | 894 // otherwise, |job| will be leaked. |
883 | 895 |
884 if (result == OK) { | 896 if (result == OK) { |
885 DCHECK(socket.get()); | 897 DCHECK(socket.get()); |
886 RemoveConnectJob(job, group); | 898 RemoveConnectJob(job, group); |
887 scoped_ptr<const Request> request = group->PopNextPendingRequest(); | 899 scoped_ptr<const Request> request = group->PopNextPendingRequest(); |
888 if (request) { | 900 if (request) { |
889 LogBoundConnectJobToRequest(job_log.source(), *request); | 901 LogBoundConnectJobToRequest(job_log.source(), *request); |
890 HandOutSocket( | 902 HandOutSocket(socket.Pass(), |
891 socket.Pass(), ClientSocketHandle::UNUSED, connect_timing, | 903 ClientSocketHandle::UNUSED, |
892 request->handle(), base::TimeDelta(), group, request->net_log()); | 904 connect_timing, |
| 905 request->handle(), |
| 906 base::TimeDelta(), |
| 907 group, |
| 908 request->net_log()); |
893 request->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL); | 909 request->net_log().EndEvent(NetLog::TYPE_SOCKET_POOL); |
894 InvokeUserCallbackLater(request->handle(), request->callback(), result); | 910 InvokeUserCallbackLater(request->handle(), request->callback(), result); |
895 } else { | 911 } else { |
896 AddIdleSocket(socket.Pass(), group); | 912 AddIdleSocket(socket.Pass(), group); |
897 OnAvailableSocketSlot(group_name, group); | 913 OnAvailableSocketSlot(group_name, group); |
898 CheckForStalledSocketGroups(); | 914 CheckForStalledSocketGroups(); |
899 } | 915 } |
900 } else { | 916 } else { |
901 // If we got a socket, it must contain error information so pass that | 917 // If we got a socket, it must contain error information so pass that |
902 // up so that the caller can retrieve it. | 918 // up so that the caller can retrieve it. |
903 bool handed_out_socket = false; | 919 bool handed_out_socket = false; |
904 scoped_ptr<const Request> request = group->PopNextPendingRequest(); | 920 scoped_ptr<const Request> request = group->PopNextPendingRequest(); |
905 if (request) { | 921 if (request) { |
906 LogBoundConnectJobToRequest(job_log.source(), *request); | 922 LogBoundConnectJobToRequest(job_log.source(), *request); |
907 job->GetAdditionalErrorState(request->handle()); | 923 job->GetAdditionalErrorState(request->handle()); |
908 RemoveConnectJob(job, group); | 924 RemoveConnectJob(job, group); |
909 if (socket.get()) { | 925 if (socket.get()) { |
910 handed_out_socket = true; | 926 handed_out_socket = true; |
911 HandOutSocket(socket.Pass(), ClientSocketHandle::UNUSED, | 927 HandOutSocket(socket.Pass(), |
912 connect_timing, request->handle(), base::TimeDelta(), | 928 ClientSocketHandle::UNUSED, |
913 group, request->net_log()); | 929 connect_timing, |
| 930 request->handle(), |
| 931 base::TimeDelta(), |
| 932 group, |
| 933 request->net_log()); |
914 } | 934 } |
915 request->net_log().EndEventWithNetErrorCode( | 935 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, |
916 NetLog::TYPE_SOCKET_POOL, result); | 936 result); |
917 InvokeUserCallbackLater(request->handle(), request->callback(), result); | 937 InvokeUserCallbackLater(request->handle(), request->callback(), result); |
918 } else { | 938 } else { |
919 RemoveConnectJob(job, group); | 939 RemoveConnectJob(job, group); |
920 } | 940 } |
921 if (!handed_out_socket) { | 941 if (!handed_out_socket) { |
922 OnAvailableSocketSlot(group_name, group); | 942 OnAvailableSocketSlot(group_name, group); |
923 CheckForStalledSocketGroups(); | 943 CheckForStalledSocketGroups(); |
924 } | 944 } |
925 } | 945 } |
926 } | 946 } |
(...skipping 12 matching lines...) Expand all Loading... |
939 void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job, | 959 void ClientSocketPoolBaseHelper::RemoveConnectJob(ConnectJob* job, |
940 Group* group) { | 960 Group* group) { |
941 CHECK_GT(connecting_socket_count_, 0); | 961 CHECK_GT(connecting_socket_count_, 0); |
942 connecting_socket_count_--; | 962 connecting_socket_count_--; |
943 | 963 |
944 DCHECK(group); | 964 DCHECK(group); |
945 group->RemoveJob(job); | 965 group->RemoveJob(job); |
946 } | 966 } |
947 | 967 |
948 void ClientSocketPoolBaseHelper::OnAvailableSocketSlot( | 968 void ClientSocketPoolBaseHelper::OnAvailableSocketSlot( |
949 const std::string& group_name, Group* group) { | 969 const std::string& group_name, |
| 970 Group* group) { |
950 DCHECK(ContainsKey(group_map_, group_name)); | 971 DCHECK(ContainsKey(group_map_, group_name)); |
951 if (group->IsEmpty()) { | 972 if (group->IsEmpty()) { |
952 RemoveGroup(group_name); | 973 RemoveGroup(group_name); |
953 } else if (group->has_pending_requests()) { | 974 } else if (group->has_pending_requests()) { |
954 ProcessPendingRequest(group_name, group); | 975 ProcessPendingRequest(group_name, group); |
955 } | 976 } |
956 } | 977 } |
957 | 978 |
958 void ClientSocketPoolBaseHelper::ProcessPendingRequest( | 979 void ClientSocketPoolBaseHelper::ProcessPendingRequest( |
959 const std::string& group_name, Group* group) { | 980 const std::string& group_name, |
| 981 Group* group) { |
960 const Request* next_request = group->GetNextPendingRequest(); | 982 const Request* next_request = group->GetNextPendingRequest(); |
961 DCHECK(next_request); | 983 DCHECK(next_request); |
962 int rv = RequestSocketInternal(group_name, *next_request); | 984 int rv = RequestSocketInternal(group_name, *next_request); |
963 if (rv != ERR_IO_PENDING) { | 985 if (rv != ERR_IO_PENDING) { |
964 scoped_ptr<const Request> request = group->PopNextPendingRequest(); | 986 scoped_ptr<const Request> request = group->PopNextPendingRequest(); |
965 DCHECK(request); | 987 DCHECK(request); |
966 if (group->IsEmpty()) | 988 if (group->IsEmpty()) |
967 RemoveGroup(group_name); | 989 RemoveGroup(group_name); |
968 | 990 |
969 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); | 991 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); |
(...skipping 12 matching lines...) Expand all Loading... |
982 DCHECK(socket); | 1004 DCHECK(socket); |
983 handle->SetSocket(socket.Pass()); | 1005 handle->SetSocket(socket.Pass()); |
984 handle->set_reuse_type(reuse_type); | 1006 handle->set_reuse_type(reuse_type); |
985 handle->set_idle_time(idle_time); | 1007 handle->set_idle_time(idle_time); |
986 handle->set_pool_id(pool_generation_number_); | 1008 handle->set_pool_id(pool_generation_number_); |
987 handle->set_connect_timing(connect_timing); | 1009 handle->set_connect_timing(connect_timing); |
988 | 1010 |
989 if (handle->is_reused()) { | 1011 if (handle->is_reused()) { |
990 net_log.AddEvent( | 1012 net_log.AddEvent( |
991 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET, | 1013 NetLog::TYPE_SOCKET_POOL_REUSED_AN_EXISTING_SOCKET, |
992 NetLog::IntegerCallback( | 1014 NetLog::IntegerCallback("idle_ms", |
993 "idle_ms", static_cast<int>(idle_time.InMilliseconds()))); | 1015 static_cast<int>(idle_time.InMilliseconds()))); |
994 } | 1016 } |
995 | 1017 |
996 net_log.AddEvent( | 1018 net_log.AddEvent( |
997 NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, | 1019 NetLog::TYPE_SOCKET_POOL_BOUND_TO_SOCKET, |
998 handle->socket()->NetLog().source().ToEventParametersCallback()); | 1020 handle->socket()->NetLog().source().ToEventParametersCallback()); |
999 | 1021 |
1000 handed_out_socket_count_++; | 1022 handed_out_socket_count_++; |
1001 group->IncrementActiveSocketCount(); | 1023 group->IncrementActiveSocketCount(); |
1002 } | 1024 } |
1003 | 1025 |
1004 void ClientSocketPoolBaseHelper::AddIdleSocket( | 1026 void ClientSocketPoolBaseHelper::AddIdleSocket(scoped_ptr<StreamSocket> socket, |
1005 scoped_ptr<StreamSocket> socket, | 1027 Group* group) { |
1006 Group* group) { | |
1007 DCHECK(socket); | 1028 DCHECK(socket); |
1008 IdleSocket idle_socket; | 1029 IdleSocket idle_socket; |
1009 idle_socket.socket = socket.release(); | 1030 idle_socket.socket = socket.release(); |
1010 idle_socket.start_time = base::TimeTicks::Now(); | 1031 idle_socket.start_time = base::TimeTicks::Now(); |
1011 | 1032 |
1012 group->mutable_idle_sockets()->push_back(idle_socket); | 1033 group->mutable_idle_sockets()->push_back(idle_socket); |
1013 IncrementIdleCount(); | 1034 IncrementIdleCount(); |
1014 } | 1035 } |
1015 | 1036 |
1016 void ClientSocketPoolBaseHelper::CancelAllConnectJobs() { | 1037 void ClientSocketPoolBaseHelper::CancelAllConnectJobs() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1050 // RemoveGroup() is called. | 1071 // RemoveGroup() is called. |
1051 RemoveGroup(i++); | 1072 RemoveGroup(i++); |
1052 } else { | 1073 } else { |
1053 ++i; | 1074 ++i; |
1054 } | 1075 } |
1055 } | 1076 } |
1056 } | 1077 } |
1057 | 1078 |
1058 bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const { | 1079 bool ClientSocketPoolBaseHelper::ReachedMaxSocketsLimit() const { |
1059 // Each connecting socket will eventually connect and be handed out. | 1080 // Each connecting socket will eventually connect and be handed out. |
1060 int total = handed_out_socket_count_ + connecting_socket_count_ + | 1081 int total = |
1061 idle_socket_count(); | 1082 handed_out_socket_count_ + connecting_socket_count_ + idle_socket_count(); |
1062 // There can be more sockets than the limit since some requests can ignore | 1083 // There can be more sockets than the limit since some requests can ignore |
1063 // the limit | 1084 // the limit |
1064 if (total < max_sockets_) | 1085 if (total < max_sockets_) |
1065 return false; | 1086 return false; |
1066 return true; | 1087 return true; |
1067 } | 1088 } |
1068 | 1089 |
1069 bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() { | 1090 bool ClientSocketPoolBaseHelper::CloseOneIdleSocket() { |
1070 if (idle_socket_count() == 0) | 1091 if (idle_socket_count() == 0) |
1071 return false; | 1092 return false; |
(...skipping 22 matching lines...) Expand all Loading... |
1094 } | 1115 } |
1095 | 1116 |
1096 return false; | 1117 return false; |
1097 } | 1118 } |
1098 | 1119 |
1099 bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInHigherLayeredPool() { | 1120 bool ClientSocketPoolBaseHelper::CloseOneIdleConnectionInHigherLayeredPool() { |
1100 // This pool doesn't have any idle sockets. It's possible that a pool at a | 1121 // This pool doesn't have any idle sockets. It's possible that a pool at a |
1101 // higher layer is holding one of this sockets active, but it's actually idle. | 1122 // higher layer is holding one of this sockets active, but it's actually idle. |
1102 // Query the higher layers. | 1123 // Query the higher layers. |
1103 for (std::set<HigherLayeredPool*>::const_iterator it = higher_pools_.begin(); | 1124 for (std::set<HigherLayeredPool*>::const_iterator it = higher_pools_.begin(); |
1104 it != higher_pools_.end(); ++it) { | 1125 it != higher_pools_.end(); |
| 1126 ++it) { |
1105 if ((*it)->CloseOneIdleConnection()) | 1127 if ((*it)->CloseOneIdleConnection()) |
1106 return true; | 1128 return true; |
1107 } | 1129 } |
1108 return false; | 1130 return false; |
1109 } | 1131 } |
1110 | 1132 |
1111 void ClientSocketPoolBaseHelper::InvokeUserCallbackLater( | 1133 void ClientSocketPoolBaseHelper::InvokeUserCallbackLater( |
1112 ClientSocketHandle* handle, const CompletionCallback& callback, int rv) { | 1134 ClientSocketHandle* handle, |
| 1135 const CompletionCallback& callback, |
| 1136 int rv) { |
1113 CHECK(!ContainsKey(pending_callback_map_, handle)); | 1137 CHECK(!ContainsKey(pending_callback_map_, handle)); |
1114 pending_callback_map_[handle] = CallbackResultPair(callback, rv); | 1138 pending_callback_map_[handle] = CallbackResultPair(callback, rv); |
1115 base::MessageLoop::current()->PostTask( | 1139 base::MessageLoop::current()->PostTask( |
1116 FROM_HERE, | 1140 FROM_HERE, |
1117 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback, | 1141 base::Bind(&ClientSocketPoolBaseHelper::InvokeUserCallback, |
1118 weak_factory_.GetWeakPtr(), handle)); | 1142 weak_factory_.GetWeakPtr(), |
| 1143 handle)); |
1119 } | 1144 } |
1120 | 1145 |
1121 void ClientSocketPoolBaseHelper::InvokeUserCallback( | 1146 void ClientSocketPoolBaseHelper::InvokeUserCallback( |
1122 ClientSocketHandle* handle) { | 1147 ClientSocketHandle* handle) { |
1123 PendingCallbackMap::iterator it = pending_callback_map_.find(handle); | 1148 PendingCallbackMap::iterator it = pending_callback_map_.find(handle); |
1124 | 1149 |
1125 // Exit if the request has already been cancelled. | 1150 // Exit if the request has already been cancelled. |
1126 if (it == pending_callback_map_.end()) | 1151 if (it == pending_callback_map_.end()) |
1127 return; | 1152 return; |
1128 | 1153 |
1129 CHECK(!handle->is_initialized()); | 1154 CHECK(!handle->is_initialized()); |
1130 CompletionCallback callback = it->second.callback; | 1155 CompletionCallback callback = it->second.callback; |
1131 int result = it->second.result; | 1156 int result = it->second.result; |
1132 pending_callback_map_.erase(it); | 1157 pending_callback_map_.erase(it); |
1133 callback.Run(result); | 1158 callback.Run(result); |
1134 } | 1159 } |
1135 | 1160 |
1136 void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() { | 1161 void ClientSocketPoolBaseHelper::TryToCloseSocketsInLayeredPools() { |
1137 while (IsStalled()) { | 1162 while (IsStalled()) { |
1138 // Closing a socket will result in calling back into |this| to use the freed | 1163 // Closing a socket will result in calling back into |this| to use the freed |
1139 // socket slot, so nothing else is needed. | 1164 // socket slot, so nothing else is needed. |
1140 if (!CloseOneIdleConnectionInHigherLayeredPool()) | 1165 if (!CloseOneIdleConnectionInHigherLayeredPool()) |
1141 return; | 1166 return; |
1142 } | 1167 } |
1143 } | 1168 } |
1144 | 1169 |
1145 ClientSocketPoolBaseHelper::Group::Group() | 1170 ClientSocketPoolBaseHelper::Group::Group() |
1146 : unassigned_job_count_(0), | 1171 : unassigned_job_count_(0), |
1147 pending_requests_(NUM_PRIORITIES), | 1172 pending_requests_(NUM_PRIORITIES), |
1148 active_socket_count_(0) {} | 1173 active_socket_count_(0) { |
| 1174 } |
1149 | 1175 |
1150 ClientSocketPoolBaseHelper::Group::~Group() { | 1176 ClientSocketPoolBaseHelper::Group::~Group() { |
1151 DCHECK_EQ(0u, unassigned_job_count_); | 1177 DCHECK_EQ(0u, unassigned_job_count_); |
1152 } | 1178 } |
1153 | 1179 |
1154 void ClientSocketPoolBaseHelper::Group::StartBackupJobTimer( | 1180 void ClientSocketPoolBaseHelper::Group::StartBackupJobTimer( |
1155 const std::string& group_name, | 1181 const std::string& group_name, |
1156 ClientSocketPoolBaseHelper* pool) { | 1182 ClientSocketPoolBaseHelper* pool) { |
1157 // Only allow one timer to run at a time. | 1183 // Only allow one timer to run at a time. |
1158 if (BackupJobTimerIsRunning()) | 1184 if (BackupJobTimerIsRunning()) |
1159 return; | 1185 return; |
1160 | 1186 |
1161 // Unretained here is okay because |backup_job_timer_| is | 1187 // Unretained here is okay because |backup_job_timer_| is |
1162 // automatically cancelled when it's destroyed. | 1188 // automatically cancelled when it's destroyed. |
1163 backup_job_timer_.Start( | 1189 backup_job_timer_.Start(FROM_HERE, |
1164 FROM_HERE, pool->ConnectRetryInterval(), | 1190 pool->ConnectRetryInterval(), |
1165 base::Bind(&Group::OnBackupJobTimerFired, base::Unretained(this), | 1191 base::Bind(&Group::OnBackupJobTimerFired, |
1166 group_name, pool)); | 1192 base::Unretained(this), |
| 1193 group_name, |
| 1194 pool)); |
1167 } | 1195 } |
1168 | 1196 |
1169 bool ClientSocketPoolBaseHelper::Group::BackupJobTimerIsRunning() const { | 1197 bool ClientSocketPoolBaseHelper::Group::BackupJobTimerIsRunning() const { |
1170 return backup_job_timer_.IsRunning(); | 1198 return backup_job_timer_.IsRunning(); |
1171 } | 1199 } |
1172 | 1200 |
1173 bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() { | 1201 bool ClientSocketPoolBaseHelper::Group::TryToUseUnassignedConnectJob() { |
1174 SanityCheck(); | 1202 SanityCheck(); |
1175 | 1203 |
1176 if (unassigned_job_count_ == 0) | 1204 if (unassigned_job_count_ == 0) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1223 if (pool->ReachedMaxSocketsLimit() || | 1251 if (pool->ReachedMaxSocketsLimit() || |
1224 !HasAvailableSocketSlot(pool->max_sockets_per_group_) || | 1252 !HasAvailableSocketSlot(pool->max_sockets_per_group_) || |
1225 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) { | 1253 (*jobs_.begin())->GetLoadState() == LOAD_STATE_RESOLVING_HOST) { |
1226 StartBackupJobTimer(group_name, pool); | 1254 StartBackupJobTimer(group_name, pool); |
1227 return; | 1255 return; |
1228 } | 1256 } |
1229 | 1257 |
1230 if (pending_requests_.empty()) | 1258 if (pending_requests_.empty()) |
1231 return; | 1259 return; |
1232 | 1260 |
1233 scoped_ptr<ConnectJob> backup_job = | 1261 scoped_ptr<ConnectJob> backup_job = pool->connect_job_factory_->NewConnectJob( |
1234 pool->connect_job_factory_->NewConnectJob( | 1262 group_name, *pending_requests_.FirstMax().value(), pool); |
1235 group_name, *pending_requests_.FirstMax().value(), pool); | |
1236 backup_job->net_log().AddEvent(NetLog::TYPE_BACKUP_CONNECT_JOB_CREATED); | 1263 backup_job->net_log().AddEvent(NetLog::TYPE_BACKUP_CONNECT_JOB_CREATED); |
1237 SIMPLE_STATS_COUNTER("socket.backup_created"); | 1264 SIMPLE_STATS_COUNTER("socket.backup_created"); |
1238 int rv = backup_job->Connect(); | 1265 int rv = backup_job->Connect(); |
1239 pool->connecting_socket_count_++; | 1266 pool->connecting_socket_count_++; |
1240 ConnectJob* raw_backup_job = backup_job.get(); | 1267 ConnectJob* raw_backup_job = backup_job.get(); |
1241 AddJob(backup_job.Pass(), false); | 1268 AddJob(backup_job.Pass(), false); |
1242 if (rv != ERR_IO_PENDING) | 1269 if (rv != ERR_IO_PENDING) |
1243 pool->OnConnectJobComplete(rv, raw_backup_job); | 1270 pool->OnConnectJobComplete(rv, raw_backup_job); |
1244 } | 1271 } |
1245 | 1272 |
1246 void ClientSocketPoolBaseHelper::Group::SanityCheck() { | 1273 void ClientSocketPoolBaseHelper::Group::SanityCheck() { |
1247 DCHECK_LE(unassigned_job_count_, jobs_.size()); | 1274 DCHECK_LE(unassigned_job_count_, jobs_.size()); |
1248 } | 1275 } |
1249 | 1276 |
1250 void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() { | 1277 void ClientSocketPoolBaseHelper::Group::RemoveAllJobs() { |
1251 SanityCheck(); | 1278 SanityCheck(); |
1252 | 1279 |
1253 // Delete active jobs. | 1280 // Delete active jobs. |
1254 STLDeleteElements(&jobs_); | 1281 STLDeleteElements(&jobs_); |
1255 unassigned_job_count_ = 0; | 1282 unassigned_job_count_ = 0; |
1256 | 1283 |
1257 // Stop backup job timer. | 1284 // Stop backup job timer. |
1258 backup_job_timer_.Stop(); | 1285 backup_job_timer_.Stop(); |
1259 } | 1286 } |
1260 | 1287 |
1261 const ClientSocketPoolBaseHelper::Request* | 1288 const ClientSocketPoolBaseHelper::Request* |
1262 ClientSocketPoolBaseHelper::Group::GetNextPendingRequest() const { | 1289 ClientSocketPoolBaseHelper::Group::GetNextPendingRequest() const { |
1263 return | 1290 return pending_requests_.empty() ? NULL |
1264 pending_requests_.empty() ? NULL : pending_requests_.FirstMax().value(); | 1291 : pending_requests_.FirstMax().value(); |
1265 } | 1292 } |
1266 | 1293 |
1267 bool ClientSocketPoolBaseHelper::Group::HasConnectJobForHandle( | 1294 bool ClientSocketPoolBaseHelper::Group::HasConnectJobForHandle( |
1268 const ClientSocketHandle* handle) const { | 1295 const ClientSocketHandle* handle) const { |
1269 // Search the first |jobs_.size()| pending requests for |handle|. | 1296 // Search the first |jobs_.size()| pending requests for |handle|. |
1270 // If it's farther back in the deque than that, it doesn't have a | 1297 // If it's farther back in the deque than that, it doesn't have a |
1271 // corresponding ConnectJob. | 1298 // corresponding ConnectJob. |
1272 size_t i = 0; | 1299 size_t i = 0; |
1273 for (RequestQueue::Pointer pointer = pending_requests_.FirstMax(); | 1300 for (RequestQueue::Pointer pointer = pending_requests_.FirstMax(); |
1274 !pointer.is_null() && i < jobs_.size(); | 1301 !pointer.is_null() && i < jobs_.size(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1322 pending_requests_.Erase(pointer); | 1349 pending_requests_.Erase(pointer); |
1323 // If there are no more requests, kill the backup timer. | 1350 // If there are no more requests, kill the backup timer. |
1324 if (pending_requests_.empty()) | 1351 if (pending_requests_.empty()) |
1325 backup_job_timer_.Stop(); | 1352 backup_job_timer_.Stop(); |
1326 return request.Pass(); | 1353 return request.Pass(); |
1327 } | 1354 } |
1328 | 1355 |
1329 } // namespace internal | 1356 } // namespace internal |
1330 | 1357 |
1331 } // namespace net | 1358 } // namespace net |
OLD | NEW |