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