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