| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "net/socket/client_socket_pool_base.h" | 5 #include "net/socket/client_socket_pool_base.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 flags_(flags), | 169 flags_(flags), |
| 170 net_log_(net_log) {} | 170 net_log_(net_log) {} |
| 171 | 171 |
| 172 ClientSocketPoolBaseHelper::Request::~Request() {} | 172 ClientSocketPoolBaseHelper::Request::~Request() {} |
| 173 | 173 |
| 174 ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( | 174 ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( |
| 175 int max_sockets, | 175 int max_sockets, |
| 176 int max_sockets_per_group, | 176 int max_sockets_per_group, |
| 177 base::TimeDelta unused_idle_socket_timeout, | 177 base::TimeDelta unused_idle_socket_timeout, |
| 178 base::TimeDelta used_idle_socket_timeout, | 178 base::TimeDelta used_idle_socket_timeout, |
| 179 ConnectJobFactory* connect_job_factory) | 179 ConnectJobFactory* connect_job_factory, |
| 180 const ClientSocketPoolOptions& options) |
| 180 : idle_socket_count_(0), | 181 : idle_socket_count_(0), |
| 181 connecting_socket_count_(0), | 182 connecting_socket_count_(0), |
| 182 handed_out_socket_count_(0), | 183 handed_out_socket_count_(0), |
| 183 max_sockets_(max_sockets), | 184 max_sockets_(max_sockets), |
| 184 max_sockets_per_group_(max_sockets_per_group), | 185 max_sockets_per_group_(max_sockets_per_group), |
| 186 use_cleanup_timer_(options.use_cleanup_timer), |
| 185 unused_idle_socket_timeout_(unused_idle_socket_timeout), | 187 unused_idle_socket_timeout_(unused_idle_socket_timeout), |
| 186 used_idle_socket_timeout_(used_idle_socket_timeout), | 188 used_idle_socket_timeout_(used_idle_socket_timeout), |
| 187 connect_job_factory_(connect_job_factory), | 189 connect_job_factory_(connect_job_factory), |
| 188 connect_backup_jobs_enabled_(false), | 190 connect_backup_jobs_enabled_(false), |
| 189 pool_generation_number_(0), | 191 pool_generation_number_(0), |
| 190 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 192 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 191 DCHECK_LE(0, max_sockets_per_group); | 193 DCHECK_LE(0, max_sockets_per_group); |
| 192 DCHECK_LE(max_sockets_per_group, max_sockets); | 194 DCHECK_LE(max_sockets_per_group, max_sockets); |
| 193 | 195 |
| 194 NetworkChangeNotifier::AddIPAddressObserver(this); | 196 NetworkChangeNotifier::AddIPAddressObserver(this); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 group->CleanupBackupJob(); | 232 group->CleanupBackupJob(); |
| 231 return req; | 233 return req; |
| 232 } | 234 } |
| 233 | 235 |
| 234 int ClientSocketPoolBaseHelper::RequestSocket( | 236 int ClientSocketPoolBaseHelper::RequestSocket( |
| 235 const std::string& group_name, | 237 const std::string& group_name, |
| 236 const Request* request) { | 238 const Request* request) { |
| 237 CHECK(request->callback()); | 239 CHECK(request->callback()); |
| 238 CHECK(request->handle()); | 240 CHECK(request->handle()); |
| 239 | 241 |
| 242 // Cleanup any timed-out idle sockets if no timer is used. |
| 243 if (!use_cleanup_timer_) |
| 244 CleanupIdleSockets(false); |
| 245 |
| 240 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL); | 246 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL); |
| 241 Group* group = GetOrCreateGroup(group_name); | 247 Group* group = GetOrCreateGroup(group_name); |
| 242 | 248 |
| 243 int rv = RequestSocketInternal(group_name, request); | 249 int rv = RequestSocketInternal(group_name, request); |
| 244 if (rv != ERR_IO_PENDING) { | 250 if (rv != ERR_IO_PENDING) { |
| 245 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); | 251 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); |
| 246 CHECK(!request->handle()->is_initialized()); | 252 CHECK(!request->handle()->is_initialized()); |
| 247 delete request; | 253 delete request; |
| 248 } else { | 254 } else { |
| 249 InsertRequestIntoQueue(request, group->mutable_pending_requests()); | 255 InsertRequestIntoQueue(request, group->mutable_pending_requests()); |
| 250 } | 256 } |
| 251 return rv; | 257 return rv; |
| 252 } | 258 } |
| 253 | 259 |
| 254 void ClientSocketPoolBaseHelper::RequestSockets( | 260 void ClientSocketPoolBaseHelper::RequestSockets( |
| 255 const std::string& group_name, | 261 const std::string& group_name, |
| 256 const Request& request, | 262 const Request& request, |
| 257 int num_sockets) { | 263 int num_sockets) { |
| 258 DCHECK(!request.callback()); | 264 DCHECK(!request.callback()); |
| 259 DCHECK(!request.handle()); | 265 DCHECK(!request.handle()); |
| 260 | 266 |
| 267 // Cleanup any timed out idle sockets if no timer is used. |
| 268 if (!use_cleanup_timer_) |
| 269 CleanupIdleSockets(false); |
| 270 |
| 261 if (num_sockets > max_sockets_per_group_) { | 271 if (num_sockets > max_sockets_per_group_) { |
| 262 num_sockets = max_sockets_per_group_; | 272 num_sockets = max_sockets_per_group_; |
| 263 } | 273 } |
| 264 | 274 |
| 265 request.net_log().BeginEvent( | 275 request.net_log().BeginEvent( |
| 266 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, | 276 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, |
| 267 make_scoped_refptr(new NetLogIntegerParameter( | 277 make_scoped_refptr(new NetLogIntegerParameter( |
| 268 "num_sockets", num_sockets))); | 278 "num_sockets", num_sockets))); |
| 269 | 279 |
| 270 Group* group = GetOrCreateGroup(group_name); | 280 Group* group = GetOrCreateGroup(group_name); |
| (...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 bool old_value = g_connect_backup_jobs_enabled; | 699 bool old_value = g_connect_backup_jobs_enabled; |
| 690 g_connect_backup_jobs_enabled = enabled; | 700 g_connect_backup_jobs_enabled = enabled; |
| 691 return old_value; | 701 return old_value; |
| 692 } | 702 } |
| 693 | 703 |
| 694 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { | 704 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
| 695 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; | 705 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
| 696 } | 706 } |
| 697 | 707 |
| 698 void ClientSocketPoolBaseHelper::IncrementIdleCount() { | 708 void ClientSocketPoolBaseHelper::IncrementIdleCount() { |
| 699 if (++idle_socket_count_ == 1) | 709 if (++idle_socket_count_ == 1 && use_cleanup_timer_) |
| 700 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, | 710 StartIdleSocketTimer(); |
| 701 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); | |
| 702 } | 711 } |
| 703 | 712 |
| 704 void ClientSocketPoolBaseHelper::DecrementIdleCount() { | 713 void ClientSocketPoolBaseHelper::DecrementIdleCount() { |
| 705 if (--idle_socket_count_ == 0) | 714 if (--idle_socket_count_ == 0) |
| 706 timer_.Stop(); | 715 timer_.Stop(); |
| 707 } | 716 } |
| 708 | 717 |
| 718 void ClientSocketPoolBaseHelper::StartIdleSocketTimer() { |
| 719 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, |
| 720 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); |
| 721 } |
| 722 |
| 709 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, | 723 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, |
| 710 StreamSocket* socket, | 724 StreamSocket* socket, |
| 711 int id) { | 725 int id) { |
| 712 GroupMap::iterator i = group_map_.find(group_name); | 726 GroupMap::iterator i = group_map_.find(group_name); |
| 713 CHECK(i != group_map_.end()); | 727 CHECK(i != group_map_.end()); |
| 714 | 728 |
| 715 Group* group = i->second; | 729 Group* group = i->second; |
| 716 | 730 |
| 717 CHECK_GT(handed_out_socket_count_, 0); | 731 CHECK_GT(handed_out_socket_count_, 0); |
| 718 handed_out_socket_count_--; | 732 handed_out_socket_count_--; |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 889 const std::string& group_name, Group* group) { | 903 const std::string& group_name, Group* group) { |
| 890 int rv = RequestSocketInternal(group_name, | 904 int rv = RequestSocketInternal(group_name, |
| 891 *group->pending_requests().begin()); | 905 *group->pending_requests().begin()); |
| 892 if (rv != ERR_IO_PENDING) { | 906 if (rv != ERR_IO_PENDING) { |
| 893 scoped_ptr<const Request> request(RemoveRequestFromQueue( | 907 scoped_ptr<const Request> request(RemoveRequestFromQueue( |
| 894 group->mutable_pending_requests()->begin(), group)); | 908 group->mutable_pending_requests()->begin(), group)); |
| 895 if (group->IsEmpty()) | 909 if (group->IsEmpty()) |
| 896 RemoveGroup(group_name); | 910 RemoveGroup(group_name); |
| 897 | 911 |
| 898 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); | 912 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); |
| 899 InvokeUserCallbackLater( | 913 InvokeUserCallbackLater(request->handle(), request->callback(), rv); |
| 900 request->handle(), request->callback(), rv); | |
| 901 } | 914 } |
| 902 } | 915 } |
| 903 | 916 |
| 904 void ClientSocketPoolBaseHelper::HandOutSocket( | 917 void ClientSocketPoolBaseHelper::HandOutSocket( |
| 905 StreamSocket* socket, | 918 StreamSocket* socket, |
| 906 bool reused, | 919 bool reused, |
| 907 ClientSocketHandle* handle, | 920 ClientSocketHandle* handle, |
| 908 base::TimeDelta idle_time, | 921 base::TimeDelta idle_time, |
| 909 Group* group, | 922 Group* group, |
| 910 const BoundNetLog& net_log) { | 923 const BoundNetLog& net_log) { |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1123 // Delete active jobs. | 1136 // Delete active jobs. |
| 1124 STLDeleteElements(&jobs_); | 1137 STLDeleteElements(&jobs_); |
| 1125 | 1138 |
| 1126 // Cancel pending backup job. | 1139 // Cancel pending backup job. |
| 1127 method_factory_.RevokeAll(); | 1140 method_factory_.RevokeAll(); |
| 1128 } | 1141 } |
| 1129 | 1142 |
| 1130 } // namespace internal | 1143 } // namespace internal |
| 1131 | 1144 |
| 1132 } // namespace net | 1145 } // namespace net |
| OLD | NEW |