| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef NET_BASE_CLIENT_SOCKET_POOL_H_ | 5 #ifndef NET_BASE_CLIENT_SOCKET_POOL_H_ |
| 6 #define NET_BASE_CLIENT_SOCKET_POOL_H_ | 6 #define NET_BASE_CLIENT_SOCKET_POOL_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 // | 28 // |
| 29 class ClientSocketPool : public base::RefCounted<ClientSocketPool> { | 29 class ClientSocketPool : public base::RefCounted<ClientSocketPool> { |
| 30 public: | 30 public: |
| 31 explicit ClientSocketPool(int max_sockets_per_group); | 31 explicit ClientSocketPool(int max_sockets_per_group); |
| 32 | 32 |
| 33 // Called to request a socket for the given handle. There are three possible | 33 // Called to request a socket for the given handle. There are three possible |
| 34 // results: 1) the handle will be initialized with a socket to reuse, 2) the | 34 // results: 1) the handle will be initialized with a socket to reuse, 2) the |
| 35 // handle will be initialized without a socket such that the consumer needs | 35 // handle will be initialized without a socket such that the consumer needs |
| 36 // to supply a socket, or 3) the handle will be added to a wait list until a | 36 // to supply a socket, or 3) the handle will be added to a wait list until a |
| 37 // socket is available to reuse or the opportunity to create a new socket | 37 // socket is available to reuse or the opportunity to create a new socket |
| 38 // arises. The completion callback is notified in the 3rd case. | 38 // arises. The completion callback is notified in the 3rd case. |priority| |
| 39 // will determine the placement into the wait list. |
| 39 // | 40 // |
| 40 // If this function returns OK, then |handle| is initialized upon return. | 41 // If this function returns OK, then |handle| is initialized upon return. |
| 41 // The |handle|'s is_initialized method will return true in this case. If a | 42 // The |handle|'s is_initialized method will return true in this case. If a |
| 42 // ClientSocket was reused, then |handle|'s socket member will be non-NULL. | 43 // ClientSocket was reused, then |handle|'s socket member will be non-NULL. |
| 43 // Otherwise, the consumer will need to supply |handle| with a socket by | 44 // Otherwise, the consumer will need to supply |handle| with a socket by |
| 44 // allocating a new ClientSocket object and calling the |handle|'s set_socket | 45 // allocating a new ClientSocket object and calling the |handle|'s set_socket |
| 45 // method. | 46 // method. |
| 46 // | 47 // |
| 47 // If ERR_IO_PENDING is returned, then the completion callback will be called | 48 // If ERR_IO_PENDING is returned, then the completion callback will be called |
| 48 // when |handle| has been initialized. | 49 // when |handle| has been initialized. |
| 49 // | 50 // |
| 50 int RequestSocket(ClientSocketHandle* handle, CompletionCallback* callback); | 51 int RequestSocket(ClientSocketHandle* handle, |
| 52 int priority, |
| 53 CompletionCallback* callback); |
| 51 | 54 |
| 52 // Called to cancel a RequestSocket call that returned ERR_IO_PENDING. The | 55 // Called to cancel a RequestSocket call that returned ERR_IO_PENDING. The |
| 53 // same handle parameter must be passed to this method as was passed to the | 56 // same handle parameter must be passed to this method as was passed to the |
| 54 // RequestSocket call being cancelled. The associated CompletionCallback is | 57 // RequestSocket call being cancelled. The associated CompletionCallback is |
| 55 // not run. | 58 // not run. |
| 56 void CancelRequest(ClientSocketHandle* handle); | 59 void CancelRequest(ClientSocketHandle* handle); |
| 57 | 60 |
| 58 // Called to release the socket member of an initialized ClientSocketHandle | 61 // Called to release the socket member of an initialized ClientSocketHandle |
| 59 // once the socket is no longer needed. If the socket member is non-null and | 62 // once the socket is no longer needed. If the socket member is non-null and |
| 60 // still has an established connection, then it will be added to the idle set | 63 // still has an established connection, then it will be added to the idle set |
| 61 // of sockets to be used to satisfy future RequestSocket calls. Otherwise, | 64 // of sockets to be used to satisfy future RequestSocket calls. Otherwise, |
| 62 // the ClientSocket is destroyed. | 65 // the ClientSocket is destroyed. |
| 63 void ReleaseSocket(ClientSocketHandle* handle); | 66 void ReleaseSocket(ClientSocketHandle* handle); |
| 64 | 67 |
| 65 // Called to close any idle connections held by the connection manager. | 68 // Called to close any idle connections held by the connection manager. |
| 66 void CloseIdleSockets(); | 69 void CloseIdleSockets(); |
| 67 | 70 |
| 68 // The total number of idle sockets in the pool. | 71 // The total number of idle sockets in the pool. |
| 69 int idle_socket_count() const { | 72 int idle_socket_count() const { |
| 70 return idle_socket_count_; | 73 return idle_socket_count_; |
| 71 } | 74 } |
| 72 | 75 |
| 73 private: | 76 private: |
| 74 friend class base::RefCounted<ClientSocketPool>; | 77 friend class base::RefCounted<ClientSocketPool>; |
| 75 | 78 |
| 76 typedef scoped_ptr<ClientSocket> ClientSocketPtr; | 79 typedef scoped_ptr<ClientSocket> ClientSocketPtr; |
| 77 | 80 |
| 78 ~ClientSocketPool(); | |
| 79 | |
| 80 // Closes all idle sockets if |force| is true. Else, only closes idle | |
| 81 // sockets that timed out or can't be reused. | |
| 82 void CleanupIdleSockets(bool force); | |
| 83 | |
| 84 // Called when the number of idle sockets changes. | |
| 85 void IncrementIdleCount(); | |
| 86 void DecrementIdleCount(); | |
| 87 | |
| 88 // Called via PostTask by ReleaseSocket. | |
| 89 void DoReleaseSocket(const std::string& group_name, ClientSocketPtr* ptr); | |
| 90 | |
| 91 // Called when timer_ fires. This method scans the idle sockets removing | |
| 92 // sockets that timed out or can't be reused. | |
| 93 void OnCleanupTimerFired() { | |
| 94 CleanupIdleSockets(false); | |
| 95 } | |
| 96 | |
| 97 // A Request is allocated per call to RequestSocket that results in | 81 // A Request is allocated per call to RequestSocket that results in |
| 98 // ERR_IO_PENDING. | 82 // ERR_IO_PENDING. |
| 99 struct Request { | 83 struct Request { |
| 100 ClientSocketHandle* handle; | 84 ClientSocketHandle* handle; |
| 101 CompletionCallback* callback; | 85 CompletionCallback* callback; |
| 86 int priority; |
| 102 }; | 87 }; |
| 103 | 88 |
| 104 // Entry for a persistent socket which became idle at time |start_time|. | 89 // Entry for a persistent socket which became idle at time |start_time|. |
| 105 struct IdleSocket { | 90 struct IdleSocket { |
| 106 ClientSocketPtr* ptr; | 91 ClientSocketPtr* ptr; |
| 107 base::TimeTicks start_time; | 92 base::TimeTicks start_time; |
| 108 | 93 |
| 109 // An idle socket should be removed if it can't be reused, or has been idle | 94 // An idle socket should be removed if it can't be reused, or has been idle |
| 110 // for too long. |now| is the current time value (TimeTicks::Now()). | 95 // for too long. |now| is the current time value (TimeTicks::Now()). |
| 111 // | 96 // |
| 112 // An idle socket can't be reused if it is disconnected or has received | 97 // An idle socket can't be reused if it is disconnected or has received |
| 113 // data unexpectedly (hence no longer idle). The unread data would be | 98 // data unexpectedly (hence no longer idle). The unread data would be |
| 114 // mistaken for the beginning of the next response if we were to reuse the | 99 // mistaken for the beginning of the next response if we were to reuse the |
| 115 // socket for a new request. | 100 // socket for a new request. |
| 116 bool ShouldCleanup(base::TimeTicks now) const; | 101 bool ShouldCleanup(base::TimeTicks now) const; |
| 117 }; | 102 }; |
| 118 | 103 |
| 104 typedef std::deque<Request> RequestQueue; |
| 105 |
| 119 // A Group is allocated per group_name when there are idle sockets or pending | 106 // A Group is allocated per group_name when there are idle sockets or pending |
| 120 // requests. Otherwise, the Group object is removed from the map. | 107 // requests. Otherwise, the Group object is removed from the map. |
| 121 struct Group { | 108 struct Group { |
| 122 Group() : active_socket_count(0) {} | 109 Group() : active_socket_count(0) {} |
| 123 std::deque<IdleSocket> idle_sockets; | 110 std::deque<IdleSocket> idle_sockets; |
| 124 std::deque<Request> pending_requests; | 111 RequestQueue pending_requests; |
| 125 int active_socket_count; | 112 int active_socket_count; |
| 126 }; | 113 }; |
| 127 | 114 |
| 128 typedef std::map<std::string, Group> GroupMap; | 115 typedef std::map<std::string, Group> GroupMap; |
| 116 |
| 117 ~ClientSocketPool(); |
| 118 |
| 119 static void InsertRequestIntoQueue(const Request& r, |
| 120 RequestQueue* pending_requests); |
| 121 |
| 122 // Closes all idle sockets if |force| is true. Else, only closes idle |
| 123 // sockets that timed out or can't be reused. |
| 124 void CleanupIdleSockets(bool force); |
| 125 |
| 126 // Called when the number of idle sockets changes. |
| 127 void IncrementIdleCount(); |
| 128 void DecrementIdleCount(); |
| 129 |
| 130 // Called via PostTask by ReleaseSocket. |
| 131 void DoReleaseSocket(const std::string& group_name, ClientSocketPtr* ptr); |
| 132 |
| 133 // Called when timer_ fires. This method scans the idle sockets removing |
| 134 // sockets that timed out or can't be reused. |
| 135 void OnCleanupTimerFired() { |
| 136 CleanupIdleSockets(false); |
| 137 } |
| 138 |
| 129 GroupMap group_map_; | 139 GroupMap group_map_; |
| 130 | 140 |
| 131 // Timer used to periodically prune idle sockets that timed out or can't be | 141 // Timer used to periodically prune idle sockets that timed out or can't be |
| 132 // reused. | 142 // reused. |
| 133 base::RepeatingTimer<ClientSocketPool> timer_; | 143 base::RepeatingTimer<ClientSocketPool> timer_; |
| 134 | 144 |
| 135 // The total number of idle sockets in the system. | 145 // The total number of idle sockets in the system. |
| 136 int idle_socket_count_; | 146 int idle_socket_count_; |
| 137 | 147 |
| 138 // The maximum number of sockets kept per group. | 148 // The maximum number of sockets kept per group. |
| 139 int max_sockets_per_group_; | 149 int max_sockets_per_group_; |
| 140 | 150 |
| 141 DISALLOW_COPY_AND_ASSIGN(ClientSocketPool); | 151 DISALLOW_COPY_AND_ASSIGN(ClientSocketPool); |
| 142 }; | 152 }; |
| 143 | 153 |
| 144 } // namespace net | 154 } // namespace net |
| 145 | 155 |
| 146 #endif // NET_BASE_CLIENT_SOCKET_POOL_H_ | 156 #endif // NET_BASE_CLIENT_SOCKET_POOL_H_ |
| OLD | NEW |