| 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 // 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 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 const Request& request, | 197 const Request& request, |
| 198 ConnectJob::Delegate* delegate) const = 0; | 198 ConnectJob::Delegate* delegate) const = 0; |
| 199 | 199 |
| 200 virtual base::TimeDelta ConnectionTimeout() const = 0; | 200 virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 201 | 201 |
| 202 private: | 202 private: |
| 203 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); | 203 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 204 }; | 204 }; |
| 205 | 205 |
| 206 ClientSocketPoolBaseHelper( | 206 ClientSocketPoolBaseHelper( |
| 207 HigherLayeredPool* pool, |
| 207 int max_sockets, | 208 int max_sockets, |
| 208 int max_sockets_per_group, | 209 int max_sockets_per_group, |
| 209 base::TimeDelta unused_idle_socket_timeout, | 210 base::TimeDelta unused_idle_socket_timeout, |
| 210 base::TimeDelta used_idle_socket_timeout, | 211 base::TimeDelta used_idle_socket_timeout, |
| 211 ConnectJobFactory* connect_job_factory); | 212 ConnectJobFactory* connect_job_factory); |
| 212 | 213 |
| 213 virtual ~ClientSocketPoolBaseHelper(); | 214 virtual ~ClientSocketPoolBaseHelper(); |
| 214 | 215 |
| 215 // Adds/Removes layered pools. It is expected in the destructor that no | 216 // Adds a lower layered pool to |this|, and adds |this| as a higher layered |
| 216 // layered pools remain. | 217 // pool on top of |lower_pool|. |
| 217 void AddLayeredPool(LayeredPool* pool); | 218 void AddLowerLayeredPool(LowerLayeredPool* lower_pool); |
| 218 void RemoveLayeredPool(LayeredPool* pool); | 219 |
| 220 // See LowerLayeredPool::IsStalled for documentation on this function. |
| 221 bool IsStalled() const; |
| 222 |
| 223 // See LowerLayeredPool for documentation on these functions. It is expected |
| 224 // in the destructor that no higher layer pools remain. |
| 225 void AddHigherLayeredPool(HigherLayeredPool* higher_pool); |
| 226 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool); |
| 219 | 227 |
| 220 // See ClientSocketPool::RequestSocket for documentation on this function. | 228 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 221 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be | 229 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be |
| 222 // heap allocated. | 230 // heap allocated. |
| 223 int RequestSocket(const std::string& group_name, const Request* request); | 231 int RequestSocket(const std::string& group_name, const Request* request); |
| 224 | 232 |
| 225 // See ClientSocketPool::RequestSocket for documentation on this function. | 233 // See ClientSocketPool::RequestSocket for documentation on this function. |
| 226 void RequestSockets(const std::string& group_name, | 234 void RequestSockets(const std::string& group_name, |
| 227 const Request& request, | 235 const Request& request, |
| 228 int num_sockets); | 236 int num_sockets); |
| 229 | 237 |
| 230 // See ClientSocketPool::CancelRequest for documentation on this function. | 238 // See ClientSocketPool::CancelRequest for documentation on this function. |
| 231 void CancelRequest(const std::string& group_name, | 239 void CancelRequest(const std::string& group_name, |
| 232 ClientSocketHandle* handle); | 240 ClientSocketHandle* handle); |
| 233 | 241 |
| 234 // See ClientSocketPool::ReleaseSocket for documentation on this function. | 242 // See ClientSocketPool::ReleaseSocket for documentation on this function. |
| 235 void ReleaseSocket(const std::string& group_name, | 243 void ReleaseSocket(const std::string& group_name, |
| 236 scoped_ptr<StreamSocket> socket, | 244 scoped_ptr<StreamSocket> socket, |
| 237 int id); | 245 int id); |
| 238 | 246 |
| 239 // See ClientSocketPool::FlushWithError for documentation on this function. | 247 // See ClientSocketPool::FlushWithError for documentation on this function. |
| 240 void FlushWithError(int error); | 248 void FlushWithError(int error); |
| 241 | 249 |
| 242 // See ClientSocketPool::IsStalled for documentation on this function. | |
| 243 bool IsStalled() const; | |
| 244 | |
| 245 // See ClientSocketPool::CloseIdleSockets for documentation on this function. | 250 // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
| 246 void CloseIdleSockets(); | 251 void CloseIdleSockets(); |
| 247 | 252 |
| 248 // See ClientSocketPool::IdleSocketCount() for documentation on this function. | 253 // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
| 249 int idle_socket_count() const { | 254 int idle_socket_count() const { |
| 250 return idle_socket_count_; | 255 return idle_socket_count_; |
| 251 } | 256 } |
| 252 | 257 |
| 253 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this | 258 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
| 254 // function. | 259 // function. |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 // sockets that timed out or can't be reused. Made public for testing. | 296 // sockets that timed out or can't be reused. Made public for testing. |
| 292 void CleanupIdleSockets(bool force); | 297 void CleanupIdleSockets(bool force); |
| 293 | 298 |
| 294 // Closes one idle socket. Picks the first one encountered. | 299 // Closes one idle socket. Picks the first one encountered. |
| 295 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we | 300 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 296 // should keep an ordered list of idle sockets, and close them in order. | 301 // should keep an ordered list of idle sockets, and close them in order. |
| 297 // Requires maintaining more state. It's not clear if it's worth it since | 302 // Requires maintaining more state. It's not clear if it's worth it since |
| 298 // I'm not sure if we hit this situation often. | 303 // I'm not sure if we hit this situation often. |
| 299 bool CloseOneIdleSocket(); | 304 bool CloseOneIdleSocket(); |
| 300 | 305 |
| 301 // Checks layered pools to see if they can close an idle connection. | 306 // Checks higher layered pools to see if they can close an idle connection. |
| 302 bool CloseOneIdleConnectionInLayeredPool(); | 307 bool CloseOneIdleConnectionInHigherLayeredPool(); |
| 303 | 308 |
| 304 // See ClientSocketPool::GetInfoAsValue for documentation on this function. | 309 // See ClientSocketPool::GetInfoAsValue for documentation on this function. |
| 305 base::DictionaryValue* GetInfoAsValue(const std::string& name, | 310 base::DictionaryValue* GetInfoAsValue(const std::string& name, |
| 306 const std::string& type) const; | 311 const std::string& type) const; |
| 307 | 312 |
| 308 base::TimeDelta ConnectionTimeout() const { | 313 base::TimeDelta ConnectionTimeout() const { |
| 309 return connect_job_factory_->ConnectionTimeout(); | 314 return connect_job_factory_->ConnectionTimeout(); |
| 310 } | 315 } |
| 311 | 316 |
| 312 static bool connect_backup_jobs_enabled(); | 317 static bool connect_backup_jobs_enabled(); |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 const scoped_ptr<ConnectJobFactory> connect_job_factory_; | 591 const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
| 587 | 592 |
| 588 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool | 593 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool |
| 589 bool connect_backup_jobs_enabled_; | 594 bool connect_backup_jobs_enabled_; |
| 590 | 595 |
| 591 // A unique id for the pool. It gets incremented every time we | 596 // A unique id for the pool. It gets incremented every time we |
| 592 // FlushWithError() the pool. This is so that when sockets get released back | 597 // FlushWithError() the pool. This is so that when sockets get released back |
| 593 // to the pool, we can make sure that they are discarded rather than reused. | 598 // to the pool, we can make sure that they are discarded rather than reused. |
| 594 int pool_generation_number_; | 599 int pool_generation_number_; |
| 595 | 600 |
| 596 std::set<LayeredPool*> higher_layer_pools_; | 601 // Used to add |this| as a higher layer pool on top of lower layer pools. May |
| 602 // be NULL if no lower layer pools will be added. |
| 603 HigherLayeredPool* pool_; |
| 604 |
| 605 // Pools that create connections through |this|. |this| will try to close |
| 606 // their idle sockets when it stalls. Must be empty on destruction. |
| 607 std::set<HigherLayeredPool*> higher_pools_; |
| 608 |
| 609 // Pools that this goes through. Typically there's only one, but not always. |
| 610 // |this| will check if they're stalled when it has a new idle socket. |this| |
| 611 // will remove itself from all lower layered pools on destruction. |
| 612 std::set<LowerLayeredPool*> lower_pools_; |
| 597 | 613 |
| 598 base::WeakPtrFactory<ClientSocketPoolBaseHelper> weak_factory_; | 614 base::WeakPtrFactory<ClientSocketPoolBaseHelper> weak_factory_; |
| 599 | 615 |
| 600 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper); | 616 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper); |
| 601 }; | 617 }; |
| 602 | 618 |
| 603 } // namespace internal | 619 } // namespace internal |
| 604 | 620 |
| 605 template <typename SocketParams> | 621 template <typename SocketParams> |
| 606 class ClientSocketPoolBase { | 622 class ClientSocketPoolBase { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 640 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); | 656 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 641 }; | 657 }; |
| 642 | 658 |
| 643 // |max_sockets| is the maximum number of sockets to be maintained by this | 659 // |max_sockets| is the maximum number of sockets to be maintained by this |
| 644 // ClientSocketPool. |max_sockets_per_group| specifies the maximum number of | 660 // ClientSocketPool. |max_sockets_per_group| specifies the maximum number of |
| 645 // sockets a "group" can have. |unused_idle_socket_timeout| specifies how | 661 // sockets a "group" can have. |unused_idle_socket_timeout| specifies how |
| 646 // long to leave an unused idle socket open before closing it. | 662 // long to leave an unused idle socket open before closing it. |
| 647 // |used_idle_socket_timeout| specifies how long to leave a previously used | 663 // |used_idle_socket_timeout| specifies how long to leave a previously used |
| 648 // idle socket open before closing it. | 664 // idle socket open before closing it. |
| 649 ClientSocketPoolBase( | 665 ClientSocketPoolBase( |
| 666 HigherLayeredPool* self, |
| 650 int max_sockets, | 667 int max_sockets, |
| 651 int max_sockets_per_group, | 668 int max_sockets_per_group, |
| 652 ClientSocketPoolHistograms* histograms, | 669 ClientSocketPoolHistograms* histograms, |
| 653 base::TimeDelta unused_idle_socket_timeout, | 670 base::TimeDelta unused_idle_socket_timeout, |
| 654 base::TimeDelta used_idle_socket_timeout, | 671 base::TimeDelta used_idle_socket_timeout, |
| 655 ConnectJobFactory* connect_job_factory) | 672 ConnectJobFactory* connect_job_factory) |
| 656 : histograms_(histograms), | 673 : histograms_(histograms), |
| 657 helper_(max_sockets, max_sockets_per_group, | 674 helper_(self, max_sockets, max_sockets_per_group, |
| 658 unused_idle_socket_timeout, used_idle_socket_timeout, | 675 unused_idle_socket_timeout, used_idle_socket_timeout, |
| 659 new ConnectJobFactoryAdaptor(connect_job_factory)) {} | 676 new ConnectJobFactoryAdaptor(connect_job_factory)) {} |
| 660 | 677 |
| 661 virtual ~ClientSocketPoolBase() {} | 678 virtual ~ClientSocketPoolBase() {} |
| 662 | 679 |
| 663 // These member functions simply forward to ClientSocketPoolBaseHelper. | 680 // These member functions simply forward to ClientSocketPoolBaseHelper. |
| 664 void AddLayeredPool(LayeredPool* pool) { | 681 void AddLowerLayeredPool(LowerLayeredPool* lower_pool) { |
| 665 helper_.AddLayeredPool(pool); | 682 helper_.AddLowerLayeredPool(lower_pool); |
| 666 } | 683 } |
| 667 | 684 |
| 668 void RemoveLayeredPool(LayeredPool* pool) { | 685 void AddHigherLayeredPool(HigherLayeredPool* higher_pool) { |
| 669 helper_.RemoveLayeredPool(pool); | 686 helper_.AddHigherLayeredPool(higher_pool); |
| 687 } |
| 688 |
| 689 void RemoveHigherLayeredPool(HigherLayeredPool* higher_pool) { |
| 690 helper_.RemoveHigherLayeredPool(higher_pool); |
| 670 } | 691 } |
| 671 | 692 |
| 672 // RequestSocket bundles up the parameters into a Request and then forwards to | 693 // RequestSocket bundles up the parameters into a Request and then forwards to |
| 673 // ClientSocketPoolBaseHelper::RequestSocket(). | 694 // ClientSocketPoolBaseHelper::RequestSocket(). |
| 674 int RequestSocket(const std::string& group_name, | 695 int RequestSocket(const std::string& group_name, |
| 675 const scoped_refptr<SocketParams>& params, | 696 const scoped_refptr<SocketParams>& params, |
| 676 RequestPriority priority, | 697 RequestPriority priority, |
| 677 ClientSocketHandle* handle, | 698 ClientSocketHandle* handle, |
| 678 const CompletionCallback& callback, | 699 const CompletionCallback& callback, |
| 679 const BoundNetLog& net_log) { | 700 const BoundNetLog& net_log) { |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 764 } | 785 } |
| 765 | 786 |
| 766 ClientSocketPoolHistograms* histograms() const { | 787 ClientSocketPoolHistograms* histograms() const { |
| 767 return histograms_; | 788 return histograms_; |
| 768 } | 789 } |
| 769 | 790 |
| 770 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } | 791 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } |
| 771 | 792 |
| 772 bool CloseOneIdleSocket() { return helper_.CloseOneIdleSocket(); } | 793 bool CloseOneIdleSocket() { return helper_.CloseOneIdleSocket(); } |
| 773 | 794 |
| 774 bool CloseOneIdleConnectionInLayeredPool() { | 795 bool CloseOneIdleConnectionInHigherLayeredPool() { |
| 775 return helper_.CloseOneIdleConnectionInLayeredPool(); | 796 return helper_.CloseOneIdleConnectionInHigherLayeredPool(); |
| 776 } | 797 } |
| 777 | 798 |
| 778 private: | 799 private: |
| 779 // This adaptor class exists to bridge the | 800 // This adaptor class exists to bridge the |
| 780 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and | 801 // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
| 781 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the | 802 // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
| 782 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to | 803 // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
| 783 // static_cast themselves. | 804 // static_cast themselves. |
| 784 class ConnectJobFactoryAdaptor | 805 class ConnectJobFactoryAdaptor |
| 785 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { | 806 : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 810 // Histograms for the pool | 831 // Histograms for the pool |
| 811 ClientSocketPoolHistograms* const histograms_; | 832 ClientSocketPoolHistograms* const histograms_; |
| 812 internal::ClientSocketPoolBaseHelper helper_; | 833 internal::ClientSocketPoolBaseHelper helper_; |
| 813 | 834 |
| 814 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 835 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 815 }; | 836 }; |
| 816 | 837 |
| 817 } // namespace net | 838 } // namespace net |
| 818 | 839 |
| 819 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 840 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |