[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 1 | // Copyright (c) 2010 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_ |
| 24 | |
| 25 | #include <deque> |
| 26 | #include <map> |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 27 | #include <set> |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 28 | #include <string> |
| 29 | |
| 30 | #include "base/basictypes.h" |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 31 | #include "base/compiler_specific.h" |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 32 | #include "base/ref_counted.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 33 | #include "base/scoped_ptr.h" |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 34 | #include "base/task.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 35 | #include "base/time.h" |
| 36 | #include "base/timer.h" |
| 37 | #include "net/base/address_list.h" |
| 38 | #include "net/base/completion_callback.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 39 | #include "net/base/load_states.h" |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 40 | #include "net/base/net_errors.h" |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 41 | #include "net/base/net_log.h" |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 42 | #include "net/base/network_change_notifier.h" |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 43 | #include "net/base/request_priority.h" |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 44 | #include "net/socket/client_socket.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 45 | #include "net/socket/client_socket_pool.h" |
| 46 | |
| 47 | namespace net { |
| 48 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 49 | class ClientSocketHandle; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 50 | |
| 51 | // ConnectJob provides an abstract interface for "connecting" a socket. |
| 52 | // The connection may involve host resolution, tcp connection, ssl connection, |
| 53 | // etc. |
| 54 | class ConnectJob { |
| 55 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 56 | class Delegate { |
| 57 | public: |
| 58 | Delegate() {} |
| 59 | virtual ~Delegate() {} |
| 60 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 61 | // Alerts the delegate that the connection completed. |
| 62 | virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 63 | |
| 64 | private: |
| 65 | DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 66 | }; |
| 67 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 68 | // A |timeout_duration| of 0 corresponds to no timeout. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 69 | ConnectJob(const std::string& group_name, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 70 | base::TimeDelta timeout_duration, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 71 | Delegate* delegate, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 72 | const BoundNetLog& net_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 73 | virtual ~ConnectJob(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 74 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 75 | // Accessors |
| 76 | const std::string& group_name() const { return group_name_; } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 77 | const BoundNetLog& net_log() { return net_log_; } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 78 | |
[email protected] | 8e12ae0 | 2009-07-02 16:15:04 | [diff] [blame] | 79 | // Releases |socket_| to the client. On connection error, this should return |
| 80 | // NULL. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 81 | ClientSocket* ReleaseSocket() { return socket_.release(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 82 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 83 | // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it |
| 84 | // cannot complete synchronously without blocking, or another net error code |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 85 | // on error. In asynchronous completion, the ConnectJob will notify |
| 86 | // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
| 87 | // completion, ReleaseSocket() can be called to acquire the connected socket |
| 88 | // if it succeeded. |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 89 | int Connect(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 90 | |
[email protected] | 4645135 | 2009-09-01 14:54:21 | [diff] [blame] | 91 | virtual LoadState GetLoadState() const = 0; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 92 | |
[email protected] | e60e47a | 2010-07-14 03:37:18 | [diff] [blame^] | 93 | // If Connect returns an error (or OnConnectJobComplete reports an error |
| 94 | // result) this method will be called, allowing the pool to add |
| 95 | // additional error state to the ClientSocketHandle (post late-binding). |
| 96 | virtual void GetAdditionalErrorState(ClientSocketHandle* handle) {} |
| 97 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 98 | protected: |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 99 | void set_socket(ClientSocket* socket); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 100 | ClientSocket* socket() { return socket_.get(); } |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 101 | void NotifyDelegateOfCompletion(int rv); |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 102 | void ResetTimer(base::TimeDelta remainingTime); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 103 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 104 | private: |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 105 | virtual int ConnectInternal() = 0; |
| 106 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 107 | void LogConnectStart(); |
| 108 | void LogConnectCompletion(int net_error); |
| 109 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 110 | // Alerts the delegate that the ConnectJob has timed out. |
| 111 | void OnTimeout(); |
| 112 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 113 | const std::string group_name_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 114 | const base::TimeDelta timeout_duration_; |
| 115 | // Timer to abort jobs that take too long. |
| 116 | base::OneShotTimer<ConnectJob> timer_; |
| 117 | Delegate* delegate_; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 118 | scoped_ptr<ClientSocket> socket_; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 119 | BoundNetLog net_log_; |
[email protected] | a2006ece | 2010-04-23 16:44:02 | [diff] [blame] | 120 | // A ConnectJob is idle until Connect() has been called. |
| 121 | bool idle_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 122 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 123 | DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 124 | }; |
| 125 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 126 | namespace internal { |
| 127 | |
| 128 | // ClientSocketPoolBaseHelper is an internal class that implements almost all |
| 129 | // the functionality from ClientSocketPoolBase without using templates. |
| 130 | // ClientSocketPoolBase adds templated definitions built on top of |
| 131 | // ClientSocketPoolBaseHelper. This class is not for external use, please use |
| 132 | // ClientSocketPoolBase instead. |
| 133 | class ClientSocketPoolBaseHelper |
| 134 | : public base::RefCounted<ClientSocketPoolBaseHelper>, |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 135 | public ConnectJob::Delegate, |
| 136 | public NetworkChangeNotifier::Observer { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 137 | public: |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 138 | class Request { |
| 139 | public: |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 140 | Request(ClientSocketHandle* handle, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 141 | CompletionCallback* callback, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 142 | RequestPriority priority, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 143 | const BoundNetLog& net_log); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 144 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 145 | virtual ~Request(); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 146 | |
| 147 | ClientSocketHandle* handle() const { return handle_; } |
| 148 | CompletionCallback* callback() const { return callback_; } |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 149 | RequestPriority priority() const { return priority_; } |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 150 | const BoundNetLog& net_log() const { return net_log_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 151 | |
| 152 | private: |
| 153 | ClientSocketHandle* const handle_; |
| 154 | CompletionCallback* const callback_; |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 155 | const RequestPriority priority_; |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 156 | BoundNetLog net_log_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 157 | |
| 158 | DISALLOW_COPY_AND_ASSIGN(Request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | class ConnectJobFactory { |
| 162 | public: |
| 163 | ConnectJobFactory() {} |
| 164 | virtual ~ConnectJobFactory() {} |
| 165 | |
| 166 | virtual ConnectJob* NewConnectJob( |
| 167 | const std::string& group_name, |
| 168 | const Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 169 | ConnectJob::Delegate* delegate) const = 0; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 170 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 171 | virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 172 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 173 | private: |
| 174 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 175 | }; |
| 176 | |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 177 | ClientSocketPoolBaseHelper( |
| 178 | int max_sockets, |
| 179 | int max_sockets_per_group, |
| 180 | base::TimeDelta unused_idle_socket_timeout, |
| 181 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 182 | ConnectJobFactory* connect_job_factory); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 183 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 184 | // See ClientSocketPool::RequestSocket for documentation on this function. |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 185 | // ClientSocketPoolBaseHelper takes ownership of |request|, which must be |
| 186 | // heap allocated. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 187 | int RequestSocket(const std::string& group_name, const Request* request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 188 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 189 | // See ClientSocketPool::CancelRequest for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 190 | void CancelRequest(const std::string& group_name, |
| 191 | const ClientSocketHandle* handle); |
| 192 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 193 | // See ClientSocketPool::ReleaseSocket for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 194 | void ReleaseSocket(const std::string& group_name, |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 195 | ClientSocket* socket, |
| 196 | int id); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 197 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 198 | // See ClientSocketPool::Flush for documentation on this function. |
| 199 | void Flush(); |
[email protected] | 241c5c2c | 2010-06-21 18:46:00 | [diff] [blame] | 200 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 201 | // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 202 | void CloseIdleSockets(); |
| 203 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 204 | // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 205 | int idle_socket_count() const { |
| 206 | return idle_socket_count_; |
| 207 | } |
| 208 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 209 | // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
| 210 | // function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 211 | int IdleSocketCountInGroup(const std::string& group_name) const; |
| 212 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 213 | // See ClientSocketPool::GetLoadState() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 214 | LoadState GetLoadState(const std::string& group_name, |
| 215 | const ClientSocketHandle* handle) const; |
| 216 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 217 | int ConnectRetryIntervalMs() const { |
| 218 | // TODO(mbelshe): Make this tuned dynamically based on measured RTT. |
| 219 | // For now, just use the max retry interval. |
| 220 | return ClientSocketPool::kMaxConnectRetryIntervalMs; |
| 221 | } |
| 222 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 223 | // ConnectJob::Delegate methods: |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 224 | virtual void OnConnectJobComplete(int result, ConnectJob* job); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 225 | |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 226 | // NetworkChangeNotifier::Observer methods: |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 227 | virtual void OnIPAddressChanged(); |
[email protected] | a554a826 | 2010-05-20 00:13:52 | [diff] [blame] | 228 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 229 | int NumConnectJobsInGroup(const std::string& group_name) const { |
| 230 | return group_map_.find(group_name)->second.jobs.size(); |
| 231 | } |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 232 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 233 | // Closes all idle sockets if |force| is true. Else, only closes idle |
| 234 | // sockets that timed out or can't be reused. Made public for testing. |
| 235 | void CleanupIdleSockets(bool force); |
| 236 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 237 | base::TimeDelta ConnectionTimeout() const { |
| 238 | return connect_job_factory_->ConnectionTimeout(); |
| 239 | } |
| 240 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 241 | void EnableBackupJobs() { backup_jobs_enabled_ = true; } |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 242 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 243 | private: |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 244 | friend class base::RefCounted<ClientSocketPoolBaseHelper>; |
| 245 | |
| 246 | ~ClientSocketPoolBaseHelper(); |
| 247 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 248 | // Entry for a persistent socket which became idle at time |start_time|. |
| 249 | struct IdleSocket { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 250 | IdleSocket() : socket(NULL), used(false) {} |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 251 | ClientSocket* socket; |
| 252 | base::TimeTicks start_time; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 253 | bool used; // Indicates whether or not the socket has been used yet. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 254 | |
| 255 | // An idle socket should be removed if it can't be reused, or has been idle |
| 256 | // for too long. |now| is the current time value (TimeTicks::Now()). |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 257 | // |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] | 258 | // |
| 259 | // An idle socket can't be reused if it is disconnected or has received |
| 260 | // data unexpectedly (hence no longer idle). The unread data would be |
| 261 | // mistaken for the beginning of the next response if we were to reuse the |
| 262 | // socket for a new request. |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 263 | bool ShouldCleanup(base::TimeTicks now, base::TimeDelta timeout) const; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 264 | }; |
| 265 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 266 | typedef std::deque<const Request*> RequestQueue; |
| 267 | typedef std::map<const ClientSocketHandle*, const Request*> RequestMap; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 268 | |
| 269 | // A Group is allocated per group_name when there are idle sockets or pending |
| 270 | // requests. Otherwise, the Group object is removed from the map. |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 271 | // |active_socket_count| tracks the number of sockets held by clients. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 272 | struct Group { |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 273 | Group() |
| 274 | : active_socket_count(0), |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 275 | backup_job(NULL), |
| 276 | backup_task(NULL) { |
| 277 | } |
| 278 | |
| 279 | ~Group() { |
| 280 | CleanupBackupJob(); |
| 281 | } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 282 | |
| 283 | bool IsEmpty() const { |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 284 | return active_socket_count == 0 && idle_sockets.empty() && jobs.empty() && |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 285 | pending_requests.empty(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | bool HasAvailableSocketSlot(int max_sockets_per_group) const { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 289 | return active_socket_count + static_cast<int>(jobs.size()) < |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 290 | max_sockets_per_group; |
| 291 | } |
| 292 | |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 293 | RequestPriority TopPendingPriority() const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 294 | return pending_requests.front()->priority(); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 297 | void CleanupBackupJob() { |
| 298 | if (backup_job) { |
| 299 | delete backup_job; |
| 300 | backup_job = NULL; |
| 301 | } |
| 302 | if (backup_task) { |
| 303 | backup_task->Cancel(); |
| 304 | backup_task = NULL; |
| 305 | } |
| 306 | } |
| 307 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 308 | std::deque<IdleSocket> idle_sockets; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 309 | std::set<const ConnectJob*> jobs; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 310 | RequestQueue pending_requests; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 311 | int active_socket_count; // number of active sockets used by clients |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 312 | // A backup job in case the connect for this group takes too long. |
| 313 | ConnectJob* backup_job; |
| 314 | CancelableTask* backup_task; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | typedef std::map<std::string, Group> GroupMap; |
| 318 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 319 | typedef std::set<const ConnectJob*> ConnectJobSet; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 320 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 321 | static void InsertRequestIntoQueue(const Request* r, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 322 | RequestQueue* pending_requests); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame] | 323 | static const Request* RemoveRequestFromQueue(RequestQueue::iterator it, |
| 324 | RequestQueue* pending_requests); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 325 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 326 | // Called when the number of idle sockets changes. |
| 327 | void IncrementIdleCount(); |
| 328 | void DecrementIdleCount(); |
| 329 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 330 | // Scans the group map for groups which have an available socket slot and |
| 331 | // at least one pending request. Returns number of groups found, and if found |
| 332 | // at least one, fills |group| and |group_name| with data of the stalled group |
| 333 | // having highest priority. |
| 334 | int FindTopStalledGroup(Group** group, std::string* group_name); |
| 335 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 336 | // Called when timer_ fires. This method scans the idle sockets removing |
| 337 | // sockets that timed out or can't be reused. |
| 338 | void OnCleanupTimerFired() { |
| 339 | CleanupIdleSockets(false); |
| 340 | } |
| 341 | |
[email protected] | 4d3b05d | 2010-01-27 21:27:29 | [diff] [blame] | 342 | // Removes |job| from |connect_job_set_|. Also updates |group| if non-NULL. |
| 343 | void RemoveConnectJob(const ConnectJob* job, Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 344 | |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 345 | // Might delete the Group from |group_map_|. |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 346 | // If |was_at_socket_limit|, will also check for idle sockets to assign |
| 347 | // to any stalled groups. |
| 348 | void OnAvailableSocketSlot(const std::string& group_name, |
| 349 | bool was_at_socket_limit); |
[email protected] | 8ae03f4 | 2010-07-07 19:08:10 | [diff] [blame] | 350 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 351 | // Process a pending socket request for a group. |
| 352 | // If |was_at_socket_limit|, will also check for idle sockets to assign |
| 353 | // to any stalled groups. |
| 354 | void ProcessPendingRequest(const std::string& group_name, |
| 355 | bool was_at_socket_limit); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 356 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 357 | // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
| 358 | void HandOutSocket(ClientSocket* socket, |
| 359 | bool reused, |
| 360 | ClientSocketHandle* handle, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 361 | base::TimeDelta time_idle, |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 362 | Group* group, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 363 | const BoundNetLog& net_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 364 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 365 | // Adds |socket| to the list of idle sockets for |group|. |used| indicates |
| 366 | // whether or not the socket has previously been used. |
| 367 | void AddIdleSocket(ClientSocket* socket, bool used, Group* group); |
| 368 | |
| 369 | // Iterates through |connect_job_map_|, canceling all ConnectJobs. |
| 370 | // Afterwards, it iterates through all groups and deletes them if they are no |
| 371 | // longer needed. |
| 372 | void CancelAllConnectJobs(); |
| 373 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 374 | // 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] | 375 | bool ReachedMaxSocketsLimit() const; |
| 376 | |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 377 | // This is the internal implementation of RequestSocket(). It differs in that |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 378 | // it does not handle logging into NetLog of the queueing status of |
[email protected] | fd4fe0b | 2010-02-08 23:02:15 | [diff] [blame] | 379 | // |request|. |
| 380 | int RequestSocketInternal(const std::string& group_name, |
| 381 | const Request* request); |
| 382 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 383 | // Assigns an idle socket for the group to the request. |
| 384 | // Returns |true| if an idle socket is available, false otherwise. |
| 385 | bool AssignIdleSocketToGroup(Group* group, const Request* request); |
| 386 | |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 387 | static void LogBoundConnectJobToRequest( |
| 388 | const NetLog::Source& connect_job_source, const Request* request); |
| 389 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 390 | // Set a timer to create a backup socket if it takes too long to create one. |
| 391 | void StartBackupSocketTimer(const std::string& group_name); |
| 392 | |
| 393 | // Called when the backup socket timer fires. |
| 394 | void OnBackupSocketTimerFired(const std::string& group_name); |
| 395 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 396 | // Closes one idle socket. Picks the first one encountered. |
| 397 | // TODO(willchan): Consider a better algorithm for doing this. Perhaps we |
| 398 | // should keep an ordered list of idle sockets, and close them in order. |
| 399 | // Requires maintaining more state. It's not clear if it's worth it since |
| 400 | // I'm not sure if we hit this situation often. |
| 401 | void CloseOneIdleSocket(); |
| 402 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 403 | // Checks if there are stalled socket groups that should be notified |
| 404 | // for possible wakeup. |
| 405 | void CheckForStalledSocketGroups(); |
| 406 | |
| 407 | // Returns true if we might have a stalled group. |
| 408 | bool MayHaveStalledGroups(); |
| 409 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 410 | GroupMap group_map_; |
| 411 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 412 | // Timer used to periodically prune idle sockets that timed out or can't be |
| 413 | // reused. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 414 | base::RepeatingTimer<ClientSocketPoolBaseHelper> timer_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 415 | |
| 416 | // The total number of idle sockets in the system. |
| 417 | int idle_socket_count_; |
| 418 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 419 | // Number of connecting sockets across all groups. |
| 420 | int connecting_socket_count_; |
| 421 | |
| 422 | // Number of connected sockets we handed out across all groups. |
| 423 | int handed_out_socket_count_; |
| 424 | |
| 425 | // The maximum total number of sockets. See ReachedMaxSocketsLimit. |
| 426 | const int max_sockets_; |
| 427 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 428 | // The maximum number of sockets kept per group. |
| 429 | const int max_sockets_per_group_; |
| 430 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 431 | // The time to wait until closing idle sockets. |
| 432 | const base::TimeDelta unused_idle_socket_timeout_; |
| 433 | const base::TimeDelta used_idle_socket_timeout_; |
| 434 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 435 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 436 | |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 437 | // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool |
| 438 | bool backup_jobs_enabled_; |
| 439 | |
[email protected] | 6b624c6 | 2010-03-14 08:37:32 | [diff] [blame] | 440 | // A factory to pin the backup_job tasks. |
| 441 | ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 442 | |
| 443 | // A unique id for the pool. It gets incremented every time we Flush() the |
| 444 | // pool. This is so that when sockets get released back to the pool, we can |
| 445 | // make sure that they are discarded rather than reused. |
| 446 | int pool_generation_number_; |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 447 | |
| 448 | // The count of stalled groups the last time we checked. |
| 449 | int last_stalled_group_count_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | } // namespace internal |
| 453 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 454 | // The maximum duration, in seconds, to keep used idle persistent sockets alive. |
| 455 | static const int kUsedIdleSocketTimeout = 300; // 5 minutes |
| 456 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 457 | template <typename SocketParams> |
| 458 | class ClientSocketPoolBase { |
| 459 | public: |
| 460 | class Request : public internal::ClientSocketPoolBaseHelper::Request { |
| 461 | public: |
| 462 | Request(ClientSocketHandle* handle, |
| 463 | CompletionCallback* callback, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 464 | RequestPriority priority, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 465 | const scoped_refptr<SocketParams>& params, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 466 | const BoundNetLog& net_log) |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 467 | : internal::ClientSocketPoolBaseHelper::Request( |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 468 | handle, callback, priority, net_log), |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 469 | params_(params) {} |
| 470 | |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 471 | const scoped_refptr<SocketParams>& params() const { return params_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 472 | |
| 473 | private: |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 474 | scoped_refptr<SocketParams> params_; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 475 | }; |
| 476 | |
| 477 | class ConnectJobFactory { |
| 478 | public: |
| 479 | ConnectJobFactory() {} |
| 480 | virtual ~ConnectJobFactory() {} |
| 481 | |
| 482 | virtual ConnectJob* NewConnectJob( |
| 483 | const std::string& group_name, |
| 484 | const Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 485 | ConnectJob::Delegate* delegate) const = 0; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 486 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 487 | virtual base::TimeDelta ConnectionTimeout() const = 0; |
| 488 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 489 | private: |
| 490 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 491 | }; |
| 492 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 493 | // |max_sockets| is the maximum number of sockets to be maintained by this |
| 494 | // ClientSocketPool. |max_sockets_per_group| specifies the maximum number of |
| 495 | // sockets a "group" can have. |unused_idle_socket_timeout| specifies how |
| 496 | // long to leave an unused idle socket open before closing it. |
| 497 | // |used_idle_socket_timeout| specifies how long to leave a previously used |
| 498 | // idle socket open before closing it. |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 499 | ClientSocketPoolBase( |
| 500 | int max_sockets, |
| 501 | int max_sockets_per_group, |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 502 | const scoped_refptr<ClientSocketPoolHistograms>& histograms, |
[email protected] | 100d5fb9 | 2009-12-21 21:08:35 | [diff] [blame] | 503 | base::TimeDelta unused_idle_socket_timeout, |
| 504 | base::TimeDelta used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 505 | ConnectJobFactory* connect_job_factory) |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 506 | : histograms_(histograms), |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 507 | helper_(new internal::ClientSocketPoolBaseHelper( |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 508 | max_sockets, max_sockets_per_group, |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 509 | unused_idle_socket_timeout, used_idle_socket_timeout, |
[email protected] | 66761b95 | 2010-06-25 21:30:38 | [diff] [blame] | 510 | new ConnectJobFactoryAdaptor(connect_job_factory))) {} |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 511 | |
[email protected] | 20cb5f48 | 2009-12-16 01:01:25 | [diff] [blame] | 512 | virtual ~ClientSocketPoolBase() {} |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 513 | |
| 514 | // These member functions simply forward to ClientSocketPoolBaseHelper. |
| 515 | |
| 516 | // RequestSocket bundles up the parameters into a Request and then forwards to |
| 517 | // ClientSocketPoolBaseHelper::RequestSocket(). Note that the memory |
| 518 | // ownership is transferred in the asynchronous (ERR_IO_PENDING) case. |
| 519 | int RequestSocket(const std::string& group_name, |
[email protected] | df4b4ef | 2010-07-12 18:25:21 | [diff] [blame] | 520 | const scoped_refptr<SocketParams>& params, |
[email protected] | ac790b4 | 2009-12-02 04:31:31 | [diff] [blame] | 521 | RequestPriority priority, |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 522 | ClientSocketHandle* handle, |
| 523 | CompletionCallback* callback, |
[email protected] | 9e743cd | 2010-03-16 07:03:53 | [diff] [blame] | 524 | const BoundNetLog& net_log) { |
[email protected] | e7e9932 | 2010-05-04 23:30:17 | [diff] [blame] | 525 | Request* request = new Request(handle, callback, priority, params, net_log); |
| 526 | return helper_->RequestSocket(group_name, request); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | void CancelRequest(const std::string& group_name, |
| 530 | const ClientSocketHandle* handle) { |
| 531 | return helper_->CancelRequest(group_name, handle); |
| 532 | } |
| 533 | |
[email protected] | 241c5c2c | 2010-06-21 18:46:00 | [diff] [blame] | 534 | void ReleaseSocket(const std::string& group_name, ClientSocket* socket, |
| 535 | int id) { |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 536 | return helper_->ReleaseSocket(group_name, socket, id); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | void CloseIdleSockets() { return helper_->CloseIdleSockets(); } |
| 540 | |
| 541 | int idle_socket_count() const { return helper_->idle_socket_count(); } |
| 542 | |
| 543 | int IdleSocketCountInGroup(const std::string& group_name) const { |
| 544 | return helper_->IdleSocketCountInGroup(group_name); |
| 545 | } |
| 546 | |
| 547 | LoadState GetLoadState(const std::string& group_name, |
| 548 | const ClientSocketHandle* handle) const { |
| 549 | return helper_->GetLoadState(group_name, handle); |
| 550 | } |
| 551 | |
| 552 | virtual void OnConnectJobComplete(int result, ConnectJob* job) { |
| 553 | return helper_->OnConnectJobComplete(result, job); |
| 554 | } |
| 555 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 556 | int NumConnectJobsInGroup(const std::string& group_name) const { |
| 557 | return helper_->NumConnectJobsInGroup(group_name); |
| 558 | } |
| 559 | |
[email protected] | 9bf28db | 2009-08-29 01:35:16 | [diff] [blame] | 560 | void CleanupIdleSockets(bool force) { |
| 561 | return helper_->CleanupIdleSockets(force); |
| 562 | } |
| 563 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 564 | base::TimeDelta ConnectionTimeout() const { |
| 565 | return helper_->ConnectionTimeout(); |
| 566 | } |
| 567 | |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 568 | scoped_refptr<ClientSocketPoolHistograms> histograms() const { |
| 569 | return histograms_; |
| 570 | } |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 571 | |
[email protected] | 43a21b8 | 2010-06-10 21:30:54 | [diff] [blame] | 572 | void EnableBackupJobs() { helper_->EnableBackupJobs(); } |
[email protected] | 7c28e9a | 2010-03-20 01:16:13 | [diff] [blame] | 573 | |
[email protected] | a7e3857 | 2010-06-07 18:22:24 | [diff] [blame] | 574 | void Flush() { helper_->Flush(); } |
| 575 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 576 | private: |
| 577 | // This adaptor class exists to bridge the |
| 578 | // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
| 579 | // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
| 580 | // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
| 581 | // static_cast themselves. |
| 582 | class ConnectJobFactoryAdaptor |
| 583 | : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| 584 | public: |
| 585 | typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory |
| 586 | ConnectJobFactory; |
| 587 | |
| 588 | explicit ConnectJobFactoryAdaptor( |
| 589 | ConnectJobFactory* connect_job_factory) |
| 590 | : connect_job_factory_(connect_job_factory) {} |
| 591 | virtual ~ConnectJobFactoryAdaptor() {} |
| 592 | |
| 593 | virtual ConnectJob* NewConnectJob( |
| 594 | const std::string& group_name, |
| 595 | const internal::ClientSocketPoolBaseHelper::Request& request, |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 596 | ConnectJob::Delegate* delegate) const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 597 | const Request* casted_request = static_cast<const Request*>(&request); |
| 598 | return connect_job_factory_->NewConnectJob( |
[email protected] | 06650c5 | 2010-06-03 00:49:17 | [diff] [blame] | 599 | group_name, *casted_request, delegate); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 600 | } |
| 601 | |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 602 | virtual base::TimeDelta ConnectionTimeout() const { |
| 603 | return connect_job_factory_->ConnectionTimeout(); |
| 604 | } |
| 605 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 606 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
| 607 | }; |
| 608 | |
[email protected] | b89f7e4 | 2010-05-20 20:37:00 | [diff] [blame] | 609 | // Histograms for the pool |
| 610 | const scoped_refptr<ClientSocketPoolHistograms> histograms_; |
[email protected] | a796bcec | 2010-03-22 17:17:26 | [diff] [blame] | 611 | |
[email protected] | eb5a9938 | 2010-07-11 03:18:26 | [diff] [blame] | 612 | // The reason for reference counting here is because the operations on |
| 613 | // the ClientSocketPoolBaseHelper which release sockets can cause the |
| 614 | // ClientSocketPoolBase<T> reference to drop to zero. While we're deep |
| 615 | // in cleanup code, we'll often hold a reference to |self|. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 616 | scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_; |
| 617 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 618 | DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 619 | }; |
| 620 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 621 | } // namespace net |
| 622 | |
| 623 | #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |