[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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" |
| 31 | #include "base/scoped_ptr.h" |
| 32 | #include "base/time.h" |
| 33 | #include "base/timer.h" |
| 34 | #include "net/base/address_list.h" |
| 35 | #include "net/base/completion_callback.h" |
[email protected] | ec08bb2 | 2009-08-12 00:25:12 | [diff] [blame] | 36 | #include "net/base/load_log.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 37 | #include "net/base/load_states.h" |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 38 | #include "net/base/net_errors.h" |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 39 | #include "net/socket/client_socket.h" |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 40 | #include "net/socket/client_socket_pool.h" |
| 41 | |
| 42 | namespace net { |
| 43 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 44 | class ClientSocketHandle; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 45 | |
| 46 | // ConnectJob provides an abstract interface for "connecting" a socket. |
| 47 | // The connection may involve host resolution, tcp connection, ssl connection, |
| 48 | // etc. |
| 49 | class ConnectJob { |
| 50 | public: |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 51 | class Delegate { |
| 52 | public: |
| 53 | Delegate() {} |
| 54 | virtual ~Delegate() {} |
| 55 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 56 | // Alerts the delegate that the connection completed. |
| 57 | virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 58 | |
| 59 | private: |
| 60 | DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 61 | }; |
| 62 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 63 | // A |timeout_duration| of 0 corresponds to no timeout. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 64 | ConnectJob(const std::string& group_name, |
| 65 | const ClientSocketHandle* key_handle, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 66 | base::TimeDelta timeout_duration, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 67 | Delegate* delegate, |
| 68 | LoadLog* load_log); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 69 | virtual ~ConnectJob(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 70 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 71 | // Accessors |
| 72 | const std::string& group_name() const { return group_name_; } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 73 | LoadState load_state() const { return load_state_; } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 74 | const ClientSocketHandle* key_handle() const { return key_handle_; } |
| 75 | |
[email protected] | 8e12ae0 | 2009-07-02 16:15:04 | [diff] [blame] | 76 | // Releases |socket_| to the client. On connection error, this should return |
| 77 | // NULL. |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 78 | ClientSocket* ReleaseSocket() { return socket_.release(); } |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 79 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 80 | // Begins connecting the socket. Returns OK on success, ERR_IO_PENDING if it |
| 81 | // cannot complete synchronously without blocking, or another net error code |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 82 | // on error. In asynchronous completion, the ConnectJob will notify |
| 83 | // |delegate_| via OnConnectJobComplete. In both asynchronous and synchronous |
| 84 | // completion, ReleaseSocket() can be called to acquire the connected socket |
| 85 | // if it succeeded. |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 86 | int Connect(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 87 | |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 88 | LoadLog* load_log() { return load_log_; } |
| 89 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 90 | protected: |
| 91 | void set_load_state(LoadState load_state) { load_state_ = load_state; } |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 92 | void set_socket(ClientSocket* socket) { socket_.reset(socket); } |
| 93 | ClientSocket* socket() { return socket_.get(); } |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 94 | void NotifyDelegateOfCompletion(int rv); |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 95 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 96 | private: |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 97 | virtual int ConnectInternal() = 0; |
| 98 | |
| 99 | // Alerts the delegate that the ConnectJob has timed out. |
| 100 | void OnTimeout(); |
| 101 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 102 | const std::string group_name_; |
| 103 | // Temporarily needed until we switch to late binding. |
| 104 | const ClientSocketHandle* const key_handle_; |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 105 | const base::TimeDelta timeout_duration_; |
| 106 | // Timer to abort jobs that take too long. |
| 107 | base::OneShotTimer<ConnectJob> timer_; |
| 108 | Delegate* delegate_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 109 | LoadState load_state_; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 110 | scoped_ptr<ClientSocket> socket_; |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 111 | scoped_refptr<LoadLog> load_log_; |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 112 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 113 | DISALLOW_COPY_AND_ASSIGN(ConnectJob); |
| 114 | }; |
| 115 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 116 | namespace internal { |
| 117 | |
| 118 | // ClientSocketPoolBaseHelper is an internal class that implements almost all |
| 119 | // the functionality from ClientSocketPoolBase without using templates. |
| 120 | // ClientSocketPoolBase adds templated definitions built on top of |
| 121 | // ClientSocketPoolBaseHelper. This class is not for external use, please use |
| 122 | // ClientSocketPoolBase instead. |
| 123 | class ClientSocketPoolBaseHelper |
| 124 | : public base::RefCounted<ClientSocketPoolBaseHelper>, |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 125 | public ConnectJob::Delegate { |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 126 | public: |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 127 | class Request { |
| 128 | public: |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 129 | Request(ClientSocketHandle* handle, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 130 | CompletionCallback* callback, |
| 131 | int priority, |
[email protected] | 684970b | 2009-08-14 04:54:46 | [diff] [blame] | 132 | LoadLog* load_log) |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 133 | : handle_(handle), callback_(callback), priority_(priority), |
| 134 | load_log_(load_log) {} |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 135 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 136 | virtual ~Request() {} |
| 137 | |
| 138 | ClientSocketHandle* handle() const { return handle_; } |
| 139 | CompletionCallback* callback() const { return callback_; } |
| 140 | int priority() const { return priority_; } |
| 141 | LoadLog* load_log() const { return load_log_.get(); } |
| 142 | |
| 143 | private: |
| 144 | ClientSocketHandle* const handle_; |
| 145 | CompletionCallback* const callback_; |
| 146 | const int priority_; |
| 147 | const scoped_refptr<LoadLog> load_log_; |
| 148 | |
| 149 | DISALLOW_COPY_AND_ASSIGN(Request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | class ConnectJobFactory { |
| 153 | public: |
| 154 | ConnectJobFactory() {} |
| 155 | virtual ~ConnectJobFactory() {} |
| 156 | |
| 157 | virtual ConnectJob* NewConnectJob( |
| 158 | const std::string& group_name, |
| 159 | const Request& request, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 160 | ConnectJob::Delegate* delegate, |
| 161 | LoadLog* load_log) const = 0; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 162 | |
| 163 | private: |
| 164 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 165 | }; |
| 166 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 167 | ClientSocketPoolBaseHelper(int max_sockets, |
| 168 | int max_sockets_per_group, |
| 169 | ConnectJobFactory* connect_job_factory); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 170 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 171 | ~ClientSocketPoolBaseHelper(); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 172 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 173 | // See ClientSocketPool::RequestSocket for documentation on this function. |
| 174 | // Note that |request| must be heap allocated. If ERR_IO_PENDING is returned, |
| 175 | // then ClientSocketPoolBaseHelper takes ownership of |request|. |
| 176 | int RequestSocket(const std::string& group_name, const Request* request); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 177 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 178 | // See ClientSocketPool::CancelRequest for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 179 | void CancelRequest(const std::string& group_name, |
| 180 | const ClientSocketHandle* handle); |
| 181 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 182 | // See ClientSocketPool::ReleaseSocket for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 183 | void ReleaseSocket(const std::string& group_name, |
| 184 | ClientSocket* socket); |
| 185 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 186 | // See ClientSocketPool::CloseIdleSockets for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 187 | void CloseIdleSockets(); |
| 188 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 189 | // See ClientSocketPool::IdleSocketCount() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 190 | int idle_socket_count() const { |
| 191 | return idle_socket_count_; |
| 192 | } |
| 193 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 194 | // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this |
| 195 | // function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 196 | int IdleSocketCountInGroup(const std::string& group_name) const; |
| 197 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 198 | // See ClientSocketPool::GetLoadState() for documentation on this function. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 199 | LoadState GetLoadState(const std::string& group_name, |
| 200 | const ClientSocketHandle* handle) const; |
| 201 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 202 | // ConnectJob::Delegate methods: |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 203 | virtual void OnConnectJobComplete(int result, ConnectJob* job); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 204 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 205 | // Enables late binding of sockets. In this mode, socket requests are |
| 206 | // decoupled from socket connection jobs. A socket request may initiate a |
| 207 | // socket connection job, but there is no guarantee that that socket |
| 208 | // connection will service the request (for example, a released socket may |
| 209 | // service the request sooner, or a higher priority request may come in |
| 210 | // afterward and receive the socket from the job). |
| 211 | static void EnableLateBindingOfSockets(bool enabled); |
| 212 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 213 | // For testing. |
| 214 | bool may_have_stalled_group() const { return may_have_stalled_group_; } |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 215 | |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 216 | int NumConnectJobsInGroup(const std::string& group_name) const { |
| 217 | return group_map_.find(group_name)->second.jobs.size(); |
| 218 | } |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 219 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 220 | private: |
| 221 | // Entry for a persistent socket which became idle at time |start_time|. |
| 222 | struct IdleSocket { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 223 | IdleSocket() : socket(NULL), used(false) {} |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 224 | ClientSocket* socket; |
| 225 | base::TimeTicks start_time; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 226 | bool used; // Indicates whether or not the socket has been used yet. |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 227 | |
| 228 | // An idle socket should be removed if it can't be reused, or has been idle |
| 229 | // for too long. |now| is the current time value (TimeTicks::Now()). |
| 230 | // |
| 231 | // An idle socket can't be reused if it is disconnected or has received |
| 232 | // data unexpectedly (hence no longer idle). The unread data would be |
| 233 | // mistaken for the beginning of the next response if we were to reuse the |
| 234 | // socket for a new request. |
| 235 | bool ShouldCleanup(base::TimeTicks now) const; |
| 236 | }; |
| 237 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 238 | typedef std::deque<const Request*> RequestQueue; |
| 239 | typedef std::map<const ClientSocketHandle*, const Request*> RequestMap; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 240 | |
| 241 | // A Group is allocated per group_name when there are idle sockets or pending |
| 242 | // requests. Otherwise, the Group object is removed from the map. |
| 243 | struct Group { |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 244 | Group() : active_socket_count(0) {} |
| 245 | |
| 246 | bool IsEmpty() const { |
[email protected] | 2b7523d | 2009-07-29 20:29:23 | [diff] [blame] | 247 | return active_socket_count == 0 && idle_sockets.empty() && jobs.empty() && |
| 248 | pending_requests.empty() && connecting_requests.empty(); |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | bool HasAvailableSocketSlot(int max_sockets_per_group) const { |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 252 | return active_socket_count + static_cast<int>(jobs.size()) < |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 253 | max_sockets_per_group; |
| 254 | } |
| 255 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 256 | int TopPendingPriority() const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 257 | return pending_requests.front()->priority(); |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 258 | } |
| 259 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 260 | std::deque<IdleSocket> idle_sockets; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 261 | std::set<const ConnectJob*> jobs; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 262 | RequestQueue pending_requests; |
| 263 | RequestMap connecting_requests; |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 264 | int active_socket_count; // number of active sockets used by clients |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | typedef std::map<std::string, Group> GroupMap; |
| 268 | |
| 269 | typedef std::map<const ClientSocketHandle*, ConnectJob*> ConnectJobMap; |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 270 | typedef std::set<const ConnectJob*> ConnectJobSet; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 271 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 272 | static void InsertRequestIntoQueue(const Request* r, |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 273 | RequestQueue* pending_requests); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 274 | static const Request* RemoveRequestFromQueue(RequestQueue::iterator it, |
| 275 | RequestQueue* pending_requests); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 276 | |
| 277 | // Closes all idle sockets if |force| is true. Else, only closes idle |
| 278 | // sockets that timed out or can't be reused. |
| 279 | void CleanupIdleSockets(bool force); |
| 280 | |
| 281 | // Called when the number of idle sockets changes. |
| 282 | void IncrementIdleCount(); |
| 283 | void DecrementIdleCount(); |
| 284 | |
| 285 | // Called via PostTask by ReleaseSocket. |
| 286 | void DoReleaseSocket(const std::string& group_name, ClientSocket* socket); |
| 287 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 288 | // Scans the group map for groups which have an available socket slot and |
| 289 | // at least one pending request. Returns number of groups found, and if found |
| 290 | // at least one, fills |group| and |group_name| with data of the stalled group |
| 291 | // having highest priority. |
| 292 | int FindTopStalledGroup(Group** group, std::string* group_name); |
| 293 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 294 | // Called when timer_ fires. This method scans the idle sockets removing |
| 295 | // sockets that timed out or can't be reused. |
| 296 | void OnCleanupTimerFired() { |
| 297 | CleanupIdleSockets(false); |
| 298 | } |
| 299 | |
| 300 | // Removes the ConnectJob corresponding to |handle| from the |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 301 | // |connect_job_map_| or |connect_job_set_| depending on whether or not late |
| 302 | // binding is enabled. |job| must be non-NULL when late binding is |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 303 | // enabled. Also updates |group| if non-NULL. When late binding is disabled, |
| 304 | // this will also delete the Request from |group->connecting_requests|. |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 305 | void RemoveConnectJob(const ClientSocketHandle* handle, |
[email protected] | 974ebd6 | 2009-08-03 23:14:34 | [diff] [blame] | 306 | const ConnectJob* job, |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 307 | Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 308 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 309 | // Same as OnAvailableSocketSlot except it looks up the Group first to see if |
| 310 | // it's there. |
| 311 | void MaybeOnAvailableSocketSlot(const std::string& group_name); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 312 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 313 | // Might delete the Group from |group_map_|. |
| 314 | void OnAvailableSocketSlot(const std::string& group_name, Group* group); |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 315 | |
| 316 | // Process a request from a group's pending_requests queue. |
| 317 | void ProcessPendingRequest(const std::string& group_name, Group* group); |
| 318 | |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 319 | // Assigns |socket| to |handle| and updates |group|'s counters appropriately. |
| 320 | void HandOutSocket(ClientSocket* socket, |
| 321 | bool reused, |
| 322 | ClientSocketHandle* handle, |
[email protected] | f9d285c | 2009-08-17 19:54:29 | [diff] [blame] | 323 | base::TimeDelta time_idle, |
[email protected] | 2ab05b5 | 2009-07-01 23:57:58 | [diff] [blame] | 324 | Group* group); |
| 325 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 326 | // Adds |socket| to the list of idle sockets for |group|. |used| indicates |
| 327 | // whether or not the socket has previously been used. |
| 328 | void AddIdleSocket(ClientSocket* socket, bool used, Group* group); |
| 329 | |
| 330 | // Iterates through |connect_job_map_|, canceling all ConnectJobs. |
| 331 | // Afterwards, it iterates through all groups and deletes them if they are no |
| 332 | // longer needed. |
| 333 | void CancelAllConnectJobs(); |
| 334 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 335 | // Returns true if we can't create any more sockets due to the total limit. |
| 336 | // TODO(phajdan.jr): Also take idle sockets into account. |
| 337 | bool ReachedMaxSocketsLimit() const; |
| 338 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 339 | GroupMap group_map_; |
| 340 | |
| 341 | ConnectJobMap connect_job_map_; |
| 342 | |
| 343 | // Timer used to periodically prune idle sockets that timed out or can't be |
| 344 | // reused. |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 345 | base::RepeatingTimer<ClientSocketPoolBaseHelper> timer_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 346 | |
| 347 | // The total number of idle sockets in the system. |
| 348 | int idle_socket_count_; |
| 349 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 350 | // Number of connecting sockets across all groups. |
| 351 | int connecting_socket_count_; |
| 352 | |
| 353 | // Number of connected sockets we handed out across all groups. |
| 354 | int handed_out_socket_count_; |
| 355 | |
| 356 | // The maximum total number of sockets. See ReachedMaxSocketsLimit. |
| 357 | const int max_sockets_; |
| 358 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 359 | // The maximum number of sockets kept per group. |
| 360 | const int max_sockets_per_group_; |
| 361 | |
[email protected] | 211d217 | 2009-07-22 15:48:53 | [diff] [blame] | 362 | // Until the maximum number of sockets limit is reached, a group can only |
| 363 | // have pending requests if it exceeds the "max sockets per group" limit. |
| 364 | // |
| 365 | // This means when a socket is released, the only pending requests that can |
| 366 | // be started next belong to the same group. |
| 367 | // |
| 368 | // However once the |max_sockets_| limit is reached, this stops being true: |
| 369 | // groups can now have pending requests without having first reached the |
| 370 | // |max_sockets_per_group_| limit. So choosing the next request involves |
| 371 | // selecting the highest priority request across *all* groups. |
| 372 | // |
| 373 | // Since reaching the maximum number of sockets is an edge case, we make note |
| 374 | // of when it happens, and thus avoid doing the slower "scan all groups" |
| 375 | // in the common case. |
| 376 | bool may_have_stalled_group_; |
| 377 | |
[email protected] | ab83889 | 2009-06-30 18:49:05 | [diff] [blame] | 378 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 379 | |
[email protected] | 5fc08e3 | 2009-07-15 17:09:57 | [diff] [blame] | 380 | // Controls whether or not we use late binding of sockets. |
| 381 | static bool g_late_binding; |
| 382 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | } // namespace internal |
| 386 | |
| 387 | template <typename SocketParams> |
| 388 | class ClientSocketPoolBase { |
| 389 | public: |
| 390 | class Request : public internal::ClientSocketPoolBaseHelper::Request { |
| 391 | public: |
| 392 | Request(ClientSocketHandle* handle, |
| 393 | CompletionCallback* callback, |
| 394 | int priority, |
| 395 | const SocketParams& params, |
| 396 | LoadLog* load_log) |
| 397 | : internal::ClientSocketPoolBaseHelper::Request( |
| 398 | handle, callback, priority, load_log), |
| 399 | params_(params) {} |
| 400 | |
| 401 | const SocketParams& params() const { return params_; } |
| 402 | |
| 403 | private: |
| 404 | SocketParams params_; |
| 405 | }; |
| 406 | |
| 407 | class ConnectJobFactory { |
| 408 | public: |
| 409 | ConnectJobFactory() {} |
| 410 | virtual ~ConnectJobFactory() {} |
| 411 | |
| 412 | virtual ConnectJob* NewConnectJob( |
| 413 | const std::string& group_name, |
| 414 | const Request& request, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 415 | ConnectJob::Delegate* delegate, |
| 416 | LoadLog* load_log) const = 0; |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 417 | |
| 418 | private: |
| 419 | DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
| 420 | }; |
| 421 | |
| 422 | ClientSocketPoolBase(int max_sockets, |
| 423 | int max_sockets_per_group, |
| 424 | ConnectJobFactory* connect_job_factory) |
| 425 | : helper_(new internal::ClientSocketPoolBaseHelper( |
| 426 | max_sockets, max_sockets_per_group, |
| 427 | new ConnectJobFactoryAdaptor(connect_job_factory))) {} |
| 428 | |
| 429 | ~ClientSocketPoolBase() {} |
| 430 | |
| 431 | // These member functions simply forward to ClientSocketPoolBaseHelper. |
| 432 | |
| 433 | // RequestSocket bundles up the parameters into a Request and then forwards to |
| 434 | // ClientSocketPoolBaseHelper::RequestSocket(). Note that the memory |
| 435 | // ownership is transferred in the asynchronous (ERR_IO_PENDING) case. |
| 436 | int RequestSocket(const std::string& group_name, |
| 437 | const SocketParams& params, |
| 438 | int priority, |
| 439 | ClientSocketHandle* handle, |
| 440 | CompletionCallback* callback, |
| 441 | LoadLog* load_log) { |
| 442 | scoped_ptr<Request> request( |
| 443 | new Request(handle, callback, priority, params, load_log)); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 444 | LoadLog::BeginEvent(load_log, LoadLog::TYPE_SOCKET_POOL); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 445 | int rv = helper_->RequestSocket(group_name, request.get()); |
| 446 | if (rv == ERR_IO_PENDING) |
| 447 | request.release(); |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 448 | else |
| 449 | LoadLog::EndEvent(load_log, LoadLog::TYPE_SOCKET_POOL); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 450 | return rv; |
| 451 | } |
| 452 | |
| 453 | void CancelRequest(const std::string& group_name, |
| 454 | const ClientSocketHandle* handle) { |
| 455 | return helper_->CancelRequest(group_name, handle); |
| 456 | } |
| 457 | |
| 458 | void ReleaseSocket(const std::string& group_name, ClientSocket* socket) { |
| 459 | return helper_->ReleaseSocket(group_name, socket); |
| 460 | } |
| 461 | |
| 462 | void CloseIdleSockets() { return helper_->CloseIdleSockets(); } |
| 463 | |
| 464 | int idle_socket_count() const { return helper_->idle_socket_count(); } |
| 465 | |
| 466 | int IdleSocketCountInGroup(const std::string& group_name) const { |
| 467 | return helper_->IdleSocketCountInGroup(group_name); |
| 468 | } |
| 469 | |
| 470 | LoadState GetLoadState(const std::string& group_name, |
| 471 | const ClientSocketHandle* handle) const { |
| 472 | return helper_->GetLoadState(group_name, handle); |
| 473 | } |
| 474 | |
| 475 | virtual void OnConnectJobComplete(int result, ConnectJob* job) { |
| 476 | return helper_->OnConnectJobComplete(result, job); |
| 477 | } |
| 478 | |
| 479 | // For testing. |
| 480 | bool may_have_stalled_group() const { |
| 481 | return helper_->may_have_stalled_group(); |
| 482 | } |
| 483 | |
| 484 | int NumConnectJobsInGroup(const std::string& group_name) const { |
| 485 | return helper_->NumConnectJobsInGroup(group_name); |
| 486 | } |
| 487 | |
| 488 | private: |
| 489 | // This adaptor class exists to bridge the |
| 490 | // internal::ClientSocketPoolBaseHelper::ConnectJobFactory and |
| 491 | // ClientSocketPoolBase::ConnectJobFactory types, allowing clients to use the |
| 492 | // typesafe ClientSocketPoolBase::ConnectJobFactory, rather than having to |
| 493 | // static_cast themselves. |
| 494 | class ConnectJobFactoryAdaptor |
| 495 | : public internal::ClientSocketPoolBaseHelper::ConnectJobFactory { |
| 496 | public: |
| 497 | typedef typename ClientSocketPoolBase<SocketParams>::ConnectJobFactory |
| 498 | ConnectJobFactory; |
| 499 | |
| 500 | explicit ConnectJobFactoryAdaptor( |
| 501 | ConnectJobFactory* connect_job_factory) |
| 502 | : connect_job_factory_(connect_job_factory) {} |
| 503 | virtual ~ConnectJobFactoryAdaptor() {} |
| 504 | |
| 505 | virtual ConnectJob* NewConnectJob( |
| 506 | const std::string& group_name, |
| 507 | const internal::ClientSocketPoolBaseHelper::Request& request, |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 508 | ConnectJob::Delegate* delegate, |
| 509 | LoadLog* load_log) const { |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 510 | const Request* casted_request = static_cast<const Request*>(&request); |
| 511 | return connect_job_factory_->NewConnectJob( |
[email protected] | fd7b7c9 | 2009-08-20 19:38:30 | [diff] [blame^] | 512 | group_name, *casted_request, delegate, load_log); |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
| 516 | }; |
| 517 | |
| 518 | // One might ask why ClientSocketPoolBaseHelper is also refcounted if its |
| 519 | // containing ClientSocketPool is already refcounted. The reason is because |
| 520 | // DoReleaseSocket() posts a task. If ClientSocketPool gets deleted between |
| 521 | // the posting of the task and the execution, then we'll hit the DCHECK that |
| 522 | // |ClientSocketPoolBaseHelper::group_map_| is empty. |
| 523 | scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_; |
| 524 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 525 | DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
| 526 | }; |
| 527 | |
[email protected] | d80a432 | 2009-08-14 07:07:49 | [diff] [blame] | 528 | // Enables late binding of sockets. In this mode, socket requests are |
| 529 | // decoupled from socket connection jobs. A socket request may initiate a |
| 530 | // socket connection job, but there is no guarantee that that socket |
| 531 | // connection will service the request (for example, a released socket may |
| 532 | // service the request sooner, or a higher priority request may come in |
| 533 | // afterward and receive the socket from the job). |
| 534 | void EnableLateBindingOfSockets(bool enabled); |
| 535 | |
[email protected] | ff579d4 | 2009-06-24 15:47:02 | [diff] [blame] | 536 | } // namespace net |
| 537 | |
| 538 | #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |