| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A ClientSocketPoolBase is used to restrict the number of sockets open at | 5 // A ClientSocketPoolBase is used to restrict the number of sockets open at |
| 6 // a time. It also maintains a list of idle persistent sockets for reuse. | 6 // a time. It also maintains a list of idle persistent sockets for reuse. |
| 7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle | 7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle |
| 8 // the core logic of (1) restricting the number of active (connected or | 8 // the core logic of (1) restricting the number of active (connected or |
| 9 // connecting) sockets per "group" (generally speaking, the hostname), (2) | 9 // connecting) sockets per "group" (generally speaking, the hostname), (2) |
| 10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) | 10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // | 21 // |
| 22 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 22 #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 23 #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 23 #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 24 #pragma once | 24 #pragma once |
| 25 | 25 |
| 26 #include <deque> | 26 #include <deque> |
| 27 #include <list> | 27 #include <list> |
| 28 #include <map> | 28 #include <map> |
| 29 #include <set> | 29 #include <set> |
| 30 #include <string> | 30 #include <string> |
| 31 #include <vector> | |
| 32 | 31 |
| 33 #include "base/basictypes.h" | 32 #include "base/basictypes.h" |
| 34 #include "base/memory/ref_counted.h" | 33 #include "base/memory/ref_counted.h" |
| 35 #include "base/memory/scoped_ptr.h" | 34 #include "base/memory/scoped_ptr.h" |
| 36 #include "base/task.h" | 35 #include "base/task.h" |
| 37 #include "base/time.h" | 36 #include "base/time.h" |
| 38 #include "base/timer.h" | 37 #include "base/timer.h" |
| 39 #include "net/base/address_list.h" | 38 #include "net/base/address_list.h" |
| 40 #include "net/base/completion_callback.h" | 39 #include "net/base/completion_callback.h" |
| 41 #include "net/base/load_states.h" | 40 #include "net/base/load_states.h" |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 | 232 |
| 234 ClientSocketPoolBaseHelper( | 233 ClientSocketPoolBaseHelper( |
| 235 int max_sockets, | 234 int max_sockets, |
| 236 int max_sockets_per_group, | 235 int max_sockets_per_group, |
| 237 base::TimeDelta unused_idle_socket_timeout, | 236 base::TimeDelta unused_idle_socket_timeout, |
| 238 base::TimeDelta used_idle_socket_timeout, | 237 base::TimeDelta used_idle_socket_timeout, |
| 239 ConnectJobFactory* connect_job_factory); | 238 ConnectJobFactory* connect_job_factory); |
| 240 | 239 |
| 241 virtual ~ClientSocketPoolBaseHelper(); | 240 virtual ~ClientSocketPoolBaseHelper(); |
| 242 | 241 |
| 243 // Adds/Removes layered pools. It is expected in the destructor that no | |
| 244 // layered pools remain. | |
| 245 void AddLayeredPool(LayeredPool* pool); | |
| 246 void RemoveLayeredPool(LayeredPool* pool); | |
| 247 | |
| 248 // See ClientSocketPool::RequestSocket for documentation on this function. | 242 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 249 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be | 243 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be |
| 250 // heap allocated. | 244 // heap allocated. |
| 251 int RequestSocket(const std::string& group_name, const Request* request); | 245 int RequestSocket(const std::string& group_name, const Request* request); |
| 252 | 246 |
| 253 // See ClientSocketPool::RequestSocket for documentation on this function. | 247 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 254 void RequestSockets(const std::string& group_name, | 248 void RequestSockets(const std::string& group_name, |
| 255 const Request& request, | 249 const Request& request, |
| 256 int num_sockets); | 250 int num_sockets); |
| 257 | 251 |
| 258 // See ClientSocketPool::CancelRequest for documentation on this function. | 252 // See ClientSocketPool::CancelRequest for documentation on this function. |
| 259 void CancelRequest(const std::string& group_name, | 253 void CancelRequest(const std::string& group_name, |
| 260 ClientSocketHandle* handle); | 254 ClientSocketHandle* handle); |
| 261 | 255 |
| 262 // See ClientSocketPool::ReleaseSocket for documentation on this function. | 256 // See ClientSocketPool::ReleaseSocket for documentation on this function. |
| 263 void ReleaseSocket(const std::string& group_name, | 257 void ReleaseSocket(const std::string& group_name, |
| 264 StreamSocket* socket, | 258 StreamSocket* socket, |
| 265 int id); | 259 int id); |
| 266 | 260 |
| 267 // See ClientSocketPool::Flush for documentation on this function. | 261 // See ClientSocketPool::Flush for documentation on this function. |
| 268 void Flush(); | 262 void Flush(); |
| 269 | 263 |
| 270 // See ClientSocketPool::IsStalled for documentation on this function. | |
| 271 bool IsStalled() const; | |
| 272 | |
| 273 // See ClientSocketPool::CloseIdleSockets for documentation on this function. | 264 // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
| 274 void CloseIdleSockets(); | 265 void CloseIdleSockets(); |
| 275 | 266 |
| 276 // See ClientSocketPool::IdleSocketCount() for documentation on this function. | 267 // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
| 277 int idle_socket_count() const { | 268 int idle_socket_count() const { |
| 278 return idle_socket_count_; | 269 return idle_socket_count_; |
| 279 } | 270 } |
| 280 | 271 |
| 281 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this | 272 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
| 282 // function. | 273 // function. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 307 // by kCleanupInterval are cleaned up using a timer. Otherwise they are | 298 // by kCleanupInterval are cleaned up using a timer. Otherwise they are |
| 308 // closed next time client makes a request. This may reduce network | 299 // closed next time client makes a request. This may reduce network |
| 309 // activity and power consumption. | 300 // activity and power consumption. |
| 310 static bool cleanup_timer_enabled(); | 301 static bool cleanup_timer_enabled(); |
| 311 static bool set_cleanup_timer_enabled(bool enabled); | 302 static bool set_cleanup_timer_enabled(bool enabled); |
| 312 | 303 |
| 313 // Closes all idle sockets if |force| is true. Else, only closes idle | 304 // Closes all idle sockets if |force| is true. Else, only closes idle |
| 314 // sockets that timed out or can't be reused. Made public for testing. | 305 // sockets that timed out or can't be reused. Made public for testing. |
| 315 void CleanupIdleSockets(bool force); | 306 void CleanupIdleSockets(bool force); |
| 316 | 307 |
| 317 // Closes one idle socket. Picks the first one encountered. | |
| 318 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we | |
| 319 // should keep an ordered list of idle sockets, and close them in order. | |
| 320 // Requires maintaining more state. It's not clear if it's worth it since | |
| 321 // I'm not sure if we hit this situation often. | |
| 322 bool CloseOneIdleSocket(); | |
| 323 | |
| 324 // Checks layered pools to see if they can close an idle connection. | |
| 325 bool CloseOneIdleConnectionInLayeredPool(); | |
| 326 | |
| 327 // See ClientSocketPool::GetInfoAsValue for documentation on this function. | 308 // See ClientSocketPool::GetInfoAsValue for documentation on this function. |
| 328 base::DictionaryValue* GetInfoAsValue(const std::string& name, | 309 base::DictionaryValue* GetInfoAsValue(const std::string& name, |
| 329 const std::string& type) const; | 310 const std::string& type) const; |
| 330 | 311 |
| 331 base::TimeDelta ConnectionTimeout() const { | 312 base::TimeDelta ConnectionTimeout() const { |
| 332 return connect_job_factory_->ConnectionTimeout(); | 313 return connect_job_factory_->ConnectionTimeout(); |
| 333 } | 314 } |
| 334 | 315 |
| 335 static bool connect_backup_jobs_enabled(); | 316 static bool connect_backup_jobs_enabled(); |
| 336 static bool set_connect_backup_jobs_enabled(bool enabled); | 317 static bool set_connect_backup_jobs_enabled(bool enabled); |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 void IncrementIdleCount(); | 450 void IncrementIdleCount(); |
| 470 void DecrementIdleCount(); | 451 void DecrementIdleCount(); |
| 471 | 452 |
| 472 // Start cleanup timer for idle sockets. | 453 // Start cleanup timer for idle sockets. |
| 473 void StartIdleSocketTimer(); | 454 void StartIdleSocketTimer(); |
| 474 | 455 |
| 475 // Scans the group map for groups which have an available socket slot and | 456 // Scans the group map for groups which have an available socket slot and |
| 476 // at least one pending request. Returns true if any groups are stalled, and | 457 // at least one pending request. Returns true if any groups are stalled, and |
| 477 // if so, fills |group| and |group_name| with data of the stalled group | 458 // if so, fills |group| and |group_name| with data of the stalled group |
| 478 // having highest priority. | 459 // having highest priority. |
| 479 bool FindTopStalledGroup(Group** group, std::string* group_name) const; | 460 bool FindTopStalledGroup(Group** group, std::string* group_name); |
| 480 | 461 |
| 481 // Called when timer_ fires. This method scans the idle sockets removing | 462 // Called when timer_ fires. This method scans the idle sockets removing |
| 482 // sockets that timed out or can't be reused. | 463 // sockets that timed out or can't be reused. |
| 483 void OnCleanupTimerFired() { | 464 void OnCleanupTimerFired() { |
| 484 CleanupIdleSockets(false); | 465 CleanupIdleSockets(false); |
| 485 } | 466 } |
| 486 | 467 |
| 487 // Removes |job| from |connect_job_set_|. Also updates |group| if non-NULL. | 468 // Removes |job| from |connect_job_set_|. Also updates |group| if non-NULL. |
| 488 void RemoveConnectJob(ConnectJob* job, Group* group); | 469 void RemoveConnectJob(ConnectJob* job, Group* group); |
| 489 | 470 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 521 int RequestSocketInternal(const std::string& group_name, | 502 int RequestSocketInternal(const std::string& group_name, |
| 522 const Request* request); | 503 const Request* request); |
| 523 | 504 |
| 524 // Assigns an idle socket for the group to the request. | 505 // Assigns an idle socket for the group to the request. |
| 525 // Returns |true| if an idle socket is available, false otherwise. | 506 // Returns |true| if an idle socket is available, false otherwise. |
| 526 bool AssignIdleSocketToGroup(const Request* request, Group* group); | 507 bool AssignIdleSocketToGroup(const Request* request, Group* group); |
| 527 | 508 |
| 528 static void LogBoundConnectJobToRequest( | 509 static void LogBoundConnectJobToRequest( |
| 529 const NetLog::Source& connect_job_source, const Request* request); | 510 const NetLog::Source& connect_job_source, const Request* request); |
| 530 | 511 |
| 512 // Closes one idle socket. Picks the first one encountered. |
| 513 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 514 // should keep an ordered list of idle sockets, and close them in order. |
| 515 // Requires maintaining more state. It's not clear if it's worth it since |
| 516 // I'm not sure if we hit this situation often. |
| 517 void CloseOneIdleSocket(); |
| 518 |
| 531 // Same as CloseOneIdleSocket() except it won't close an idle socket in | 519 // Same as CloseOneIdleSocket() except it won't close an idle socket in |
| 532 // |group|. If |group| is NULL, it is ignored. Returns true if it closed a | 520 // |group|. If |group| is NULL, it is ignored. Returns true if it closed a |
| 533 // socket. | 521 // socket. |
| 534 bool CloseOneIdleSocketExceptInGroup(const Group* group); | 522 bool CloseOneIdleSocketExceptInGroup(const Group* group); |
| 535 | 523 |
| 536 // Checks if there are stalled socket groups that should be notified | 524 // Checks if there are stalled socket groups that should be notified |
| 537 // for possible wakeup. | 525 // for possible wakeup. |
| 538 void CheckForStalledSocketGroups(); | 526 void CheckForStalledSocketGroups(); |
| 539 | 527 |
| 540 // Posts a task to call InvokeUserCallback() on the next iteration through the | 528 // Posts a task to call InvokeUserCallback() on the next iteration through the |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 const scoped_ptr<ConnectJobFactory> connect_job_factory_; | 573 const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
| 586 | 574 |
| 587 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool | 575 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool |
| 588 bool connect_backup_jobs_enabled_; | 576 bool connect_backup_jobs_enabled_; |
| 589 | 577 |
| 590 // A unique id for the pool. It gets incremented every time we Flush() the | 578 // A unique id for the pool. It gets incremented every time we Flush() the |
| 591 // pool. This is so that when sockets get released back to the pool, we can | 579 // pool. This is so that when sockets get released back to the pool, we can |
| 592 // make sure that they are discarded rather than reused. | 580 // make sure that they are discarded rather than reused. |
| 593 int pool_generation_number_; | 581 int pool_generation_number_; |
| 594 | 582 |
| 595 std::set<LayeredPool*> higher_layer_pools_; | |
| 596 | |
| 597 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; | 583 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; |
| 598 | 584 |
| 599 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper); | 585 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper); |
| 600 }; | 586 }; |
| 601 | 587 |
| 602 } // namespace internal | 588 } // namespace internal |
| 603 | 589 |
| 604 template <typename SocketParams> | 590 template <typename SocketParams> |
| 605 class ClientSocketPoolBase { | 591 class ClientSocketPoolBase { |
| 606 public: | 592 public: |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 653 base::TimeDelta used_idle_socket_timeout, | 639 base::TimeDelta used_idle_socket_timeout, |
| 654 ConnectJobFactory* connect_job_factory) | 640 ConnectJobFactory* connect_job_factory) |
| 655 : histograms_(histograms), | 641 : histograms_(histograms), |
| 656 helper_(max_sockets, max_sockets_per_group, | 642 helper_(max_sockets, max_sockets_per_group, |
| 657 unused_idle_socket_timeout, used_idle_socket_timeout, | 643 unused_idle_socket_timeout, used_idle_socket_timeout, |
| 658 new ConnectJobFactoryAdaptor(connect_job_factory)) {} | 644 new ConnectJobFactoryAdaptor(connect_job_factory)) {} |
| 659 | 645 |
| 660 virtual ~ClientSocketPoolBase() {} | 646 virtual ~ClientSocketPoolBase() {} |
| 661 | 647 |
| 662 // These member functions simply forward to ClientSocketPoolBaseHelper. | 648 // These member functions simply forward to ClientSocketPoolBaseHelper. |
| 663 void AddLayeredPool(LayeredPool* pool) { | |
| 664 helper_.AddLayeredPool(pool); | |
| 665 } | |
| 666 | |
| 667 void RemoveLayeredPool(LayeredPool* pool) { | |
| 668 helper_.RemoveLayeredPool(pool); | |
| 669 } | |
| 670 | 649 |
| 671 // RequestSocket bundles up the parameters into a Request and then forwards to | 650 // RequestSocket bundles up the parameters into a Request and then forwards to |
| 672 // ClientSocketPoolBaseHelper::RequestSocket(). | 651 // ClientSocketPoolBaseHelper::RequestSocket(). |
| 673 int RequestSocket(const std::string& group_name, | 652 int RequestSocket(const std::string& group_name, |
| 674 const scoped_refptr<SocketParams>& params, | 653 const scoped_refptr<SocketParams>& params, |
| 675 RequestPriority priority, | 654 RequestPriority priority, |
| 676 ClientSocketHandle* handle, | 655 ClientSocketHandle* handle, |
| 677 OldCompletionCallback* callback, | 656 OldCompletionCallback* callback, |
| 678 const BoundNetLog& net_log) { | 657 const BoundNetLog& net_log) { |
| 679 Request* request = | 658 Request* request = |
| (...skipping 24 matching lines...) Expand all Loading... |
| 704 void CancelRequest(const std::string& group_name, | 683 void CancelRequest(const std::string& group_name, |
| 705 ClientSocketHandle* handle) { | 684 ClientSocketHandle* handle) { |
| 706 return helper_.CancelRequest(group_name, handle); | 685 return helper_.CancelRequest(group_name, handle); |
| 707 } | 686 } |
| 708 | 687 |
| 709 void ReleaseSocket(const std::string& group_name, StreamSocket* socket, | 688 void ReleaseSocket(const std::string& group_name, StreamSocket* socket, |
| 710 int id) { | 689 int id) { |
| 711 return helper_.ReleaseSocket(group_name, socket, id); | 690 return helper_.ReleaseSocket(group_name, socket, id); |
| 712 } | 691 } |
| 713 | 692 |
| 714 void Flush() { helper_.Flush(); } | |
| 715 | |
| 716 bool IsStalled() const { return helper_.IsStalled(); } | |
| 717 | |
| 718 void CloseIdleSockets() { return helper_.CloseIdleSockets(); } | 693 void CloseIdleSockets() { return helper_.CloseIdleSockets(); } |
| 719 | 694 |
| 720 int idle_socket_count() const { return helper_.idle_socket_count(); } | 695 int idle_socket_count() const { return helper_.idle_socket_count(); } |
| 721 | 696 |
| 722 int IdleSocketCountInGroup(const std::string& group_name) const { | 697 int IdleSocketCountInGroup(const std::string& group_name) const { |
| 723 return helper_.IdleSocketCountInGroup(group_name); | 698 return helper_.IdleSocketCountInGroup(group_name); |
| 724 } | 699 } |
| 725 | 700 |
| 726 LoadState GetLoadState(const std::string& group_name, | 701 LoadState GetLoadState(const std::string& group_name, |
| 727 const ClientSocketHandle* handle) const { | 702 const ClientSocketHandle* handle) const { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 756 base::TimeDelta ConnectionTimeout() const { | 731 base::TimeDelta ConnectionTimeout() const { |
| 757 return helper_.ConnectionTimeout(); | 732 return helper_.ConnectionTimeout(); |
| 758 } | 733 } |
| 759 | 734 |
| 760 ClientSocketPoolHistograms* histograms() const { | 735 ClientSocketPoolHistograms* histograms() const { |
| 761 return histograms_; | 736 return histograms_; |
| 762 } | 737 } |
| 763 | 738 |
| 764 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } | 739 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } |
| 765 | 740 |
| 766 bool CloseOneIdleSocket() { return helper_.CloseOneIdleSocket(); } | 741 void Flush() { helper_.Flush(); } |
| 767 | |
| 768 bool CloseOneIdleConnectionInLayeredPool() { | |
| 769 return helper_.CloseOneIdleConnectionInLayeredPool(); | |
| 770 } | |
| 771 | 742 |
| 772 private: | 743 private: |
| 773 // This adaptor class exists to bridge the | 744 // This adaptor class exists to bridge the |
| 774 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and | 745 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
| 775 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the | 746 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
| 776 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to | 747 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
| 777 // static_cast themselves. | 748 // static_cast themselves. |
| 778 class ConnectJobFactoryAdaptor | 749 class ConnectJobFactoryAdaptor |
| 779 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { | 750 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| 780 public: | 751 public: |
| (...skipping 23 matching lines...) Expand all Loading... |
| 804 // Histograms for the pool | 775 // Histograms for the pool |
| 805 ClientSocketPoolHistograms* const histograms_; | 776 ClientSocketPoolHistograms* const histograms_; |
| 806 internal::ClientSocketPoolBaseHelper helper_; | 777 internal::ClientSocketPoolBaseHelper helper_; |
| 807 | 778 |
| 808 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 779 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 809 }; | 780 }; |
| 810 | 781 |
| 811 } // namespace net | 782 } // namespace net |
| 812 | 783 |
| 813 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 784 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |