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

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: address code review about stale idle sockets 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
« no previous file with comments | « net/socket/client_socket_pool_base.h ('k') | net/socket/client_socket_pool_base_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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);
willchan no longer on Chromium 2011/11/11 23:47:50 I think you should do this differently. The proble
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 26 matching lines...) Expand all
230 group->CleanupBackupJob(); 234 group->CleanupBackupJob();
231 return req; 235 return req;
232 } 236 }
233 237
234 int ClientSocketPoolBaseHelper::RequestSocket( 238 int ClientSocketPoolBaseHelper::RequestSocket(
235 const std::string& group_name, 239 const std::string& group_name,
236 const Request* request) { 240 const Request* request) {
237 CHECK(request->callback()); 241 CHECK(request->callback());
238 CHECK(request->handle()); 242 CHECK(request->handle());
239 243
244 // Cleanup any timed-out idle sockets if no timer is used.
245 if (!use_cleanup_timer_)
246 CleanupIdleSockets(false);
willchan no longer on Chromium 2011/11/11 23:47:50 Move this after the BeginEvent().
247
240 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL); 248 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL, NULL);
241 Group* group = GetOrCreateGroup(group_name); 249 Group* group = GetOrCreateGroup(group_name);
242 250
243 int rv = RequestSocketInternal(group_name, request); 251 int rv = RequestSocketInternal(group_name, request);
244 if (rv != ERR_IO_PENDING) { 252 if (rv != ERR_IO_PENDING) {
245 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); 253 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv);
246 CHECK(!request->handle()->is_initialized()); 254 CHECK(!request->handle()->is_initialized());
247 delete request; 255 delete request;
248 } else { 256 } else {
249 InsertRequestIntoQueue(request, group->mutable_pending_requests()); 257 InsertRequestIntoQueue(request, group->mutable_pending_requests());
250 } 258 }
251 return rv; 259 return rv;
252 } 260 }
253 261
254 void ClientSocketPoolBaseHelper::RequestSockets( 262 void ClientSocketPoolBaseHelper::RequestSockets(
255 const std::string& group_name, 263 const std::string& group_name,
256 const Request& request, 264 const Request& request,
257 int num_sockets) { 265 int num_sockets) {
258 DCHECK(!request.callback()); 266 DCHECK(!request.callback());
259 DCHECK(!request.handle()); 267 DCHECK(!request.handle());
260 268
269 // Cleanup any timed out idle sockets if no timer is used.
270 if (!use_cleanup_timer_)
271 CleanupIdleSockets(false);
272
261 if (num_sockets > max_sockets_per_group_) { 273 if (num_sockets > max_sockets_per_group_) {
262 num_sockets = max_sockets_per_group_; 274 num_sockets = max_sockets_per_group_;
263 } 275 }
264 276
265 request.net_log().BeginEvent( 277 request.net_log().BeginEvent(
266 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, 278 NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS,
267 make_scoped_refptr(new NetLogIntegerParameter( 279 make_scoped_refptr(new NetLogIntegerParameter(
268 "num_sockets", num_sockets))); 280 "num_sockets", num_sockets)));
269 281
270 Group* group = GetOrCreateGroup(group_name); 282 Group* group = GetOrCreateGroup(group_name);
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
689 bool old_value = g_connect_backup_jobs_enabled; 701 bool old_value = g_connect_backup_jobs_enabled;
690 g_connect_backup_jobs_enabled = enabled; 702 g_connect_backup_jobs_enabled = enabled;
691 return old_value; 703 return old_value;
692 } 704 }
693 705
694 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { 706 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() {
695 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; 707 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled;
696 } 708 }
697 709
698 void ClientSocketPoolBaseHelper::IncrementIdleCount() { 710 void ClientSocketPoolBaseHelper::IncrementIdleCount() {
699 if (++idle_socket_count_ == 1) 711 if (++idle_socket_count_ == 1 && use_cleanup_timer_)
700 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, 712 StartIdleSocketTimer();
701 &ClientSocketPoolBaseHelper::OnCleanupTimerFired);
702 } 713 }
703 714
704 void ClientSocketPoolBaseHelper::DecrementIdleCount() { 715 void ClientSocketPoolBaseHelper::DecrementIdleCount() {
705 if (--idle_socket_count_ == 0) 716 if (--idle_socket_count_ == 0)
706 timer_.Stop(); 717 timer_.Stop();
707 } 718 }
708 719
720 void ClientSocketPoolBaseHelper::SetCleanupTimerEnabled(bool enabled) {
willchan no longer on Chromium 2011/11/11 23:47:50 I think it's not necessary to have this code where
721 use_cleanup_timer_ = enabled;
722 if (enabled) {
723 if (idle_socket_count_ > 0)
724 StartIdleSocketTimer();
725 } else {
726 timer_.Stop();
727 }
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
« no previous file with comments | « net/socket/client_socket_pool_base.h ('k') | net/socket/client_socket_pool_base_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698