| 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 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 307 int NumConnectJobsInGroup(const std::string& group_name) const { | 307 int NumConnectJobsInGroup(const std::string& group_name) const { |
| 308 return group_map_.find(group_name)->second->jobs().size(); | 308 return group_map_.find(group_name)->second->jobs().size(); |
| 309 } | 309 } |
| 310 | 310 |
| 311 int NumActiveSocketsInGroup(const std::string& group_name) const { | 311 int NumActiveSocketsInGroup(const std::string& group_name) const { |
| 312 return group_map_.find(group_name)->second->active_socket_count(); | 312 return group_map_.find(group_name)->second->active_socket_count(); |
| 313 } | 313 } |
| 314 | 314 |
| 315 bool HasGroup(const std::string& group_name) const; | 315 bool HasGroup(const std::string& group_name) const; |
| 316 | 316 |
| 317 // Called to enable/disable cleaning up idle sockets. When enabled, | |
| 318 // idle sockets that have been around for longer than a period defined | |
| 319 // by kCleanupInterval are cleaned up using a timer. Otherwise they are | |
| 320 // closed next time client makes a request. This may reduce network | |
| 321 // activity and power consumption. | |
| 322 static bool cleanup_timer_enabled(); | |
| 323 static bool set_cleanup_timer_enabled(bool enabled); | |
| 324 | |
| 325 // Closes all idle sockets if |force| is true. Else, only closes idle | 317 // Closes all idle sockets if |force| is true. Else, only closes idle |
| 326 // sockets that timed out or can't be reused. Made public for testing. | 318 // sockets that timed out or can't be reused. Made public for testing. |
| 327 void CleanupIdleSockets(bool force); | 319 void CleanupIdleSockets(bool force); |
| 328 | 320 |
| 329 // Closes one idle socket. Picks the first one encountered. | 321 // Closes one idle socket. Picks the first one encountered. |
| 330 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we | 322 // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 331 // should keep an ordered list of idle sockets, and close them in order. | 323 // should keep an ordered list of idle sockets, and close them in order. |
| 332 // Requires maintaining more state. It's not clear if it's worth it since | 324 // Requires maintaining more state. It's not clear if it's worth it since |
| 333 // I'm not sure if we hit this situation often. | 325 // I'm not sure if we hit this situation often. |
| 334 bool CloseOneIdleSocket(); | 326 bool CloseOneIdleSocket(); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 PendingCallbackMap; | 518 PendingCallbackMap; |
| 527 | 519 |
| 528 Group* GetOrCreateGroup(const std::string& group_name); | 520 Group* GetOrCreateGroup(const std::string& group_name); |
| 529 void RemoveGroup(const std::string& group_name); | 521 void RemoveGroup(const std::string& group_name); |
| 530 void RemoveGroup(GroupMap::iterator it); | 522 void RemoveGroup(GroupMap::iterator it); |
| 531 | 523 |
| 532 // Called when the number of idle sockets changes. | 524 // Called when the number of idle sockets changes. |
| 533 void IncrementIdleCount(); | 525 void IncrementIdleCount(); |
| 534 void DecrementIdleCount(); | 526 void DecrementIdleCount(); |
| 535 | 527 |
| 536 // Start cleanup timer for idle sockets. | |
| 537 void StartIdleSocketTimer(); | |
| 538 | |
| 539 // Scans the group map for groups which have an available socket slot and | 528 // Scans the group map for groups which have an available socket slot and |
| 540 // at least one pending request. Returns true if any groups are stalled, and | 529 // at least one pending request. Returns true if any groups are stalled, and |
| 541 // if so (and if both |group| and |group_name| are not NULL), fills |group| | 530 // if so (and if both |group| and |group_name| are not NULL), fills |group| |
| 542 // and |group_name| with data of the stalled group having highest priority. | 531 // and |group_name| with data of the stalled group having highest priority. |
| 543 bool FindTopStalledGroup(Group** group, std::string* group_name) const; | 532 bool FindTopStalledGroup(Group** group, std::string* group_name) const; |
| 544 | 533 |
| 545 // Called when timer_ fires. This method scans the idle sockets removing | |
| 546 // sockets that timed out or can't be reused. | |
| 547 void OnCleanupTimerFired() { | |
| 548 CleanupIdleSockets(false); | |
| 549 } | |
| 550 | |
| 551 // Removes |job| from |group|, which must already own |job|. | 534 // Removes |job| from |group|, which must already own |job|. |
| 552 void RemoveConnectJob(ConnectJob* job, Group* group); | 535 void RemoveConnectJob(ConnectJob* job, Group* group); |
| 553 | 536 |
| 554 // Tries to see if we can handle any more requests for |group|. | 537 // Tries to see if we can handle any more requests for |group|. |
| 555 void OnAvailableSocketSlot(const std::string& group_name, Group* group); | 538 void OnAvailableSocketSlot(const std::string& group_name, Group* group); |
| 556 | 539 |
| 557 // Process a pending socket request for a group. | 540 // Process a pending socket request for a group. |
| 558 void ProcessPendingRequest(const std::string& group_name, Group* group); | 541 void ProcessPendingRequest(const std::string& group_name, Group* group); |
| 559 | 542 |
| 560 // Assigns |socket| to |handle| and updates |group|'s counters appropriately. | 543 // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 618 // this pool is stalled. | 601 // this pool is stalled. |
| 619 void TryToCloseSocketsInLayeredPools(); | 602 void TryToCloseSocketsInLayeredPools(); |
| 620 | 603 |
| 621 GroupMap group_map_; | 604 GroupMap group_map_; |
| 622 | 605 |
| 623 // Map of the ClientSocketHandles for which we have a pending Task to invoke a | 606 // Map of the ClientSocketHandles for which we have a pending Task to invoke a |
| 624 // callback. This is necessary since, before we invoke said callback, it's | 607 // callback. This is necessary since, before we invoke said callback, it's |
| 625 // possible that the request is cancelled. | 608 // possible that the request is cancelled. |
| 626 PendingCallbackMap pending_callback_map_; | 609 PendingCallbackMap pending_callback_map_; |
| 627 | 610 |
| 628 // Timer used to periodically prune idle sockets that timed out or can't be | |
| 629 // reused. | |
| 630 base::RepeatingTimer timer_; | |
| 631 | |
| 632 // The total number of idle sockets in the system. | 611 // The total number of idle sockets in the system. |
| 633 int idle_socket_count_; | 612 int idle_socket_count_; |
| 634 | 613 |
| 635 // Number of connecting sockets across all groups. | 614 // Number of connecting sockets across all groups. |
| 636 int connecting_socket_count_; | 615 int connecting_socket_count_; |
| 637 | 616 |
| 638 // Number of connected sockets we handed out across all groups. | 617 // Number of connected sockets we handed out across all groups. |
| 639 int handed_out_socket_count_; | 618 int handed_out_socket_count_; |
| 640 | 619 |
| 641 // The maximum total number of sockets. See ReachedMaxSocketsLimit. | 620 // The maximum total number of sockets. See ReachedMaxSocketsLimit. |
| 642 const int max_sockets_; | 621 const int max_sockets_; |
| 643 | 622 |
| 644 // The maximum number of sockets kept per group. | 623 // The maximum number of sockets kept per group. |
| 645 const int max_sockets_per_group_; | 624 const int max_sockets_per_group_; |
| 646 | 625 |
| 647 // Whether to use timer to cleanup idle sockets. | |
| 648 bool use_cleanup_timer_; | |
| 649 | |
| 650 // The time to wait until closing idle sockets. | 626 // The time to wait until closing idle sockets. |
| 651 const base::TimeDelta unused_idle_socket_timeout_; | 627 const base::TimeDelta unused_idle_socket_timeout_; |
| 652 const base::TimeDelta used_idle_socket_timeout_; | 628 const base::TimeDelta used_idle_socket_timeout_; |
| 653 | 629 |
| 654 const std::unique_ptr<ConnectJobFactory> connect_job_factory_; | 630 const std::unique_ptr<ConnectJobFactory> connect_job_factory_; |
| 655 | 631 |
| 656 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool | 632 // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool |
| 657 bool connect_backup_jobs_enabled_; | 633 bool connect_backup_jobs_enabled_; |
| 658 | 634 |
| 659 // A unique id for the pool. It gets incremented every time we | 635 // A unique id for the pool. It gets incremented every time we |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 889 }; | 865 }; |
| 890 | 866 |
| 891 internal::ClientSocketPoolBaseHelper helper_; | 867 internal::ClientSocketPoolBaseHelper helper_; |
| 892 | 868 |
| 893 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 869 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 894 }; | 870 }; |
| 895 | 871 |
| 896 } // namespace net | 872 } // namespace net |
| 897 | 873 |
| 898 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 874 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| OLD | NEW |