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