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