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