Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(791)

Side by Side Diff: net/socket/client_socket_pool_base.cc

Issue 8526006: Close idle sockets next time we are about to send data. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 : idle_socket_count_(0), 180 : idle_socket_count_(0),
181 connecting_socket_count_(0), 181 connecting_socket_count_(0),
182 handed_out_socket_count_(0), 182 handed_out_socket_count_(0),
183 max_sockets_(max_sockets), 183 max_sockets_(max_sockets),
184 max_sockets_per_group_(max_sockets_per_group), 184 max_sockets_per_group_(max_sockets_per_group),
185 use_cleanup_timer_(true),
185 unused_idle_socket_timeout_(unused_idle_socket_timeout), 186 unused_idle_socket_timeout_(unused_idle_socket_timeout),
186 used_idle_socket_timeout_(used_idle_socket_timeout), 187 used_idle_socket_timeout_(used_idle_socket_timeout),
187 connect_job_factory_(connect_job_factory), 188 connect_job_factory_(connect_job_factory),
188 connect_backup_jobs_enabled_(false), 189 connect_backup_jobs_enabled_(false),
189 pool_generation_number_(0), 190 pool_generation_number_(0),
190 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { 191 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
191 DCHECK_LE(0, max_sockets_per_group); 192 DCHECK_LE(0, max_sockets_per_group);
192 DCHECK_LE(max_sockets_per_group, max_sockets); 193 DCHECK_LE(max_sockets_per_group, max_sockets);
193 194
195 #if defined(OS_ANDROID)
196 use_cleanup_timer_ = false;
mmenke 2011/11/11 03:33:01 Just for consistency, and so it's only modified in
selim 2011/11/11 19:30:58 done
197 #endif
194 NetworkChangeNotifier::AddIPAddressObserver(this); 198 NetworkChangeNotifier::AddIPAddressObserver(this);
195 } 199 }
196 200
197 ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() { 201 ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
198 // Clean up any idle sockets and pending connect jobs. Assert that we have no 202 // Clean up any idle sockets and pending connect jobs. Assert that we have no
199 // remaining active sockets or pending requests. They should have all been 203 // remaining active sockets or pending requests. They should have all been
200 // cleaned up prior to |this| being destroyed. 204 // cleaned up prior to |this| being destroyed.
201 Flush(); 205 Flush();
202 DCHECK(group_map_.empty()); 206 DCHECK(group_map_.empty());
203 DCHECK(pending_callback_map_.empty()); 207 DCHECK(pending_callback_map_.empty());
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 Group* group = GetOrCreateGroup(group_name); 245 Group* group = GetOrCreateGroup(group_name);
242 246
243 int rv = RequestSocketInternal(group_name, request); 247 int rv = RequestSocketInternal(group_name, request);
244 if (rv != ERR_IO_PENDING) { 248 if (rv != ERR_IO_PENDING) {
245 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); 249 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
246 CHECK(!request->handle()->is_initialized()); 250 CHECK(!request->handle()->is_initialized());
247 delete request; 251 delete request;
248 } else { 252 } else {
249 InsertRequestIntoQueue(request, group->mutable_pending_requests()); 253 InsertRequestIntoQueue(request, group->mutable_pending_requests());
250 } 254 }
255 // Cleanup any timed-out idle sockets if no timer is used.
256 if (!use_cleanup_timer_) CleanupIdleSockets(false);
mmenke 2011/11/11 03:33:01 nit: Google style is to always split ifs onto two
selim 2011/11/11 19:30:58 done. Even though I find it less readable, code co
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
(...skipping 27 matching lines...) Expand all
288 // error. 294 // error.
289 NOTREACHED(); 295 NOTREACHED();
290 deleted_group = true; 296 deleted_group = true;
291 break; 297 break;
292 } 298 }
293 } 299 }
294 300
295 if (!deleted_group && group->IsEmpty()) 301 if (!deleted_group && group->IsEmpty())
296 RemoveGroup(group_name); 302 RemoveGroup(group_name);
297 303
304 // Cleanup any timed out idle sockets if no timer is used.
305 if (!use_cleanup_timer_) CleanupIdleSockets(false);
mmenke 2011/11/11 03:33:01 nit: Google style is to always split ifs onto two
selim 2011/11/11 19:30:58 done
306
298 if (rv == ERR_IO_PENDING) 307 if (rv == ERR_IO_PENDING)
299 rv = OK; 308 rv = OK;
300 request.net_log().EndEventWithNetErrorCode( 309 request.net_log().EndEventWithNetErrorCode(
301 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv); 310 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
302 } 311 }
303 312
304 int ClientSocketPoolBaseHelper::RequestSocketInternal( 313 int ClientSocketPoolBaseHelper::RequestSocketInternal(
305 const std::string& group_name, 314 const std::string& group_name,
306 const Request* request) { 315 const Request* request) {
307 DCHECK_GE(request->priority(), 0); 316 DCHECK_GE(request->priority(), 0);
(...skipping 381 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 bool old_value = g_connect_backup_jobs_enabled; 698 bool old_value = g_connect_backup_jobs_enabled;
690 g_connect_backup_jobs_enabled = enabled; 699 g_connect_backup_jobs_enabled = enabled;
691 return old_value; 700 return old_value;
692 } 701 }
693 702
694 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { 703 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
695 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; 704 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
696 } 705 }
697 706
698 void ClientSocketPoolBaseHelper::IncrementIdleCount() { 707 void ClientSocketPoolBaseHelper::IncrementIdleCount() {
699 if (++idle_socket_count_ == 1) 708 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
700 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, 709 startIdleSocketTimer();
701 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
702 } 710 }
703 711
704 void ClientSocketPoolBaseHelper::DecrementIdleCount() { 712 void ClientSocketPoolBaseHelper::DecrementIdleCount() {
705 if (--idle_socket_count_ == 0) 713 if (--idle_socket_count_ == 0)
706 timer_.Stop(); 714 timer_.Stop();
707 } 715 }
708 716
717 bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() {
718 return use_cleanup_timer_;
mmenke 2011/11/11 03:33:01 nit: non-virtual one-liners like this are general
selim 2011/11/11 19:30:58 done
719 }
720
721 void ClientSocketPoolBaseHelper::set_cleanup_timer_enabled( bool enabled ) {
mmenke 2011/11/11 03:33:01 nit: "(bool enabled)"
mmenke 2011/11/11 03:33:01 nit: Try to put these in the same order relative
selim 2011/11/11 19:30:58 done
722 use_cleanup_timer_ = enabled;
723 if (enabled) {
724 if (idle_socket_count_ > 0)
725 startIdleSocketTimer();
726 } else
727 timer_.Stop();
mmenke 2011/11/11 03:33:01 nit: If you use braces on the first block of a co
selim 2011/11/11 19:30:58 done
728 }
729
730 void ClientSocketPoolBaseHelper::startIdleSocketTimer() {
731 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
732 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
733 }
734
709 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, 735 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
710 StreamSocket* socket, 736 StreamSocket* socket,
711 int id) { 737 int id) {
712 GroupMap::iterator i = group_map_.find(group_name); 738 GroupMap::iterator i = group_map_.find(group_name);
713 CHECK(i != group_map_.end()); 739 CHECK(i != group_map_.end());
714 740
715 Group* group = i->second; 741 Group* group = i->second;
716 742
717 CHECK_GT(handed_out_socket_count_, 0); 743 CHECK_GT(handed_out_socket_count_, 0);
718 handed_out_socket_count_--; 744 handed_out_socket_count_--;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 const std::string& group_name, Group* group) { 915 const std::string& group_name, Group* group) {
890 int rv = RequestSocketInternal(group_name, 916 int rv = RequestSocketInternal(group_name,
891 *group->pending_requests().begin()); 917 *group->pending_requests().begin());
892 if (rv != ERR_IO_PENDING) { 918 if (rv != ERR_IO_PENDING) {
893 scoped_ptr<const Request> request(RemoveRequestFromQueue( 919 scoped_ptr<const Request> request(RemoveRequestFromQueue(
894 group->mutable_pending_requests()->begin(), group)); 920 group->mutable_pending_requests()->begin(), group));
895 if (group->IsEmpty()) 921 if (group->IsEmpty())
896 RemoveGroup(group_name); 922 RemoveGroup(group_name);
897 923
898 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); 924 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
899 InvokeUserCallbackLater( 925 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
900 request->handle(), request->callback(), rv);
901 } 926 }
902 } 927 }
903 928
904 void ClientSocketPoolBaseHelper::HandOutSocket( 929 void ClientSocketPoolBaseHelper::HandOutSocket(
905 StreamSocket* socket, 930 StreamSocket* socket,
906 bool reused, 931 bool reused,
907 ClientSocketHandle* handle, 932 ClientSocketHandle* handle,
908 base::TimeDelta idle_time, 933 base::TimeDelta idle_time,
909 Group* group, 934 Group* group,
910 const BoundNetLog& net_log) { 935 const BoundNetLog& net_log) {
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 // Delete active jobs. 1148 // Delete active jobs.
1124 STLDeleteElements(&jobs_); 1149 STLDeleteElements(&jobs_);
1125 1150
1126 // Cancel pending backup job. 1151 // Cancel pending backup job.
1127 method_factory_.RevokeAll(); 1152 method_factory_.RevokeAll();
1128 } 1153 }
1129 1154
1130 } // namespace internal 1155 } // namespace internal
1131 1156
1132 } // namespace net 1157 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698