[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 4 | // |
| 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. |
| 7 | // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle |
| 8 | // the core logic of (1) restricting the number of active (connected or |
| 9 | // connecting) sockets per "group" (generally speaking, the hostname), (2) |
| 10 | // maintaining a per-group list of idle, persistent sockets for reuse, and (3) |
| 11 | // limiting the total number of active sockets in the system. |
| 12 | // |
| 13 | // ClientSocketPoolBase abstracts socket connection details behind ConnectJob, |
| 14 | // ConnectJobFactory, and SocketParams. When a socket "slot" becomes available, |
| 15 | // the ClientSocketPoolBase will ask the ConnectJobFactory to create a |
| 16 | // ConnectJob with a SocketParams. Subclasses of ClientSocketPool should |
| 17 | // implement their socket specific connection by subclassing ConnectJob and |
| 18 | // implementing ConnectJob::ConnectInternal(). They can control the parameters |
| 19 | // passed to each new ConnectJob instance via their ConnectJobFactory subclass |
| 20 | // and templated SocketParams parameter. |
| 21 | // |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 22 | #ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
| 23 | #define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 24 | #pragma once |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 25 | |
| 26 | #include <deque> |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 27 | #include <list> |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 28 | #include <map> |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 29 | #include <set> |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 30 | #include <string> |
| 31 | |
| 32 | #include "base/basictypes.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 33 | #include "base/memory/ref_counted.h" |
| 34 | #include "base/memory/scoped_ptr.h" |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 35 | #include "base/task.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 36 | #include "base/time.h" |
| 37 | #include "base/timer.h" |
| 38 | #include "net/base/address_list.h" |
| 39 | #include "net/base/completion_callback.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 40 | #include "net/base/load_states.h" |
[email protected] | f898601 | 2011-05-19 21:53:58 | [diff] [blame] | 41 | #include "net/base/net_api.h" |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 42 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 43 | #include "net/base/net_log.h" |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 44 | #include "net/base/network_change_notifier.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 45 | #include "net/base/request_priority.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 46 | #include "net/socket/client_socket_pool.h" |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 47 | #include "net/socket/stream_socket.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 48 | |
| 49 | namespace net { |
| 50 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 51 | class ClientSocketHandle; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 52 | |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame^] | 53 | // Returns the client socket reuse policy. |
| 54 | int GetSocketReusePolicy(); |
| 55 | |
| 56 | // Sets the client socket reuse policy. |
| 57 | // NOTE: 'policy' should be a valid ClientSocketReusePolicy enum value. |
| 58 | NET_API void SetSocketReusePolicy(int policy); |
| 59 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 60 | // ConnectJob provides an abstract interface for "connecting" a socket. |
| 61 | // The connection may involve host resolution, tcp connection, ssl connection, |
| 62 | // etc. |
[email protected] | f898601 | 2011-05-19 21:53:58 | [diff] [blame] | 63 | class NET_TEST ConnectJob { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 64 | public: |
[email protected] | f898601 | 2011-05-19 21:53:58 | [diff] [blame] | 65 | class NET_TEST Delegate { |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 66 | public: |
| 67 | Delegate() {} |
| 68 | virtual ~Delegate() {} |
| 69 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 70 | // Alerts the delegate that the connection completed. |
| 71 | virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 75 | }; |
| 76 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 77 | // A |timeout_duration| of 0 corresponds to no timeout. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 78 | ConnectJob(const std::string& group_name, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 79 | base::TimeDelta timeout_duration, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 80 | Delegate* delegate, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 81 | const BoundNetLog& net_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 82 | virtual ~ConnectJob(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 83 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 84 | // Accessors |
| 85 | const std::string& group_name() const { return group_name_; } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 86 | const BoundNetLog& net_log() { return net_log_; } |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 87 | bool is_preconnect() const { return preconnect_state_ != NOT_PRECONNECT; } |
| 88 | bool is_unused_preconnect() const { |
| 89 | return preconnect_state_ == UNUSED_PRECONNECT; |
| 90 | } |
| 91 | |
| 92 | // Initialized by the ClientSocketPoolBaseHelper. |
| 93 | // TODO(willchan): Move most of the constructor arguments over here. We |
| 94 | // shouldn't give the ConnectJobFactory (subclasses) the ability to screw up |
| 95 | // the initialization. |
| 96 | void Initialize(bool is_preconnect); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 97 | |
[email protected] | 8e12ae0 | 2009-07-02 16:15:04 | [diff] [blame] | 98 | // Releases |socket_| to the client. On connection error, this should return |
| 99 | // NULL. |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 100 | StreamSocket* ReleaseSocket() { return socket_.release(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 101 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 102 | // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it |
| 103 | // cannot complete synchronously without blocking, or another net error code |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 104 | // on error. In asynchronous completion, the ConnectJob will notify |
| 105 | // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
| 106 | // completion, ReleaseSocket() can be called to acquire the connected socket |
| 107 | // if it succeeded. |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 108 | int Connect(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 109 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 110 | // Precondition: is_unused_preconnect() must be true. Marks the job as a |
| 111 | // used preconnect job. |
| 112 | void UseForNormalRequest(); |
| 113 | |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 114 | virtual LoadState GetLoadState() const = 0; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 115 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame] | 116 | // If Connect returns an error (or OnConnectJobComplete reports an error |
| 117 | // result) this method will be called, allowing the pool to add |
| 118 | // additional error state to the ClientSocketHandle (post late-binding). |
| 119 | virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} |
| 120 | |
[email protected] | 0496f9a3 | 2010-09-30 16:08:07 | [diff] [blame] | 121 | const BoundNetLog& net_log() const { return net_log_; } |
| 122 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 123 | protected: |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 124 | void set_socket(StreamSocket* socket); |
| 125 | StreamSocket* socket() { return socket_.get(); } |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 126 | void NotifyDelegateOfCompletion(int rv); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 127 | void ResetTimer(base::TimeDelta remainingTime); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 128 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 129 | private: |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 130 | enum PreconnectState { |
| 131 | NOT_PRECONNECT, |
| 132 | UNUSED_PRECONNECT, |
| 133 | USED_PRECONNECT, |
| 134 | }; |
| 135 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 136 | virtual int ConnectInternal() = 0; |
| 137 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 138 | void LogConnectStart(); |
| 139 | void LogConnectCompletion(int net_error); |
| 140 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 141 | // Alerts the delegate that the ConnectJob has timed out. |
| 142 | void OnTimeout(); |
| 143 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 144 | const std::string group_name_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 145 | const base::TimeDelta timeout_duration_; |
| 146 | // Timer to abort jobs that take too long. |
| 147 | base::OneShotTimer<ConnectJob> timer_; |
| 148 | Delegate* delegate_; |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 149 | scoped_ptr<StreamSocket> socket_; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 150 | BoundNetLog net_log_; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 151 | // A ConnectJob is idle until Connect() has been called. |
| 152 | bool idle_; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 153 | PreconnectState preconnect_state_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 154 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 155 | DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 156 | }; |
| 157 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 158 | namespace internal { |
| 159 | |
| 160 | // ClientSocketPoolBaseHelper is an internal class that implements almost all |
| 161 | // the functionality from ClientSocketPoolBase without using templates. |
| 162 | // ClientSocketPoolBase adds templated definitions built on top of |
| 163 | // ClientSocketPoolBaseHelper. This class is not for external use, please use |
| 164 | // ClientSocketPoolBase instead. |
[email protected] | f898601 | 2011-05-19 21:53:58 | [diff] [blame] | 165 | class NET_TEST ClientSocketPoolBaseHelper |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 166 | : public ConnectJob::Delegate, |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 167 | public NetworkChangeNotifier::IPAddressObserver { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 168 | public: |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 169 | typedef uint32 Flags; |
| 170 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 171 | // Used to specify specific behavior for the ClientSocketPool. |
| 172 | enum Flag { |
| 173 | NORMAL = 0, // Normal behavior. |
| 174 | NO_IDLE_SOCKETS = 0x1, // Do not return an idle socket. Create a new one. |
| 175 | }; |
| 176 | |
[email protected] | 5e6efa5 | 2011-06-27 17:26:41 | [diff] [blame^] | 177 | enum ClientSocketReusePolicy { |
| 178 | // Socket with largest amount of bytes transferred. |
| 179 | USE_WARMEST_SOCKET = 0, |
| 180 | |
| 181 | // Socket which scores highest on large bytes transferred and low idle time. |
| 182 | USE_WARM_SOCKET = 1, |
| 183 | |
| 184 | // Socket which was most recently used. |
| 185 | USE_LAST_ACCESSED_SOCKET = 2, |
| 186 | }; |
| 187 | |
[email protected] | f898601 | 2011-05-19 21:53:58 | [diff] [blame] | 188 | class NET_TEST Request { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 189 | public: |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 190 | Request(ClientSocketHandle* handle, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 191 | CompletionCallback* callback, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 192 | RequestPriority priority, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 193 | bool ignore_limits, |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 194 | Flags flags, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 195 | const BoundNetLog& net_log); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 196 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 197 | virtual ~Request(); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 198 | |
| 199 | ClientSocketHandle* handle() const { return handle_; } |
| 200 | CompletionCallback* callback() const { return callback_; } |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 201 | RequestPriority priority() const { return priority_; } |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 202 | bool ignore_limits() const { return ignore_limits_; } |
[email protected] | 6a166f81 | 2010-10-13 02:27:23 | [diff] [blame] | 203 | Flags flags() const { return flags_; } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 204 | const BoundNetLog& net_log() const { return net_log_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 205 | |
| 206 | private: |
| 207 | ClientSocketHandle* const handle_; |
| 208 | CompletionCallback* const callback_; |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 209 | const RequestPriority priority_; |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 210 | bool ignore_limits_; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 211 | const Flags flags_; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 212 | BoundNetLog net_log_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 213 | |
| 214 | DISALLOW_COPY_AND_ASSIGN(Request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 215 | }; |
| 216 | |
| 217 | class ConnectJobFactory { |
| 218 | public: |
| 219 | ConnectJobFactory() {} |
| 220 | virtual ~ConnectJobFactory() {} |
| 221 | |
| 222 | virtual ConnectJob* NewConnectJob( |
| 223 | const std::string& group_name, |
| 224 | const Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 225 | ConnectJob::Delegate* delegate) const = 0; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 226 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 227 | virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 228 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 229 | private: |
| 230 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 231 | }; |
| 232 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 233 | ClientSocketPoolBaseHelper( |
| 234 | int max_sockets, |
| 235 | int max_sockets_per_group, |
| 236 | base::TimeDelta unused_idle_socket_timeout, |
| 237 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 238 | ConnectJobFactory* connect_job_factory); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 239 | |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 240 | virtual ~ClientSocketPoolBaseHelper(); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 241 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 242 | // See ClientSocketPool::RequestSocket for documentation on this function. |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 243 | // ClientSocketPoolBaseHelper takes ownership of |request|, which must be |
| 244 | // heap allocated. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 245 | int RequestSocket(const std::string& group_name, const Request* request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 246 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 247 | // See ClientSocketPool::RequestSocket for documentation on this function. |
| 248 | void RequestSockets(const std::string& group_name, |
| 249 | const Request& request, |
| 250 | int num_sockets); |
| 251 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 252 | // See ClientSocketPool::CancelRequest for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 253 | void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 254 | ClientSocketHandle* handle); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 255 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 256 | // See ClientSocketPool::ReleaseSocket for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 257 | void ReleaseSocket(const std::string& group_name, |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 258 | StreamSocket* socket, |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 259 | int id); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 260 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 261 | // See ClientSocketPool::Flush for documentation on this function. |
| 262 | void Flush(); |
[email protected] | 241c5c2c | 2010-06-21 18:46:00 | [diff] [blame] | 263 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 264 | // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 265 | void CloseIdleSockets(); |
| 266 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 267 | // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 268 | int idle_socket_count() const { |
| 269 | return idle_socket_count_; |
| 270 | } |
| 271 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 272 | // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
| 273 | // function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 274 | int IdleSocketCountInGroup(const std::string& group_name) const; |
| 275 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 276 | // See ClientSocketPool::GetLoadState() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 277 | LoadState GetLoadState(const std::string& group_name, |
| 278 | const ClientSocketHandle* handle) const; |
| 279 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 280 | int ConnectRetryIntervalMs() const { |
| 281 | // TODO(mbelshe): Make this tuned dynamically based on measured RTT. |
| 282 | // For now, just use the max retry interval. |
| 283 | return ClientSocketPool::kMaxConnectRetryIntervalMs; |
| 284 | } |
| 285 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 286 | int NumConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 287 | return group_map_.find(group_name)->second->jobs().size(); |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 288 | } |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 289 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 290 | int NumActiveSocketsInGroup(const std::string& group_name) const { |
| 291 | return group_map_.find(group_name)->second->active_socket_count(); |
| 292 | } |
| 293 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 294 | bool HasGroup(const std::string& group_name) const; |
| 295 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 296 | // Closes all idle sockets if |force| is true. Else, only closes idle |
| 297 | // sockets that timed out or can't be reused. Made public for testing. |
| 298 | void CleanupIdleSockets(bool force); |
| 299 | |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 300 | // See ClientSocketPool::GetInfoAsValue for documentation on this function. |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 301 | DictionaryValue* GetInfoAsValue(const std::string& name, |
| 302 | const std::string& type) const; |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 303 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 304 | base::TimeDelta ConnectionTimeout() const { |
| 305 | return connect_job_factory_->ConnectionTimeout(); |
| 306 | } |
| 307 | |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 308 | static bool connect_backup_jobs_enabled(); |
[email protected] | 636b825 | 2011-04-08 19:56:54 | [diff] [blame] | 309 | static bool set_connect_backup_jobs_enabled(bool enabled); |
[email protected] | 2d672869 | 2011-03-12 01:39:55 | [diff] [blame] | 310 | |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 311 | void EnableConnectBackupJobs(); |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 312 | |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 313 | // ConnectJob::Delegate methods: |
| 314 | virtual void OnConnectJobComplete(int result, ConnectJob* job); |
| 315 | |
[email protected] | 232a581 | 2011-03-04 22:42:08 | [diff] [blame] | 316 | // NetworkChangeNotifier::IPAddressObserver methods: |
[email protected] | f48b943 | 2011-01-11 07:25:40 | [diff] [blame] | 317 | virtual void OnIPAddressChanged(); |
| 318 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 319 | private: |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 320 | friend class base::RefCounted<ClientSocketPoolBaseHelper>; |
| 321 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 322 | // Entry for a persistent socket which became idle at time |start_time|. |
| 323 | struct IdleSocket { |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 324 | IdleSocket() : socket(NULL) {} |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 325 | |
| 326 | // An idle socket should be removed if it can't be reused, or has been idle |
| 327 | // for too long. |now| is the current time value (TimeTicks::Now()). |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 328 | // |timeout| is the length of time to wait before timing out an idle socket. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 329 | // |
| 330 | // An idle socket can't be reused if it is disconnected or has received |
| 331 | // data unexpectedly (hence no longer idle). The unread data would be |
| 332 | // mistaken for the beginning of the next response if we were to reuse the |
| 333 | // socket for a new request. |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 334 | bool ShouldCleanup(base::TimeTicks now, base::TimeDelta timeout) const; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 335 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 336 | StreamSocket* socket; |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 337 | base::TimeTicks start_time; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 338 | }; |
| 339 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 340 | typedef std::deque<const Request* > RequestQueue; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 341 | typedef std::map<const ClientSocketHandle*, const Request*> RequestMap; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 342 | |
| 343 | // A Group is allocated per group_name when there are idle sockets or pending |
| 344 | // requests. Otherwise, the Group object is removed from the map. |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 345 | // |active_socket_count| tracks the number of sockets held by clients. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 346 | class Group { |
| 347 | public: |
| 348 | Group(); |
| 349 | ~Group(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 350 | |
| 351 | bool IsEmpty() const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 352 | return active_socket_count_ == 0 && idle_sockets_.empty() && |
| 353 | jobs_.empty() && pending_requests_.empty(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | bool HasAvailableSocketSlot(int max_sockets_per_group) const { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 357 | return NumActiveSocketSlots() < max_sockets_per_group; |
| 358 | } |
| 359 | |
| 360 | int NumActiveSocketSlots() const { |
| 361 | return active_socket_count_ + static_cast<int>(jobs_.size()) + |
| 362 | static_cast<int>(idle_sockets_.size()); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 363 | } |
| 364 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 365 | bool IsStalled(int max_sockets_per_group) const { |
| 366 | return HasAvailableSocketSlot(max_sockets_per_group) && |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 367 | pending_requests_.size() > jobs_.size(); |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 370 | RequestPriority TopPendingPriority() const { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 371 | return pending_requests_.front()->priority(); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 372 | } |
| 373 | |
[email protected] | e4d9a972 | 2011-05-12 00:16:00 | [diff] [blame] | 374 | bool HasBackupJob() const { return !method_factory_.empty(); } |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 375 | |
[email protected] | e4d9a972 | 2011-05-12 00:16:00 | [diff] [blame] | 376 | void CleanupBackupJob() { |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 377 | method_factory_.RevokeAll(); |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 378 | } |
| 379 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 380 | // Set a timer to create a backup socket if it takes too long to create one. |
| 381 | void StartBackupSocketTimer(const std::string& group_name, |
| 382 | ClientSocketPoolBaseHelper* pool); |
| 383 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 384 | // Searches |jobs_| to see if there's a preconnect ConnectJob, and if so, |
| 385 | // uses it. Returns true on success. Otherwise, returns false. |
| 386 | bool TryToUsePreconnectConnectJob(); |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 387 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 388 | void AddJob(ConnectJob* job) { jobs_.insert(job); } |
| 389 | void RemoveJob(ConnectJob* job) { jobs_.erase(job); } |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 390 | void RemoveAllJobs(); |
| 391 | |
| 392 | void IncrementActiveSocketCount() { active_socket_count_++; } |
| 393 | void DecrementActiveSocketCount() { active_socket_count_--; } |
| 394 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 395 | const std::set<ConnectJob*>& jobs() const { return jobs_; } |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 396 | const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; } |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 397 | const RequestQueue& pending_requests() const { return pending_requests_; } |
| 398 | int active_socket_count() const { return active_socket_count_; } |
| 399 | RequestQueue* mutable_pending_requests() { return &pending_requests_; } |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 400 | std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; } |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 401 | |
| 402 | private: |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 403 | // Called when the backup socket timer fires. |
| 404 | void OnBackupSocketTimerFired( |
| 405 | std::string group_name, |
| 406 | ClientSocketPoolBaseHelper* pool); |
| 407 | |
[email protected] | e1b54dc | 2010-10-06 21:27:22 | [diff] [blame] | 408 | std::list<IdleSocket> idle_sockets_; |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 409 | std::set<ConnectJob*> jobs_; |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 410 | RequestQueue pending_requests_; |
| 411 | int active_socket_count_; // number of active sockets used by clients |
| 412 | // A factory to pin the backup_job tasks. |
| 413 | ScopedRunnableMethodFactory<Group> method_factory_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 414 | }; |
| 415 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 416 | typedef std::map<std::string, Group*> GroupMap; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 417 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 418 | typedef std::set<ConnectJob*> ConnectJobSet; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 419 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 420 | struct CallbackResultPair { |
| 421 | CallbackResultPair() : callback(NULL), result(OK) {} |
| 422 | CallbackResultPair(CompletionCallback* callback_in, int result_in) |
| 423 | : callback(callback_in), result(result_in) {} |
| 424 | |
| 425 | CompletionCallback* callback; |
| 426 | int result; |
| 427 | }; |
| 428 | |
| 429 | typedef std::map<const ClientSocketHandle*, CallbackResultPair> |
| 430 | PendingCallbackMap; |
| 431 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 432 | static void InsertRequestIntoQueue(const Request* r, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 433 | RequestQueue* pending_requests); |
[email protected] | 417da10 | 2011-03-11 17:45:05 | [diff] [blame] | 434 | static const Request* RemoveRequestFromQueue(const RequestQueue::iterator& it, |
[email protected] | 3f00be8 | 2010-09-27 19:50:02 | [diff] [blame] | 435 | Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 436 | |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 437 | Group* GetOrCreateGroup(const std::string& group_name); |
| 438 | void RemoveGroup(const std::string& group_name); |
| 439 | void RemoveGroup(GroupMap::iterator it); |
| 440 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 441 | // Called when the number of idle sockets changes. |
| 442 | void IncrementIdleCount(); |
| 443 | void DecrementIdleCount(); |
| 444 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 445 | // Scans the group map for groups which have an available socket slot and |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 446 | // at least one pending request. Returns true if any groups are stalled, and |
| 447 | // if so, fills |group| and |group_name| with data of the stalled group |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 448 | // having highest priority. |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 449 | bool FindTopStalledGroup(Group** group, std::string* group_name); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 450 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 451 | // Called when timer_ fires. This method scans the idle sockets removing |
| 452 | // sockets that timed out or can't be reused. |
| 453 | void OnCleanupTimerFired() { |
| 454 | CleanupIdleSockets(false); |
| 455 | } |
| 456 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 457 | // Removes |job| from |connect_job_set_|. Also updates |group| if non-NULL. |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 458 | void RemoveConnectJob(ConnectJob* job, Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 459 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 460 | // Tries to see if we can handle any more requests for |group|. |
| 461 | void OnAvailableSocketSlot(const std::string& group_name, Group* group); |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 462 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 463 | // Process a pending socket request for a group. |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 464 | void ProcessPendingRequest(const std::string& group_name, Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 465 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 466 | // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 467 | void HandOutSocket(StreamSocket* socket, |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 468 | bool reused, |
| 469 | ClientSocketHandle* handle, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 470 | base::TimeDelta time_idle, |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 471 | Group* group, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 472 | const BoundNetLog& net_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 473 | |
[email protected] | 0f873e8 | 2010-09-02 16:09:01 | [diff] [blame] | 474 | // Adds |socket| to the list of idle sockets for |group|. |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 475 | void AddIdleSocket(StreamSocket* socket, Group* group); |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 476 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 477 | // Iterates through |group_map_|, canceling all ConnectJobs and deleting |
| 478 | // groups if they are no longer needed. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 479 | void CancelAllConnectJobs(); |
| 480 | |
[email protected] | 06f9246 | 2010-08-31 19:24:14 | [diff] [blame] | 481 | // Iterates through |group_map_|, posting ERR_ABORTED callbacks for all |
| 482 | // requests, and then deleting groups if they are no longer needed. |
| 483 | void AbortAllRequests(); |
| 484 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 485 | // Returns true if we can't create any more sockets due to the total limit. |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 486 | bool ReachedMaxSocketsLimit() const; |
| 487 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 488 | // This is the internal implementation of RequestSocket(). It differs in that |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 489 | // it does not handle logging into NetLog of the queueing status of |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 490 | // |request|. |
| 491 | int RequestSocketInternal(const std::string& group_name, |
| 492 | const Request* request); |
| 493 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 494 | // Assigns an idle socket for the group to the request. |
| 495 | // Returns |true| if an idle socket is available, false otherwise. |
[email protected] | aed99ef0 | 2010-08-26 14:04:32 | [diff] [blame] | 496 | bool AssignIdleSocketToGroup(const Request* request, Group* group); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 497 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 498 | static void LogBoundConnectJobToRequest( |
| 499 | const NetLog::Source& connect_job_source, const Request* request); |
| 500 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 501 | // Closes one idle socket. Picks the first one encountered. |
| 502 | // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 503 | // should keep an ordered list of idle sockets, and close them in order. |
| 504 | // Requires maintaining more state. It's not clear if it's worth it since |
| 505 | // I'm not sure if we hit this situation often. |
| 506 | void CloseOneIdleSocket(); |
| 507 | |
[email protected] | dcbe168a | 2010-12-02 03:14:46 | [diff] [blame] | 508 | // Same as CloseOneIdleSocket() except it won't close an idle socket in |
| 509 | // |group|. If |group| is NULL, it is ignored. Returns true if it closed a |
| 510 | // socket. |
| 511 | bool CloseOneIdleSocketExceptInGroup(const Group* group); |
| 512 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 513 | // Checks if there are stalled socket groups that should be notified |
| 514 | // for possible wakeup. |
| 515 | void CheckForStalledSocketGroups(); |
| 516 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 517 | // Posts a task to call InvokeUserCallback() on the next iteration through the |
| 518 | // current message loop. Inserts |callback| into |pending_callback_map_|, |
| 519 | // keyed by |handle|. |
| 520 | void InvokeUserCallbackLater( |
| 521 | ClientSocketHandle* handle, CompletionCallback* callback, int rv); |
| 522 | |
| 523 | // Invokes the user callback for |handle|. By the time this task has run, |
| 524 | // it's possible that the request has been cancelled, so |handle| may not |
| 525 | // exist in |pending_callback_map_|. We look up the callback and result code |
| 526 | // in |pending_callback_map_|. |
| 527 | void InvokeUserCallback(ClientSocketHandle* handle); |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 528 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 529 | GroupMap group_map_; |
| 530 | |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 531 | // Map of the ClientSocketHandles for which we have a pending Task to invoke a |
| 532 | // callback. This is necessary since, before we invoke said callback, it's |
| 533 | // possible that the request is cancelled. |
| 534 | PendingCallbackMap pending_callback_map_; |
| 535 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 536 | // Timer used to periodically prune idle sockets that timed out or can't be |
| 537 | // reused. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 538 | base::RepeatingTimer<ClientSocketPoolBaseHelper> timer_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 539 | |
| 540 | // The total number of idle sockets in the system. |
| 541 | int idle_socket_count_; |
| 542 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 543 | // Number of connecting sockets across all groups. |
| 544 | int connecting_socket_count_; |
| 545 | |
| 546 | // Number of connected sockets we handed out across all groups. |
| 547 | int handed_out_socket_count_; |
| 548 | |
| 549 | // The maximum total number of sockets. See ReachedMaxSocketsLimit. |
| 550 | const int max_sockets_; |
| 551 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 552 | // The maximum number of sockets kept per group. |
| 553 | const int max_sockets_per_group_; |
| 554 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 555 | // The time to wait until closing idle sockets. |
| 556 | const base::TimeDelta unused_idle_socket_timeout_; |
| 557 | const base::TimeDelta used_idle_socket_timeout_; |
| 558 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 559 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 560 | |
[email protected] | ab73904 | 2011-04-07 15:22:28 | [diff] [blame] | 561 | // TODO(vandebo) Remove when backup jobs move to TransportClientSocketPool |
[email protected] | 06d9404 | 2010-08-25 01:45:22 | [diff] [blame] | 562 | bool connect_backup_jobs_enabled_; |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 563 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 564 | // A unique id for the pool. It gets incremented every time we Flush() the |
| 565 | // pool. This is so that when sockets get released back to the pool, we can |
| 566 | // make sure that they are discarded rather than reused. |
| 567 | int pool_generation_number_; |
[email protected] | 09d6ecb0 | 2010-07-22 20:10:45 | [diff] [blame] | 568 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 569 | ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; |
| 570 | |
[email protected] | 3598c602 | 2010-09-17 23:13:09 | [diff] [blame] | 571 | DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 572 | }; |
| 573 | |
| 574 | } // namespace internal |
| 575 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 576 | // The maximum duration, in seconds, to keep used idle persistent sockets alive. |
| 577 | static const int kUsedIdleSocketTimeout = 300; // 5 minutes |
| 578 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 579 | template <typename SocketParams> |
| 580 | class ClientSocketPoolBase { |
| 581 | public: |
| 582 | class Request : public internal::ClientSocketPoolBaseHelper::Request { |
| 583 | public: |
| 584 | Request(ClientSocketHandle* handle, |
| 585 | CompletionCallback* callback, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 586 | RequestPriority priority, |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 587 | internal::ClientSocketPoolBaseHelper::Flags flags, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 588 | bool ignore_limits, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 589 | const scoped_refptr<SocketParams>& params, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 590 | const BoundNetLog& net_log) |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 591 | : internal::ClientSocketPoolBaseHelper::Request( |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 592 | handle, callback, priority, ignore_limits, flags, net_log), |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 593 | params_(params) {} |
| 594 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 595 | const scoped_refptr<SocketParams>& params() const { return params_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 596 | |
| 597 | private: |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 598 | const scoped_refptr<SocketParams> params_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 599 | }; |
| 600 | |
| 601 | class ConnectJobFactory { |
| 602 | public: |
| 603 | ConnectJobFactory() {} |
| 604 | virtual ~ConnectJobFactory() {} |
| 605 | |
| 606 | virtual ConnectJob* NewConnectJob( |
| 607 | const std::string& group_name, |
| 608 | const Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 609 | ConnectJob::Delegate* delegate) const = 0; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 610 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 611 | virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 612 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 613 | private: |
| 614 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 615 | }; |
| 616 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 617 | // |max_sockets| is the maximum number of sockets to be maintained by this |
| 618 | // ClientSocketPool. |max_sockets_per_group| specifies the maximum number of |
| 619 | // sockets a "group" can have. |unused_idle_socket_timeout| specifies how |
| 620 | // long to leave an unused idle socket open before closing it. |
| 621 | // |used_idle_socket_timeout| specifies how long to leave a previously used |
| 622 | // idle socket open before closing it. |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 623 | ClientSocketPoolBase( |
| 624 | int max_sockets, |
| 625 | int max_sockets_per_group, |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 626 | ClientSocketPoolHistograms* histograms, |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 627 | base::TimeDelta unused_idle_socket_timeout, |
| 628 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 629 | ConnectJobFactory* connect_job_factory) |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 630 | : histograms_(histograms), |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 631 | helper_(max_sockets, max_sockets_per_group, |
| 632 | unused_idle_socket_timeout, used_idle_socket_timeout, |
| 633 | new ConnectJobFactoryAdaptor(connect_job_factory)) {} |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 634 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 635 | virtual ~ClientSocketPoolBase() {} |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 636 | |
| 637 | // These member functions simply forward to ClientSocketPoolBaseHelper. |
| 638 | |
| 639 | // RequestSocket bundles up the parameters into a Request and then forwards to |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 640 | // ClientSocketPoolBaseHelper::RequestSocket(). |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 641 | int RequestSocket(const std::string& group_name, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 642 | const scoped_refptr<SocketParams>& params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 643 | RequestPriority priority, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 644 | ClientSocketHandle* handle, |
| 645 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 646 | const BoundNetLog& net_log) { |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 647 | Request* request = |
| 648 | new Request(handle, callback, priority, |
| 649 | internal::ClientSocketPoolBaseHelper::NORMAL, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 650 | params->ignore_limits(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 651 | params, net_log); |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 652 | return helper_.RequestSocket(group_name, request); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 653 | } |
| 654 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 655 | // RequestSockets bundles up the parameters into a Request and then forwards |
| 656 | // to ClientSocketPoolBaseHelper::RequestSockets(). Note that it assigns the |
| 657 | // priority to LOWEST and specifies the NO_IDLE_SOCKETS flag. |
| 658 | void RequestSockets(const std::string& group_name, |
| 659 | const scoped_refptr<SocketParams>& params, |
| 660 | int num_sockets, |
| 661 | const BoundNetLog& net_log) { |
| 662 | const Request request(NULL /* no handle */, |
| 663 | NULL /* no callback */, |
| 664 | LOWEST, |
| 665 | internal::ClientSocketPoolBaseHelper::NO_IDLE_SOCKETS, |
[email protected] | 5acdce1 | 2011-03-30 13:00:20 | [diff] [blame] | 666 | params->ignore_limits(), |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 667 | params, |
| 668 | net_log); |
| 669 | helper_.RequestSockets(group_name, request, num_sockets); |
| 670 | } |
| 671 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 672 | void CancelRequest(const std::string& group_name, |
[email protected] | 05ea9ff | 2010-07-15 19:08:21 | [diff] [blame] | 673 | ClientSocketHandle* handle) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 674 | return helper_.CancelRequest(group_name, handle); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 675 | } |
| 676 | |
[email protected] | 3268023f | 2011-05-05 00:08:10 | [diff] [blame] | 677 | void ReleaseSocket(const std::string& group_name, StreamSocket* socket, |
[email protected] | 241c5c2c | 2010-06-21 18:46:00 | [diff] [blame] | 678 | int id) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 679 | return helper_.ReleaseSocket(group_name, socket, id); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 680 | } |
| 681 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 682 | void CloseIdleSockets() { return helper_.CloseIdleSockets(); } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 683 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 684 | int idle_socket_count() const { return helper_.idle_socket_count(); } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 685 | |
| 686 | int IdleSocketCountInGroup(const std::string& group_name) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 687 | return helper_.IdleSocketCountInGroup(group_name); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | LoadState GetLoadState(const std::string& group_name, |
| 691 | const ClientSocketHandle* handle) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 692 | return helper_.GetLoadState(group_name, handle); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | virtual void OnConnectJobComplete(int result, ConnectJob* job) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 696 | return helper_.OnConnectJobComplete(result, job); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 697 | } |
| 698 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 699 | int NumConnectJobsInGroup(const std::string& group_name) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 700 | return helper_.NumConnectJobsInGroup(group_name); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 701 | } |
| 702 | |
[email protected] | 2c2bef15 | 2010-10-13 00:55:03 | [diff] [blame] | 703 | int NumActiveSocketsInGroup(const std::string& group_name) const { |
| 704 | return helper_.NumActiveSocketsInGroup(group_name); |
| 705 | } |
| 706 | |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 707 | bool HasGroup(const std::string& group_name) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 708 | return helper_.HasGroup(group_name); |
[email protected] | 2abfe90a | 2010-08-25 17:49:51 | [diff] [blame] | 709 | } |
| 710 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 711 | void CleanupIdleSockets(bool force) { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 712 | return helper_.CleanupIdleSockets(force); |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 713 | } |
| 714 | |
[email protected] | ba00b49 | 2010-09-08 14:53:38 | [diff] [blame] | 715 | DictionaryValue* GetInfoAsValue(const std::string& name, |
| 716 | const std::string& type) const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 717 | return helper_.GetInfoAsValue(name, type); |
[email protected] | 59d7a5a | 2010-08-30 16:44:27 | [diff] [blame] | 718 | } |
| 719 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 720 | base::TimeDelta ConnectionTimeout() const { |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 721 | return helper_.ConnectionTimeout(); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 722 | } |
| 723 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 724 | ClientSocketPoolHistograms* histograms() const { |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 725 | return histograms_; |
| 726 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 727 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 728 | void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); } |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 729 | |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 730 | void Flush() { helper_.Flush(); } |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 731 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 732 | private: |
| 733 | // This adaptor class exists to bridge the |
| 734 | // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
| 735 | // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
| 736 | // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
| 737 | // static_cast themselves. |
| 738 | class ConnectJobFactoryAdaptor |
| 739 | : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| 740 | public: |
| 741 | typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory |
| 742 | ConnectJobFactory; |
| 743 | |
[email protected] | 9008c86f | 2010-08-06 07:10:24 | [diff] [blame] | 744 | explicit ConnectJobFactoryAdaptor(ConnectJobFactory* connect_job_factory) |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 745 | : connect_job_factory_(connect_job_factory) {} |
| 746 | virtual ~ConnectJobFactoryAdaptor() {} |
| 747 | |
| 748 | virtual ConnectJob* NewConnectJob( |
| 749 | const std::string& group_name, |
| 750 | const internal::ClientSocketPoolBaseHelper::Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 751 | ConnectJob::Delegate* delegate) const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 752 | const Request* casted_request = static_cast<const Request*>(&request); |
| 753 | return connect_job_factory_->NewConnectJob( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 754 | group_name, *casted_request, delegate); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 755 | } |
| 756 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 757 | virtual base::TimeDelta ConnectionTimeout() const { |
| 758 | return connect_job_factory_->ConnectionTimeout(); |
| 759 | } |
| 760 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 761 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
| 762 | }; |
| 763 | |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 764 | // Histograms for the pool |
[email protected] | 2431756e | 2010-09-29 20:26:13 | [diff] [blame] | 765 | ClientSocketPoolHistograms* const histograms_; |
| 766 | internal::ClientSocketPoolBaseHelper helper_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 767 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 768 | DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 769 | }; |
| 770 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 771 | } // namespace net |
| 772 | |
| 773 | #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |