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

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: addressed code review 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 SetCleanupTimerEnabled(false);
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_)
257 CleanupIdleSockets(false);
mmenke 2011/11/11 20:19:20 Wonder if potentially using a more stale than norm
selim 2011/11/11 21:33:06 I think, if we call cleanup idle sockets first, th
251 return rv; 258 return rv;
252 } 259 }
253 260
254 void ClientSocketPoolBaseHelper::RequestSockets( 261 void ClientSocketPoolBaseHelper::RequestSockets(
255 const std::string& group_name, 262 const std::string& group_name,
256 const Request& request, 263 const Request& request,
257 int num_sockets) { 264 int num_sockets) {
258 DCHECK(!request.callback()); 265 DCHECK(!request.callback());
259 DCHECK(!request.handle()); 266 DCHECK(!request.handle());
260 267
(...skipping 27 matching lines...) Expand all
288 // error. 295 // error.
289 NOTREACHED(); 296 NOTREACHED();
290 deleted_group = true; 297 deleted_group = true;
291 break; 298 break;
292 } 299 }
293 } 300 }
294 301
295 if (!deleted_group && group->IsEmpty()) 302 if (!deleted_group && group->IsEmpty())
296 RemoveGroup(group_name); 303 RemoveGroup(group_name);
297 304
305 // Cleanup any timed out idle sockets if no timer is used.
306 if (!use_cleanup_timer_)
307 CleanupIdleSockets(false);
308
298 if (rv == ERR_IO_PENDING) 309 if (rv == ERR_IO_PENDING)
299 rv = OK; 310 rv = OK;
300 request.net_log().EndEventWithNetErrorCode( 311 request.net_log().EndEventWithNetErrorCode(
301 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv); 312 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, rv);
302 } 313 }
303 314
304 int ClientSocketPoolBaseHelper::RequestSocketInternal( 315 int ClientSocketPoolBaseHelper::RequestSocketInternal(
305 const std::string& group_name, 316 const std::string& group_name,
306 const Request* request) { 317 const Request* request) {
307 DCHECK_GE(request->priority(), 0); 318 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; 700 bool old_value = g_connect_backup_jobs_enabled;
690 g_connect_backup_jobs_enabled = enabled; 701 g_connect_backup_jobs_enabled = enabled;
691 return old_value; 702 return old_value;
692 } 703 }
693 704
694 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { 705 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
695 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; 706 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
696 } 707 }
697 708
698 void ClientSocketPoolBaseHelper::IncrementIdleCount() { 709 void ClientSocketPoolBaseHelper::IncrementIdleCount() {
699 if (++idle_socket_count_ == 1) 710 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
700 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, 711 StartIdleSocketTimer();
701 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
702 } 712 }
703 713
704 void ClientSocketPoolBaseHelper::DecrementIdleCount() { 714 void ClientSocketPoolBaseHelper::DecrementIdleCount() {
705 if (--idle_socket_count_ == 0) 715 if (--idle_socket_count_ == 0)
706 timer_.Stop(); 716 timer_.Stop();
707 } 717 }
708 718
719 void ClientSocketPoolBaseHelper::SetCleanupTimerEnabled(bool enabled) {
720 use_cleanup_timer_ = enabled;
721 if (enabled) {
722 if (idle_socket_count_ > 0)
723 StartIdleSocketTimer();
724 } else {
725 timer_.Stop();
726 }
727 }
728
729 void ClientSocketPoolBaseHelper::StartIdleSocketTimer() {
730 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this,
731 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
732 }
733
709 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name, 734 void ClientSocketPoolBaseHelper::ReleaseSocket(const std::string& group_name,
710 StreamSocket* socket, 735 StreamSocket* socket,
711 int id) { 736 int id) {
712 GroupMap::iterator i = group_map_.find(group_name); 737 GroupMap::iterator i = group_map_.find(group_name);
713 CHECK(i != group_map_.end()); 738 CHECK(i != group_map_.end());
714 739
715 Group* group = i->second; 740 Group* group = i->second;
716 741
717 CHECK_GT(handed_out_socket_count_, 0); 742 CHECK_GT(handed_out_socket_count_, 0);
718 handed_out_socket_count_--; 743 handed_out_socket_count_--;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
889 const std::string& group_name, Group* group) { 914 const std::string& group_name, Group* group) {
890 int rv = RequestSocketInternal(group_name, 915 int rv = RequestSocketInternal(group_name,
891 *group->pending_requests().begin()); 916 *group->pending_requests().begin());
892 if (rv != ERR_IO_PENDING) { 917 if (rv != ERR_IO_PENDING) {
893 scoped_ptr<const Request> request(RemoveRequestFromQueue( 918 scoped_ptr<const Request> request(RemoveRequestFromQueue(
894 group->mutable_pending_requests()->begin(), group)); 919 group->mutable_pending_requests()->begin(), group));
895 if (group->IsEmpty()) 920 if (group->IsEmpty())
896 RemoveGroup(group_name); 921 RemoveGroup(group_name);
897 922
898 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); 923 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
899 InvokeUserCallbackLater( 924 InvokeUserCallbackLater(request->handle(), request->callback(), rv);
900 request->handle(), request->callback(), rv);
901 } 925 }
902 } 926 }
903 927
904 void ClientSocketPoolBaseHelper::HandOutSocket( 928 void ClientSocketPoolBaseHelper::HandOutSocket(
905 StreamSocket* socket, 929 StreamSocket* socket,
906 bool reused, 930 bool reused,
907 ClientSocketHandle* handle, 931 ClientSocketHandle* handle,
908 base::TimeDelta idle_time, 932 base::TimeDelta idle_time,
909 Group* group, 933 Group* group,
910 const BoundNetLog& net_log) { 934 const BoundNetLog& net_log) {
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
1123 // Delete active jobs. 1147 // Delete active jobs.
1124 STLDeleteElements(&jobs_); 1148 STLDeleteElements(&jobs_);
1125 1149
1126 // Cancel pending backup job. 1150 // Cancel pending backup job.
1127 method_factory_.RevokeAll(); 1151 method_factory_.RevokeAll();
1128 } 1152 }
1129 1153
1130 } // namespace internal 1154 } // namespace internal
1131 1155
1132 } // namespace net 1156 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698