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

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

Issue 176021: Control the amount of time to leave an unused socket idle before closing it. (Closed)
Patch Set: Address wtc's comments. Created 11 years, 3 months 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/message_loop.h" 8 #include "base/message_loop.h"
9 #include "base/stl_util-inl.h" 9 #include "base/stl_util-inl.h"
10 #include "base/time.h" 10 #include "base/time.h"
11 #include "net/base/load_log.h" 11 #include "net/base/load_log.h"
12 #include "net/base/net_errors.h" 12 #include "net/base/net_errors.h"
13 #include "net/socket/client_socket_handle.h" 13 #include "net/socket/client_socket_handle.h"
14 14
15 using base::TimeDelta; 15 using base::TimeDelta;
16 16
17 namespace { 17 namespace {
18 18
19 // The timeout value, in seconds, used to clean up idle sockets that can't be 19 // The timeout value, in seconds, used to clean up idle sockets that can't be
20 // reused. 20 // reused.
21 // 21 //
22 // Note: It's important to close idle sockets that have received data as soon 22 // Note: It's important to close idle sockets that have received data as soon
23 // as possible because the received data may cause BSOD on Windows XP under 23 // as possible because the received data may cause BSOD on Windows XP under
24 // some conditions. See http://crbug.com/4606. 24 // some conditions. See http://crbug.com/4606.
25 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. 25 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT.
26 26
27 // The maximum duration, in seconds, to keep idle persistent sockets alive.
28 const int kIdleTimeout = 300; // 5 minutes.
29
30 } // namespace 27 } // namespace
31 28
32 namespace net { 29 namespace net {
33 30
34 ConnectJob::ConnectJob(const std::string& group_name, 31 ConnectJob::ConnectJob(const std::string& group_name,
35 const ClientSocketHandle* key_handle, 32 const ClientSocketHandle* key_handle,
36 base::TimeDelta timeout_duration, 33 base::TimeDelta timeout_duration,
37 Delegate* delegate, 34 Delegate* delegate,
38 LoadLog* load_log) 35 LoadLog* load_log)
39 : group_name_(group_name), 36 : group_name_(group_name),
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
92 NotifyDelegateOfCompletion(ERR_TIMED_OUT); 89 NotifyDelegateOfCompletion(ERR_TIMED_OUT);
93 } 90 }
94 91
95 namespace internal { 92 namespace internal {
96 93
97 bool ClientSocketPoolBaseHelper::g_late_binding = false; 94 bool ClientSocketPoolBaseHelper::g_late_binding = false;
98 95
99 ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper( 96 ClientSocketPoolBaseHelper::ClientSocketPoolBaseHelper(
100 int max_sockets, 97 int max_sockets,
101 int max_sockets_per_group, 98 int max_sockets_per_group,
99 base::TimeDelta unused_idle_socket_timeout,
100 base::TimeDelta used_idle_socket_timeout,
102 ConnectJobFactory* connect_job_factory) 101 ConnectJobFactory* connect_job_factory)
103 : idle_socket_count_(0), 102 : idle_socket_count_(0),
104 connecting_socket_count_(0), 103 connecting_socket_count_(0),
105 handed_out_socket_count_(0), 104 handed_out_socket_count_(0),
106 max_sockets_(max_sockets), 105 max_sockets_(max_sockets),
107 max_sockets_per_group_(max_sockets_per_group), 106 max_sockets_per_group_(max_sockets_per_group),
107 unused_idle_socket_timeout_(unused_idle_socket_timeout),
108 used_idle_socket_timeout_(used_idle_socket_timeout),
108 may_have_stalled_group_(false), 109 may_have_stalled_group_(false),
109 connect_job_factory_(connect_job_factory) { 110 connect_job_factory_(connect_job_factory) {
110 DCHECK_LE(0, max_sockets_per_group); 111 DCHECK_LE(0, max_sockets_per_group);
111 DCHECK_LE(max_sockets_per_group, max_sockets); 112 DCHECK_LE(max_sockets_per_group, max_sockets);
112 } 113 }
113 114
114 ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() { 115 ClientSocketPoolBaseHelper::~ClientSocketPoolBaseHelper() {
115 if (g_late_binding) 116 if (g_late_binding)
116 CancelAllConnectJobs(); 117 CancelAllConnectJobs();
117 // Clean up any idle sockets. Assert that we have no remaining active 118 // Clean up any idle sockets. Assert that we have no remaining active
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 return LOAD_STATE_IDLE; 334 return LOAD_STATE_IDLE;
334 } 335 }
335 } 336 }
336 } 337 }
337 338
338 NOTREACHED(); 339 NOTREACHED();
339 return LOAD_STATE_IDLE; 340 return LOAD_STATE_IDLE;
340 } 341 }
341 342
342 bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup( 343 bool ClientSocketPoolBaseHelper::IdleSocket::ShouldCleanup(
343 base::TimeTicks now) const { 344 base::TimeTicks now,
344 bool timed_out = (now - start_time) >= 345 base::TimeDelta timeout) const {
345 base::TimeDelta::FromSeconds(kIdleTimeout); 346 bool timed_out = (now - start_time) >= timeout;
346 return timed_out || 347 return timed_out ||
347 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected()); 348 !(used ? socket->IsConnectedAndIdle() : socket->IsConnected());
348 } 349 }
349 350
350 void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) { 351 void ClientSocketPoolBaseHelper::CleanupIdleSockets(bool force) {
351 if (idle_socket_count_ == 0) 352 if (idle_socket_count_ == 0)
352 return; 353 return;
353 354
354 // Current time value. Retrieving it once at the function start rather than 355 // Current time value. Retrieving it once at the function start rather than
355 // inside the inner loop, since it shouldn't change by any meaningful amount. 356 // inside the inner loop, since it shouldn't change by any meaningful amount.
356 base::TimeTicks now = base::TimeTicks::Now(); 357 base::TimeTicks now = base::TimeTicks::Now();
357 358
358 GroupMap::iterator i = group_map_.begin(); 359 GroupMap::iterator i = group_map_.begin();
359 while (i != group_map_.end()) { 360 while (i != group_map_.end()) {
360 Group& group = i->second; 361 Group& group = i->second;
361 362
362 std::deque<IdleSocket>::iterator j = group.idle_sockets.begin(); 363 std::deque<IdleSocket>::iterator j = group.idle_sockets.begin();
363 while (j != group.idle_sockets.end()) { 364 while (j != group.idle_sockets.end()) {
364 if (force || j->ShouldCleanup(now)) { 365 base::TimeDelta timeout =
366 j->used ? used_idle_socket_timeout_ : unused_idle_socket_timeout_;
367 if (force || j->ShouldCleanup(now, timeout)) {
365 delete j->socket; 368 delete j->socket;
366 j = group.idle_sockets.erase(j); 369 j = group.idle_sockets.erase(j);
367 DecrementIdleCount(); 370 DecrementIdleCount();
368 } else { 371 } else {
369 ++j; 372 ++j;
370 } 373 }
371 } 374 }
372 375
373 // Delete group if no longer needed. 376 // Delete group if no longer needed.
374 if (group.IsEmpty()) { 377 if (group.IsEmpty()) {
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
646 return total == max_sockets_; 649 return total == max_sockets_;
647 } 650 }
648 651
649 } // namespace internal 652 } // namespace internal
650 653
651 void EnableLateBindingOfSockets(bool enabled) { 654 void EnableLateBindingOfSockets(bool enabled) {
652 internal::ClientSocketPoolBaseHelper::EnableLateBindingOfSockets(enabled); 655 internal::ClientSocketPoolBaseHelper::EnableLateBindingOfSockets(enabled);
653 } 656 }
654 657
655 } // namespace net 658 } // 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