OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A ClientSocketPoolBase is used to restrict the number of sockets open at | 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. | 6 // a time. It also maintains a list of idle persistent sockets for reuse. |
7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle | 7 // Subclasses of ClientSocketPool should compose ClientSocketPoolBase to handle |
8 // the core logic of (1) restricting the number of active (connected or | 8 // the core logic of (1) restricting the number of active (connected or |
9 // connecting) sockets per "group" (generally speaking, the hostname), (2) | 9 // connecting) sockets per "group" (generally speaking, the hostname), (2) |
10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) | 10 // maintaining a per-group list of idle, persistent sockets for reuse, and (3) |
(...skipping 21 matching lines...) Expand all Loading... |
32 #include "base/ref_counted.h" | 32 #include "base/ref_counted.h" |
33 #include "base/scoped_ptr.h" | 33 #include "base/scoped_ptr.h" |
34 #include "base/task.h" | 34 #include "base/task.h" |
35 #include "base/time.h" | 35 #include "base/time.h" |
36 #include "base/timer.h" | 36 #include "base/timer.h" |
37 #include "net/base/address_list.h" | 37 #include "net/base/address_list.h" |
38 #include "net/base/completion_callback.h" | 38 #include "net/base/completion_callback.h" |
39 #include "net/base/load_states.h" | 39 #include "net/base/load_states.h" |
40 #include "net/base/net_errors.h" | 40 #include "net/base/net_errors.h" |
41 #include "net/base/net_log.h" | 41 #include "net/base/net_log.h" |
42 #include "net/base/network_change_notifier.h" | |
43 #include "net/base/request_priority.h" | 42 #include "net/base/request_priority.h" |
44 #include "net/socket/client_socket.h" | 43 #include "net/socket/client_socket.h" |
45 #include "net/socket/client_socket_pool.h" | 44 #include "net/socket/client_socket_pool.h" |
46 | 45 |
47 namespace net { | 46 namespace net { |
48 | 47 |
49 class ClientSocketHandle; | 48 class ClientSocketHandle; |
50 | 49 |
51 // ConnectJob provides an abstract interface for "connecting" a socket. | 50 // ConnectJob provides an abstract interface for "connecting" a socket. |
52 // The connection may involve host resolution, tcp connection, ssl connection, | 51 // The connection may involve host resolution, tcp connection, ssl connection, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 | 114 |
116 namespace internal { | 115 namespace internal { |
117 | 116 |
118 // ClientSocketPoolBaseHelper is an internal class that implements almost all | 117 // ClientSocketPoolBaseHelper is an internal class that implements almost all |
119 // the functionality from ClientSocketPoolBase without using templates. | 118 // the functionality from ClientSocketPoolBase without using templates. |
120 // ClientSocketPoolBase adds templated definitions built on top of | 119 // ClientSocketPoolBase adds templated definitions built on top of |
121 // ClientSocketPoolBaseHelper. This class is not for external use, please use | 120 // ClientSocketPoolBaseHelper. This class is not for external use, please use |
122 // ClientSocketPoolBase instead. | 121 // ClientSocketPoolBase instead. |
123 class ClientSocketPoolBaseHelper | 122 class ClientSocketPoolBaseHelper |
124 : public base::RefCounted<ClientSocketPoolBaseHelper>, | 123 : public base::RefCounted<ClientSocketPoolBaseHelper>, |
125 public ConnectJob::Delegate, | 124 public ConnectJob::Delegate { |
126 public NetworkChangeNotifier::Observer { | |
127 public: | 125 public: |
128 class Request { | 126 class Request { |
129 public: | 127 public: |
130 Request(ClientSocketHandle* handle, | 128 Request(ClientSocketHandle* handle, |
131 CompletionCallback* callback, | 129 CompletionCallback* callback, |
132 RequestPriority priority, | 130 RequestPriority priority, |
133 const BoundNetLog& net_log); | 131 const BoundNetLog& net_log); |
134 | 132 |
135 virtual ~Request(); | 133 virtual ~Request(); |
136 | 134 |
(...skipping 26 matching lines...) Expand all Loading... |
163 | 161 |
164 private: | 162 private: |
165 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); | 163 DISALLOW_COPY_AND_ASSIGN(ConnectJobFactory); |
166 }; | 164 }; |
167 | 165 |
168 ClientSocketPoolBaseHelper( | 166 ClientSocketPoolBaseHelper( |
169 int max_sockets, | 167 int max_sockets, |
170 int max_sockets_per_group, | 168 int max_sockets_per_group, |
171 base::TimeDelta unused_idle_socket_timeout, | 169 base::TimeDelta unused_idle_socket_timeout, |
172 base::TimeDelta used_idle_socket_timeout, | 170 base::TimeDelta used_idle_socket_timeout, |
173 ConnectJobFactory* connect_job_factory, | 171 ConnectJobFactory* connect_job_factory); |
174 NetworkChangeNotifier* network_change_notifier); | |
175 | 172 |
176 // See ClientSocketPool::RequestSocket for documentation on this function. | 173 // See ClientSocketPool::RequestSocket for documentation on this function. |
177 // Note that |request| must be heap allocated. If ERR_IO_PENDING is returned, | 174 // Note that |request| must be heap allocated. If ERR_IO_PENDING is returned, |
178 // then ClientSocketPoolBaseHelper takes ownership of |request|. | 175 // then ClientSocketPoolBaseHelper takes ownership of |request|. |
179 int RequestSocket(const std::string& group_name, const Request* request); | 176 int RequestSocket(const std::string& group_name, const Request* request); |
180 | 177 |
181 // See ClientSocketPool::CancelRequest for documentation on this function. | 178 // See ClientSocketPool::CancelRequest for documentation on this function. |
182 void CancelRequest(const std::string& group_name, | 179 void CancelRequest(const std::string& group_name, |
183 const ClientSocketHandle* handle); | 180 const ClientSocketHandle* handle); |
184 | 181 |
(...skipping 19 matching lines...) Expand all Loading... |
204 | 201 |
205 int ConnectRetryIntervalMs() const { | 202 int ConnectRetryIntervalMs() const { |
206 // TODO(mbelshe): Make this tuned dynamically based on measured RTT. | 203 // TODO(mbelshe): Make this tuned dynamically based on measured RTT. |
207 // For now, just use the max retry interval. | 204 // For now, just use the max retry interval. |
208 return ClientSocketPool::kMaxConnectRetryIntervalMs; | 205 return ClientSocketPool::kMaxConnectRetryIntervalMs; |
209 } | 206 } |
210 | 207 |
211 // ConnectJob::Delegate methods: | 208 // ConnectJob::Delegate methods: |
212 virtual void OnConnectJobComplete(int result, ConnectJob* job); | 209 virtual void OnConnectJobComplete(int result, ConnectJob* job); |
213 | 210 |
214 // NetworkChangeNotifier::Observer methods: | |
215 virtual void OnIPAddressChanged(); | |
216 | |
217 // For testing. | 211 // For testing. |
218 bool may_have_stalled_group() const { return may_have_stalled_group_; } | 212 bool may_have_stalled_group() const { return may_have_stalled_group_; } |
219 | 213 |
220 int NumConnectJobsInGroup(const std::string& group_name) const { | 214 int NumConnectJobsInGroup(const std::string& group_name) const { |
221 return group_map_.find(group_name)->second.jobs.size(); | 215 return group_map_.find(group_name)->second.jobs.size(); |
222 } | 216 } |
223 | 217 |
224 // Closes all idle sockets if |force| is true. Else, only closes idle | 218 // Closes all idle sockets if |force| is true. Else, only closes idle |
225 // sockets that timed out or can't be reused. Made public for testing. | 219 // sockets that timed out or can't be reused. Made public for testing. |
226 void CleanupIdleSockets(bool force); | 220 void CleanupIdleSockets(bool force); |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
427 // |max_sockets_per_group_| limit. So choosing the next request involves | 421 // |max_sockets_per_group_| limit. So choosing the next request involves |
428 // selecting the highest priority request across *all* groups. | 422 // selecting the highest priority request across *all* groups. |
429 // | 423 // |
430 // Since reaching the maximum number of sockets is an edge case, we make note | 424 // Since reaching the maximum number of sockets is an edge case, we make note |
431 // of when it happens, and thus avoid doing the slower "scan all groups" | 425 // of when it happens, and thus avoid doing the slower "scan all groups" |
432 // in the common case. | 426 // in the common case. |
433 bool may_have_stalled_group_; | 427 bool may_have_stalled_group_; |
434 | 428 |
435 const scoped_ptr<ConnectJobFactory> connect_job_factory_; | 429 const scoped_ptr<ConnectJobFactory> connect_job_factory_; |
436 | 430 |
437 NetworkChangeNotifier* const network_change_notifier_; | |
438 | |
439 // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool | 431 // TODO(vandebo) Remove when backup jobs move to TCPClientSocketPool |
440 bool backup_jobs_enabled_; | 432 bool backup_jobs_enabled_; |
441 | 433 |
442 // A factory to pin the backup_job tasks. | 434 // A factory to pin the backup_job tasks. |
443 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; | 435 ScopedRunnableMethodFactory<ClientSocketPoolBaseHelper> method_factory_; |
444 }; | 436 }; |
445 | 437 |
446 } // namespace internal | 438 } // namespace internal |
447 | 439 |
448 // The maximum duration, in seconds, to keep unused idle persistent sockets | 440 // The maximum duration, in seconds, to keep unused idle persistent sockets |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
495 // sockets a "group" can have. |unused_idle_socket_timeout| specifies how | 487 // sockets a "group" can have. |unused_idle_socket_timeout| specifies how |
496 // long to leave an unused idle socket open before closing it. | 488 // long to leave an unused idle socket open before closing it. |
497 // |used_idle_socket_timeout| specifies how long to leave a previously used | 489 // |used_idle_socket_timeout| specifies how long to leave a previously used |
498 // idle socket open before closing it. | 490 // idle socket open before closing it. |
499 ClientSocketPoolBase( | 491 ClientSocketPoolBase( |
500 int max_sockets, | 492 int max_sockets, |
501 int max_sockets_per_group, | 493 int max_sockets_per_group, |
502 const std::string& name, | 494 const std::string& name, |
503 base::TimeDelta unused_idle_socket_timeout, | 495 base::TimeDelta unused_idle_socket_timeout, |
504 base::TimeDelta used_idle_socket_timeout, | 496 base::TimeDelta used_idle_socket_timeout, |
505 ConnectJobFactory* connect_job_factory, | 497 ConnectJobFactory* connect_job_factory) |
506 NetworkChangeNotifier* network_change_notifier) | |
507 : name_(name), | 498 : name_(name), |
508 helper_(new internal::ClientSocketPoolBaseHelper( | 499 helper_(new internal::ClientSocketPoolBaseHelper( |
509 max_sockets, max_sockets_per_group, | 500 max_sockets, max_sockets_per_group, |
510 unused_idle_socket_timeout, used_idle_socket_timeout, | 501 unused_idle_socket_timeout, used_idle_socket_timeout, |
511 new ConnectJobFactoryAdaptor(connect_job_factory), | 502 new ConnectJobFactoryAdaptor(connect_job_factory))) {} |
512 network_change_notifier)) {} | |
513 | 503 |
514 virtual ~ClientSocketPoolBase() {} | 504 virtual ~ClientSocketPoolBase() {} |
515 | 505 |
516 // These member functions simply forward to ClientSocketPoolBaseHelper. | 506 // These member functions simply forward to ClientSocketPoolBaseHelper. |
517 | 507 |
518 // RequestSocket bundles up the parameters into a Request and then forwards to | 508 // RequestSocket bundles up the parameters into a Request and then forwards to |
519 // ClientSocketPoolBaseHelper::RequestSocket(). Note that the memory | 509 // ClientSocketPoolBaseHelper::RequestSocket(). Note that the memory |
520 // ownership is transferred in the asynchronous (ERR_IO_PENDING) case. | 510 // ownership is transferred in the asynchronous (ERR_IO_PENDING) case. |
521 int RequestSocket(const std::string& group_name, | 511 int RequestSocket(const std::string& group_name, |
522 const SocketParams& params, | 512 const SocketParams& params, |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
622 // the posting of the task and the execution, then we'll hit the DCHECK that | 612 // the posting of the task and the execution, then we'll hit the DCHECK that |
623 // |ClientSocketPoolBaseHelper::group_map_| is empty. | 613 // |ClientSocketPoolBaseHelper::group_map_| is empty. |
624 scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_; | 614 scoped_refptr<internal::ClientSocketPoolBaseHelper> helper_; |
625 | 615 |
626 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); | 616 DISALLOW_COPY_AND_ASSIGN(ClientSocketPoolBase); |
627 }; | 617 }; |
628 | 618 |
629 } // namespace net | 619 } // namespace net |
630 | 620 |
631 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ | 621 #endif // NET_SOCKET_CLIENT_SOCKET_POOL_BASE_H_ |
OLD | NEW |