blob: a39eeaa2db85924560d42ea6b4d6195663356873 [file] [log] [blame]
[email protected]a796bcec2010-03-22 17:17:261// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]ff579d42009-06-24 15:47:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]d80a4322009-08-14 07:07:494//
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]ff579d42009-06-24 15:47:0222#ifndef NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_
23#define NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_
[email protected]32b76ef2010-07-26 23:08:2424#pragma once
[email protected]ff579d42009-06-24 15:47:0225
26#include <deque>
[email protected]e1b54dc2010-10-06 21:27:2227#include <list>
[email protected]ff579d42009-06-24 15:47:0228#include <map>
[email protected]5fc08e32009-07-15 17:09:5729#include <set>
[email protected]ff579d42009-06-24 15:47:0230#include <string>
31
32#include "base/basictypes.h"
[email protected]100d5fb92009-12-21 21:08:3533#include "base/ref_counted.h"
[email protected]ff579d42009-06-24 15:47:0234#include "base/scoped_ptr.h"
[email protected]6b624c62010-03-14 08:37:3235#include "base/task.h"
[email protected]ff579d42009-06-24 15:47:0236#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]ff579d42009-06-24 15:47:0240#include "net/base/load_states.h"
[email protected]d80a4322009-08-14 07:07:4941#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5342#include "net/base/net_log.h"
[email protected]a554a8262010-05-20 00:13:5243#include "net/base/network_change_notifier.h"
[email protected]ac790b42009-12-02 04:31:3144#include "net/base/request_priority.h"
[email protected]2ab05b52009-07-01 23:57:5845#include "net/socket/client_socket.h"
[email protected]ff579d42009-06-24 15:47:0246#include "net/socket/client_socket_pool.h"
47
48namespace net {
49
[email protected]ff579d42009-06-24 15:47:0250class ClientSocketHandle;
[email protected]ff579d42009-06-24 15:47:0251
52// ConnectJob provides an abstract interface for "connecting" a socket.
53// The connection may involve host resolution, tcp connection, ssl connection,
54// etc.
55class ConnectJob {
56 public:
[email protected]ab838892009-06-30 18:49:0557 class Delegate {
58 public:
59 Delegate() {}
60 virtual ~Delegate() {}
61
[email protected]2ab05b52009-07-01 23:57:5862 // Alerts the delegate that the connection completed.
63 virtual void OnConnectJobComplete(int result, ConnectJob* job) = 0;
[email protected]ab838892009-06-30 18:49:0564
65 private:
66 DISALLOW_COPY_AND_ASSIGN(Delegate);
67 };
68
[email protected]974ebd62009-08-03 23:14:3469 // A |timeout_duration| of 0 corresponds to no timeout.
[email protected]2ab05b52009-07-01 23:57:5870 ConnectJob(const std::string& group_name,
[email protected]974ebd62009-08-03 23:14:3471 base::TimeDelta timeout_duration,
[email protected]fd7b7c92009-08-20 19:38:3072 Delegate* delegate,
[email protected]9e743cd2010-03-16 07:03:5373 const BoundNetLog& net_log);
[email protected]2ab05b52009-07-01 23:57:5874 virtual ~ConnectJob();
[email protected]ff579d42009-06-24 15:47:0275
[email protected]2ab05b52009-07-01 23:57:5876 // Accessors
77 const std::string& group_name() const { return group_name_; }
[email protected]9e743cd2010-03-16 07:03:5378 const BoundNetLog& net_log() { return net_log_; }
[email protected]2ab05b52009-07-01 23:57:5879
[email protected]8e12ae02009-07-02 16:15:0480 // Releases |socket_| to the client. On connection error, this should return
81 // NULL.
[email protected]2ab05b52009-07-01 23:57:5882 ClientSocket* ReleaseSocket() { return socket_.release(); }
[email protected]ab838892009-06-30 18:49:0583
[email protected]ff579d42009-06-24 15:47:0284 // 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]2ab05b52009-07-01 23:57:5886 // 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]974ebd62009-08-03 23:14:3490 int Connect();
[email protected]ff579d42009-06-24 15:47:0291
[email protected]46451352009-09-01 14:54:2192 virtual LoadState GetLoadState() const = 0;
[email protected]fd7b7c92009-08-20 19:38:3093
[email protected]e60e47a2010-07-14 03:37:1894 // 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]0496f9a32010-09-30 16:08:0799 const BoundNetLog& net_log() const { return net_log_; }
100
[email protected]ab838892009-06-30 18:49:05101 protected:
[email protected]06650c52010-06-03 00:49:17102 void set_socket(ClientSocket* socket);
[email protected]2ab05b52009-07-01 23:57:58103 ClientSocket* socket() { return socket_.get(); }
[email protected]fd7b7c92009-08-20 19:38:30104 void NotifyDelegateOfCompletion(int rv);
[email protected]a796bcec2010-03-22 17:17:26105 void ResetTimer(base::TimeDelta remainingTime);
[email protected]ab838892009-06-30 18:49:05106
[email protected]ff579d42009-06-24 15:47:02107 private:
[email protected]974ebd62009-08-03 23:14:34108 virtual int ConnectInternal() = 0;
109
[email protected]06650c52010-06-03 00:49:17110 void LogConnectStart();
111 void LogConnectCompletion(int net_error);
112
[email protected]974ebd62009-08-03 23:14:34113 // Alerts the delegate that the ConnectJob has timed out.
114 void OnTimeout();
115
[email protected]2ab05b52009-07-01 23:57:58116 const std::string group_name_;
[email protected]974ebd62009-08-03 23:14:34117 const base::TimeDelta timeout_duration_;
118 // Timer to abort jobs that take too long.
119 base::OneShotTimer<ConnectJob> timer_;
120 Delegate* delegate_;
[email protected]2ab05b52009-07-01 23:57:58121 scoped_ptr<ClientSocket> socket_;
[email protected]9e743cd2010-03-16 07:03:53122 BoundNetLog net_log_;
[email protected]a2006ece2010-04-23 16:44:02123 // A ConnectJob is idle until Connect() has been called.
124 bool idle_;
[email protected]ab838892009-06-30 18:49:05125
[email protected]ff579d42009-06-24 15:47:02126 DISALLOW_COPY_AND_ASSIGN(ConnectJob);
127};
128
[email protected]d80a4322009-08-14 07:07:49129namespace 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.
136class ClientSocketPoolBaseHelper
[email protected]2431756e2010-09-29 20:26:13137 : public ConnectJob::Delegate,
[email protected]a554a8262010-05-20 00:13:52138 public NetworkChangeNotifier::Observer {
[email protected]ff579d42009-06-24 15:47:02139 public:
[email protected]d80a4322009-08-14 07:07:49140 class Request {
141 public:
[email protected]684970b2009-08-14 04:54:46142 Request(ClientSocketHandle* handle,
[email protected]ff579d42009-06-24 15:47:02143 CompletionCallback* callback,
[email protected]ac790b42009-12-02 04:31:31144 RequestPriority priority,
[email protected]9e743cd2010-03-16 07:03:53145 const BoundNetLog& net_log);
[email protected]ff579d42009-06-24 15:47:02146
[email protected]fd4fe0b2010-02-08 23:02:15147 virtual ~Request();
[email protected]d80a4322009-08-14 07:07:49148
149 ClientSocketHandle* handle() const { return handle_; }
150 CompletionCallback* callback() const { return callback_; }
[email protected]ac790b42009-12-02 04:31:31151 RequestPriority priority() const { return priority_; }
[email protected]9e743cd2010-03-16 07:03:53152 const BoundNetLog& net_log() const { return net_log_; }
[email protected]d80a4322009-08-14 07:07:49153
154 private:
155 ClientSocketHandle* const handle_;
156 CompletionCallback* const callback_;
[email protected]ac790b42009-12-02 04:31:31157 const RequestPriority priority_;
[email protected]9e743cd2010-03-16 07:03:53158 BoundNetLog net_log_;
[email protected]d80a4322009-08-14 07:07:49159
160 DISALLOW_COPY_AND_ASSIGN(Request);
[email protected]ff579d42009-06-24 15:47:02161 };
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]06650c52010-06-03 00:49:17171 ConnectJob::Delegate* delegate) const = 0;
[email protected]ff579d42009-06-24 15:47:02172
[email protected]a796bcec2010-03-22 17:17:26173 virtual base::TimeDelta ConnectionTimeout() const = 0;
174
[email protected]ff579d42009-06-24 15:47:02175 private:
176 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory);
177 };
178
[email protected]100d5fb92009-12-21 21:08:35179 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]66761b952010-06-25 21:30:38184 ConnectJobFactory* connect_job_factory);
[email protected]ff579d42009-06-24 15:47:02185
[email protected]2431756e2010-09-29 20:26:13186 ~ClientSocketPoolBaseHelper();
187
[email protected]d80a4322009-08-14 07:07:49188 // See ClientSocketPool::RequestSocket for documentation on this function.
[email protected]e7e99322010-05-04 23:30:17189 // ClientSocketPoolBaseHelper takes ownership of |request|, which must be
190 // heap allocated.
[email protected]d80a4322009-08-14 07:07:49191 int RequestSocket(const std::string& group_name, const Request* request);
[email protected]ff579d42009-06-24 15:47:02192
[email protected]d80a4322009-08-14 07:07:49193 // See ClientSocketPool::CancelRequest for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02194 void CancelRequest(const std::string& group_name,
[email protected]05ea9ff2010-07-15 19:08:21195 ClientSocketHandle* handle);
[email protected]ff579d42009-06-24 15:47:02196
[email protected]d80a4322009-08-14 07:07:49197 // See ClientSocketPool::ReleaseSocket for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02198 void ReleaseSocket(const std::string& group_name,
[email protected]a7e38572010-06-07 18:22:24199 ClientSocket* socket,
200 int id);
[email protected]ff579d42009-06-24 15:47:02201
[email protected]a7e38572010-06-07 18:22:24202 // See ClientSocketPool::Flush for documentation on this function.
203 void Flush();
[email protected]241c5c2c2010-06-21 18:46:00204
[email protected]d80a4322009-08-14 07:07:49205 // See ClientSocketPool::CloseIdleSockets for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02206 void CloseIdleSockets();
207
[email protected]d80a4322009-08-14 07:07:49208 // See ClientSocketPool::IdleSocketCount() for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02209 int idle_socket_count() const {
210 return idle_socket_count_;
211 }
212
[email protected]d80a4322009-08-14 07:07:49213 // See ClientSocketPool::IdleSocketCountInGroup() for documentation on this
214 // function.
[email protected]ff579d42009-06-24 15:47:02215 int IdleSocketCountInGroup(const std::string& group_name) const;
216
[email protected]d80a4322009-08-14 07:07:49217 // See ClientSocketPool::GetLoadState() for documentation on this function.
[email protected]ff579d42009-06-24 15:47:02218 LoadState GetLoadState(const std::string& group_name,
219 const ClientSocketHandle* handle) const;
220
[email protected]6b624c62010-03-14 08:37:32221 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]d80a4322009-08-14 07:07:49227 // ConnectJob::Delegate methods:
[email protected]2ab05b52009-07-01 23:57:58228 virtual void OnConnectJobComplete(int result, ConnectJob* job);
[email protected]ff579d42009-06-24 15:47:02229
[email protected]a554a8262010-05-20 00:13:52230 // NetworkChangeNotifier::Observer methods:
[email protected]66761b952010-06-25 21:30:38231 virtual void OnIPAddressChanged();
[email protected]a554a8262010-05-20 00:13:52232
[email protected]974ebd62009-08-03 23:14:34233 int NumConnectJobsInGroup(const std::string& group_name) const {
[email protected]aed99ef02010-08-26 14:04:32234 return group_map_.find(group_name)->second->jobs().size();
[email protected]974ebd62009-08-03 23:14:34235 }
[email protected]211d2172009-07-22 15:48:53236
[email protected]2abfe90a2010-08-25 17:49:51237 bool HasGroup(const std::string& group_name) const;
238
[email protected]9bf28db2009-08-29 01:35:16239 // 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]59d7a5a2010-08-30 16:44:27243 // See ClientSocketPool::GetInfoAsValue for documentation on this function.
[email protected]ba00b492010-09-08 14:53:38244 DictionaryValue* GetInfoAsValue(const std::string& name,
245 const std::string& type) const;
[email protected]59d7a5a2010-08-30 16:44:27246
[email protected]a796bcec2010-03-22 17:17:26247 base::TimeDelta ConnectionTimeout() const {
248 return connect_job_factory_->ConnectionTimeout();
249 }
250
[email protected]06d94042010-08-25 01:45:22251 static void set_connect_backup_jobs_enabled(bool enabled);
252 void EnableConnectBackupJobs();
[email protected]7c28e9a2010-03-20 01:16:13253
[email protected]ff579d42009-06-24 15:47:02254 private:
[email protected]5389bc72009-11-05 23:34:24255 friend class base::RefCounted<ClientSocketPoolBaseHelper>;
256
[email protected]ff579d42009-06-24 15:47:02257 // Entry for a persistent socket which became idle at time |start_time|.
258 struct IdleSocket {
[email protected]0f873e82010-09-02 16:09:01259 IdleSocket() : socket(NULL) {}
[email protected]ff579d42009-06-24 15:47:02260 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]9bf28db2009-08-29 01:35:16265 // |timeout| is the length of time to wait before timing out an idle socket.
[email protected]ff579d42009-06-24 15:47:02266 //
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]9bf28db2009-08-29 01:35:16271 bool ShouldCleanup(base::TimeTicks now, base::TimeDelta timeout) const;
[email protected]ff579d42009-06-24 15:47:02272 };
273
[email protected]d80a4322009-08-14 07:07:49274 typedef std::deque<const Request*> RequestQueue;
275 typedef std::map<const ClientSocketHandle*, const Request*> RequestMap;
[email protected]ff579d42009-06-24 15:47:02276
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]eb5a99382010-07-11 03:18:26279 // |active_socket_count| tracks the number of sockets held by clients.
[email protected]aed99ef02010-08-26 14:04:32280 class Group {
281 public:
282 Group();
283 ~Group();
[email protected]2ab05b52009-07-01 23:57:58284
285 bool IsEmpty() const {
[email protected]aed99ef02010-08-26 14:04:32286 return active_socket_count_ == 0 && idle_sockets_.empty() &&
287 jobs_.empty() && pending_requests_.empty();
[email protected]2ab05b52009-07-01 23:57:58288 }
289
290 bool HasAvailableSocketSlot(int max_sockets_per_group) const {
[email protected]aed99ef02010-08-26 14:04:32291 return active_socket_count_ + static_cast<int>(jobs_.size()) <
[email protected]2ab05b52009-07-01 23:57:58292 max_sockets_per_group;
293 }
294
[email protected]05ea9ff2010-07-15 19:08:21295 bool IsStalled(int max_sockets_per_group) const {
296 return HasAvailableSocketSlot(max_sockets_per_group) &&
[email protected]aed99ef02010-08-26 14:04:32297 pending_requests_.size() > jobs_.size();
[email protected]05ea9ff2010-07-15 19:08:21298 }
299
[email protected]ac790b42009-12-02 04:31:31300 RequestPriority TopPendingPriority() const {
[email protected]aed99ef02010-08-26 14:04:32301 return pending_requests_.front()->priority();
[email protected]211d2172009-07-22 15:48:53302 }
303
[email protected]aed99ef02010-08-26 14:04:32304 bool HasBackupJob() const { return !method_factory_.empty(); }
305
[email protected]6b624c62010-03-14 08:37:32306 void CleanupBackupJob() {
[email protected]aed99ef02010-08-26 14:04:32307 method_factory_.RevokeAll();
[email protected]6b624c62010-03-14 08:37:32308 }
309
[email protected]aed99ef02010-08-26 14:04:32310 // 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]e1b54dc2010-10-06 21:27:22327 const std::list<IdleSocket>& idle_sockets() const { return idle_sockets_; }
[email protected]aed99ef02010-08-26 14:04:32328 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]e1b54dc2010-10-06 21:27:22331 std::list<IdleSocket>* mutable_idle_sockets() { return &idle_sockets_; }
[email protected]aed99ef02010-08-26 14:04:32332
333 private:
[email protected]e1b54dc2010-10-06 21:27:22334 std::list<IdleSocket> idle_sockets_;
[email protected]aed99ef02010-08-26 14:04:32335 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]ff579d42009-06-24 15:47:02340 };
341
[email protected]aed99ef02010-08-26 14:04:32342 typedef std::map<std::string, Group*> GroupMap;
[email protected]ff579d42009-06-24 15:47:02343
[email protected]5fc08e32009-07-15 17:09:57344 typedef std::set<const ConnectJob*> ConnectJobSet;
[email protected]ff579d42009-06-24 15:47:02345
[email protected]05ea9ff2010-07-15 19:08:21346 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]d80a4322009-08-14 07:07:49358 static void InsertRequestIntoQueue(const Request* r,
[email protected]ff579d42009-06-24 15:47:02359 RequestQueue* pending_requests);
[email protected]fd7b7c92009-08-20 19:38:30360 static const Request* RemoveRequestFromQueue(RequestQueue::iterator it,
[email protected]3f00be82010-09-27 19:50:02361 Group* group);
[email protected]ff579d42009-06-24 15:47:02362
[email protected]aed99ef02010-08-26 14:04:32363 Group* GetOrCreateGroup(const std::string& group_name);
364 void RemoveGroup(const std::string& group_name);
365 void RemoveGroup(GroupMap::iterator it);
366
[email protected]ff579d42009-06-24 15:47:02367 // Called when the number of idle sockets changes.
368 void IncrementIdleCount();
369 void DecrementIdleCount();
370
[email protected]211d2172009-07-22 15:48:53371 // Scans the group map for groups which have an available socket slot and
[email protected]05ea9ff2010-07-15 19:08:21372 // 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]211d2172009-07-22 15:48:53374 // having highest priority.
[email protected]05ea9ff2010-07-15 19:08:21375 bool FindTopStalledGroup(Group** group, std::string* group_name);
[email protected]211d2172009-07-22 15:48:53376
[email protected]ff579d42009-06-24 15:47:02377 // 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]4d3b05d2010-01-27 21:27:29383 // Removes |job| from |connect_job_set_|. Also updates |group| if non-NULL.
384 void RemoveConnectJob(const ConnectJob* job, Group* group);
[email protected]ff579d42009-06-24 15:47:02385
[email protected]05ea9ff2010-07-15 19:08:21386 // Tries to see if we can handle any more requests for |group|.
387 void OnAvailableSocketSlot(const std::string& group_name, Group* group);
[email protected]8ae03f42010-07-07 19:08:10388
[email protected]eb5a99382010-07-11 03:18:26389 // Process a pending socket request for a group.
[email protected]05ea9ff2010-07-15 19:08:21390 void ProcessPendingRequest(const std::string& group_name, Group* group);
[email protected]ff579d42009-06-24 15:47:02391
[email protected]2ab05b52009-07-01 23:57:58392 // Assigns |socket| to |handle| and updates |group|'s counters appropriately.
393 void HandOutSocket(ClientSocket* socket,
394 bool reused,
395 ClientSocketHandle* handle,
[email protected]f9d285c2009-08-17 19:54:29396 base::TimeDelta time_idle,
[email protected]fd4fe0b2010-02-08 23:02:15397 Group* group,
[email protected]9e743cd2010-03-16 07:03:53398 const BoundNetLog& net_log);
[email protected]2ab05b52009-07-01 23:57:58399
[email protected]0f873e82010-09-02 16:09:01400 // Adds |socket| to the list of idle sockets for |group|.
401 void AddIdleSocket(ClientSocket* socket, Group* group);
[email protected]5fc08e32009-07-15 17:09:57402
[email protected]06f92462010-08-31 19:24:14403 // Iterates through |group_map_|, canceling all ConnectJobs and deleting
404 // groups if they are no longer needed.
[email protected]5fc08e32009-07-15 17:09:57405 void CancelAllConnectJobs();
406
[email protected]06f92462010-08-31 19:24:14407 // 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]211d2172009-07-22 15:48:53411 // Returns true if we can't create any more sockets due to the total limit.
[email protected]211d2172009-07-22 15:48:53412 bool ReachedMaxSocketsLimit() const;
413
[email protected]fd4fe0b2010-02-08 23:02:15414 // This is the internal implementation of RequestSocket(). It differs in that
[email protected]9e743cd2010-03-16 07:03:53415 // it does not handle logging into NetLog of the queueing status of
[email protected]fd4fe0b2010-02-08 23:02:15416 // |request|.
417 int RequestSocketInternal(const std::string& group_name,
418 const Request* request);
419
[email protected]eb5a99382010-07-11 03:18:26420 // Assigns an idle socket for the group to the request.
421 // Returns |true| if an idle socket is available, false otherwise.
[email protected]aed99ef02010-08-26 14:04:32422 bool AssignIdleSocketToGroup(const Request* request, Group* group);
[email protected]eb5a99382010-07-11 03:18:26423
[email protected]06650c52010-06-03 00:49:17424 static void LogBoundConnectJobToRequest(
425 const NetLog::Source& connect_job_source, const Request* request);
426
[email protected]43a21b82010-06-10 21:30:54427 // 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]eb5a99382010-07-11 03:18:26434 // Checks if there are stalled socket groups that should be notified
435 // for possible wakeup.
436 void CheckForStalledSocketGroups();
437
[email protected]05ea9ff2010-07-15 19:08:21438 // 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]eb5a99382010-07-11 03:18:26449
[email protected]ff579d42009-06-24 15:47:02450 GroupMap group_map_;
451
[email protected]05ea9ff2010-07-15 19:08:21452 // 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]ff579d42009-06-24 15:47:02457 // Timer used to periodically prune idle sockets that timed out or can't be
458 // reused.
[email protected]d80a4322009-08-14 07:07:49459 base::RepeatingTimer<ClientSocketPoolBaseHelper> timer_;
[email protected]ff579d42009-06-24 15:47:02460
461 // The total number of idle sockets in the system.
462 int idle_socket_count_;
463
[email protected]211d2172009-07-22 15:48:53464 // 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]ff579d42009-06-24 15:47:02473 // The maximum number of sockets kept per group.
474 const int max_sockets_per_group_;
475
[email protected]9bf28db2009-08-29 01:35:16476 // 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]ab838892009-06-30 18:49:05480 const scoped_ptr<ConnectJobFactory> connect_job_factory_;
[email protected]ff579d42009-06-24 15:47:02481
[email protected]7c28e9a2010-03-20 01:16:13482 // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool
[email protected]06d94042010-08-25 01:45:22483 bool connect_backup_jobs_enabled_;
[email protected]7c28e9a2010-03-20 01:16:13484
[email protected]a7e38572010-06-07 18:22:24485 // 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]09d6ecb02010-07-22 20:10:45489
[email protected]2431756e2010-09-29 20:26:13490 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_;
491
[email protected]3598c6022010-09-17 23:13:09492 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBaseHelper);
[email protected]d80a4322009-08-14 07:07:49493};
494
495} // namespace internal
496
[email protected]9bf28db2009-08-29 01:35:16497// The maximum duration, in seconds, to keep used idle persistent sockets alive.
498static const int kUsedIdleSocketTimeout = 300; // 5 minutes
499
[email protected]d80a4322009-08-14 07:07:49500template <typename SocketParams>
501class ClientSocketPoolBase {
502 public:
503 class Request : public internal::ClientSocketPoolBaseHelper::Request {
504 public:
505 Request(ClientSocketHandle* handle,
506 CompletionCallback* callback,
[email protected]ac790b42009-12-02 04:31:31507 RequestPriority priority,
[email protected]df4b4ef2010-07-12 18:25:21508 const scoped_refptr<SocketParams>& params,
[email protected]9e743cd2010-03-16 07:03:53509 const BoundNetLog& net_log)
[email protected]d80a4322009-08-14 07:07:49510 : internal::ClientSocketPoolBaseHelper::Request(
[email protected]9008c86f2010-08-06 07:10:24511 handle, callback, priority, net_log),
[email protected]d80a4322009-08-14 07:07:49512 params_(params) {}
513
[email protected]df4b4ef2010-07-12 18:25:21514 const scoped_refptr<SocketParams>& params() const { return params_; }
[email protected]d80a4322009-08-14 07:07:49515
516 private:
[email protected]df4b4ef2010-07-12 18:25:21517 scoped_refptr<SocketParams> params_;
[email protected]d80a4322009-08-14 07:07:49518 };
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]06650c52010-06-03 00:49:17528 ConnectJob::Delegate* delegate) const = 0;
[email protected]d80a4322009-08-14 07:07:49529
[email protected]a796bcec2010-03-22 17:17:26530 virtual base::TimeDelta ConnectionTimeout() const = 0;
531
[email protected]d80a4322009-08-14 07:07:49532 private:
533 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory);
534 };
535
[email protected]9bf28db2009-08-29 01:35:16536 // |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]100d5fb92009-12-21 21:08:35542 ClientSocketPoolBase(
543 int max_sockets,
544 int max_sockets_per_group,
[email protected]2431756e2010-09-29 20:26:13545 ClientSocketPoolHistograms* histograms,
[email protected]100d5fb92009-12-21 21:08:35546 base::TimeDelta unused_idle_socket_timeout,
547 base::TimeDelta used_idle_socket_timeout,
[email protected]66761b952010-06-25 21:30:38548 ConnectJobFactory* connect_job_factory)
[email protected]b89f7e42010-05-20 20:37:00549 : histograms_(histograms),
[email protected]2431756e2010-09-29 20:26:13550 helper_(max_sockets, max_sockets_per_group,
551 unused_idle_socket_timeout, used_idle_socket_timeout,
552 new ConnectJobFactoryAdaptor(connect_job_factory)) {}
[email protected]d80a4322009-08-14 07:07:49553
[email protected]20cb5f482009-12-16 01:01:25554 virtual ~ClientSocketPoolBase() {}
[email protected]d80a4322009-08-14 07:07:49555
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]df4b4ef2010-07-12 18:25:21562 const scoped_refptr<SocketParams>& params,
[email protected]ac790b42009-12-02 04:31:31563 RequestPriority priority,
[email protected]d80a4322009-08-14 07:07:49564 ClientSocketHandle* handle,
565 CompletionCallback* callback,
[email protected]9e743cd2010-03-16 07:03:53566 const BoundNetLog& net_log) {
[email protected]e7e99322010-05-04 23:30:17567 Request* request = new Request(handle, callback, priority, params, net_log);
[email protected]2431756e2010-09-29 20:26:13568 return helper_.RequestSocket(group_name, request);
[email protected]d80a4322009-08-14 07:07:49569 }
570
571 void CancelRequest(const std::string& group_name,
[email protected]05ea9ff2010-07-15 19:08:21572 ClientSocketHandle* handle) {
[email protected]2431756e2010-09-29 20:26:13573 return helper_.CancelRequest(group_name, handle);
[email protected]d80a4322009-08-14 07:07:49574 }
575
[email protected]241c5c2c2010-06-21 18:46:00576 void ReleaseSocket(const std::string& group_name, ClientSocket* socket,
577 int id) {
[email protected]2431756e2010-09-29 20:26:13578 return helper_.ReleaseSocket(group_name, socket, id);
[email protected]d80a4322009-08-14 07:07:49579 }
580
[email protected]2431756e2010-09-29 20:26:13581 void CloseIdleSockets() { return helper_.CloseIdleSockets(); }
[email protected]d80a4322009-08-14 07:07:49582
[email protected]2431756e2010-09-29 20:26:13583 int idle_socket_count() const { return helper_.idle_socket_count(); }
[email protected]d80a4322009-08-14 07:07:49584
585 int IdleSocketCountInGroup(const std::string& group_name) const {
[email protected]2431756e2010-09-29 20:26:13586 return helper_.IdleSocketCountInGroup(group_name);
[email protected]d80a4322009-08-14 07:07:49587 }
588
589 LoadState GetLoadState(const std::string& group_name,
590 const ClientSocketHandle* handle) const {
[email protected]2431756e2010-09-29 20:26:13591 return helper_.GetLoadState(group_name, handle);
[email protected]d80a4322009-08-14 07:07:49592 }
593
594 virtual void OnConnectJobComplete(int result, ConnectJob* job) {
[email protected]2431756e2010-09-29 20:26:13595 return helper_.OnConnectJobComplete(result, job);
[email protected]d80a4322009-08-14 07:07:49596 }
597
[email protected]d80a4322009-08-14 07:07:49598 int NumConnectJobsInGroup(const std::string& group_name) const {
[email protected]2431756e2010-09-29 20:26:13599 return helper_.NumConnectJobsInGroup(group_name);
[email protected]d80a4322009-08-14 07:07:49600 }
601
[email protected]2abfe90a2010-08-25 17:49:51602 bool HasGroup(const std::string& group_name) const {
[email protected]2431756e2010-09-29 20:26:13603 return helper_.HasGroup(group_name);
[email protected]2abfe90a2010-08-25 17:49:51604 }
605
[email protected]9bf28db2009-08-29 01:35:16606 void CleanupIdleSockets(bool force) {
[email protected]2431756e2010-09-29 20:26:13607 return helper_.CleanupIdleSockets(force);
[email protected]9bf28db2009-08-29 01:35:16608 }
609
[email protected]ba00b492010-09-08 14:53:38610 DictionaryValue* GetInfoAsValue(const std::string& name,
611 const std::string& type) const {
[email protected]2431756e2010-09-29 20:26:13612 return helper_.GetInfoAsValue(name, type);
[email protected]59d7a5a2010-08-30 16:44:27613 }
614
[email protected]a796bcec2010-03-22 17:17:26615 base::TimeDelta ConnectionTimeout() const {
[email protected]2431756e2010-09-29 20:26:13616 return helper_.ConnectionTimeout();
[email protected]a796bcec2010-03-22 17:17:26617 }
618
[email protected]2431756e2010-09-29 20:26:13619 ClientSocketPoolHistograms* histograms() const {
[email protected]b89f7e42010-05-20 20:37:00620 return histograms_;
621 }
[email protected]a796bcec2010-03-22 17:17:26622
[email protected]2431756e2010-09-29 20:26:13623 void EnableConnectBackupJobs() { helper_.EnableConnectBackupJobs(); }
[email protected]7c28e9a2010-03-20 01:16:13624
[email protected]2431756e2010-09-29 20:26:13625 void Flush() { helper_.Flush(); }
[email protected]a7e38572010-06-07 18:22:24626
[email protected]d80a4322009-08-14 07:07:49627 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]9008c86f2010-08-06 07:10:24639 explicit ConnectJobFactoryAdaptor(ConnectJobFactory* connect_job_factory)
[email protected]d80a4322009-08-14 07:07:49640 : 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]06650c52010-06-03 00:49:17646 ConnectJob::Delegate* delegate) const {
[email protected]d80a4322009-08-14 07:07:49647 const Request* casted_request = static_cast<const Request*>(&request);
648 return connect_job_factory_->NewConnectJob(
[email protected]06650c52010-06-03 00:49:17649 group_name, *casted_request, delegate);
[email protected]d80a4322009-08-14 07:07:49650 }
651
[email protected]a796bcec2010-03-22 17:17:26652 virtual base::TimeDelta ConnectionTimeout() const {
653 return connect_job_factory_->ConnectionTimeout();
654 }
655
[email protected]d80a4322009-08-14 07:07:49656 const scoped_ptr<ConnectJobFactory> connect_job_factory_;
657 };
658
[email protected]b89f7e42010-05-20 20:37:00659 // Histograms for the pool
[email protected]2431756e2010-09-29 20:26:13660 ClientSocketPoolHistograms* const histograms_;
661 internal::ClientSocketPoolBaseHelper helper_;
[email protected]d80a4322009-08-14 07:07:49662
[email protected]ff579d42009-06-24 15:47:02663 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase);
664};
665
[email protected]ff579d42009-06-24 15:47:02666} // namespace net
667
668#endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_