OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/stl_util.h" | 15 #include "base/stl_util.h" |
16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "base/time/time.h" | 18 #include "base/time/time.h" |
19 #include "base/trace_event/trace_event.h" | 19 #include "base/trace_event/trace_event.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
22 #include "net/log/net_log.h" | 22 #include "net/log/net_log.h" |
23 | 23 |
24 using base::TimeDelta; | 24 using base::TimeDelta; |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 namespace { | 28 namespace { |
29 | 29 |
30 // Indicate whether we should enable idle socket cleanup timer. When timer is | |
31 // disabled, sockets are closed next time a socket request is made. | |
32 // Keep this enabled for windows as long as we support Windows XP, see the note | |
33 // in kCleanupInterval below. | |
34 #if defined(OS_WIN) | |
35 bool g_cleanup_timer_enabled = true; | |
36 #else | |
37 bool g_cleanup_timer_enabled = false; | |
38 #endif | |
39 | |
40 // The timeout value, in seconds, used to clean up idle sockets that can't be | |
41 // reused. | |
42 // | |
43 // Note: It's important to close idle sockets that have received data as soon | |
44 // as possible because the received data may cause BSOD on Windows XP under | |
45 // some conditions. See http://crbug.com/4606. | |
46 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. | |
47 | |
48 // Indicate whether or not we should establish a new transport layer connection | 30 // Indicate whether or not we should establish a new transport layer connection |
49 // after a certain timeout has passed without receiving an ACK. | 31 // after a certain timeout has passed without receiving an ACK. |
50 bool g_connect_backup_jobs_enabled = true; | 32 bool g_connect_backup_jobs_enabled = true; |
51 | 33 |
52 } // namespace | 34 } // namespace |
53 | 35 |
54 ConnectJob::ConnectJob(const std::string& group_name, | 36 ConnectJob::ConnectJob(const std::string& group_name, |
55 base::TimeDelta timeout_duration, | 37 base::TimeDelta timeout_duration, |
56 RequestPriority priority, | 38 RequestPriority priority, |
57 ClientSocketPool::RespectLimits respect_limits, | 39 ClientSocketPool::RespectLimits respect_limits, |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 int max_sockets, | 153 int max_sockets, |
172 int max_sockets_per_group, | 154 int max_sockets_per_group, |
173 base::TimeDelta unused_idle_socket_timeout, | 155 base::TimeDelta unused_idle_socket_timeout, |
174 base::TimeDelta used_idle_socket_timeout, | 156 base::TimeDelta used_idle_socket_timeout, |
175 ConnectJobFactory* connect_job_factory) | 157 ConnectJobFactory* connect_job_factory) |
176 : idle_socket_count_(0), | 158 : idle_socket_count_(0), |
177 connecting_socket_count_(0), | 159 connecting_socket_count_(0), |
178 handed_out_socket_count_(0), | 160 handed_out_socket_count_(0), |
179 max_sockets_(max_sockets), | 161 max_sockets_(max_sockets), |
180 max_sockets_per_group_(max_sockets_per_group), | 162 max_sockets_per_group_(max_sockets_per_group), |
181 use_cleanup_timer_(g_cleanup_timer_enabled), | |
182 unused_idle_socket_timeout_(unused_idle_socket_timeout), | 163 unused_idle_socket_timeout_(unused_idle_socket_timeout), |
183 used_idle_socket_timeout_(used_idle_socket_timeout), | 164 used_idle_socket_timeout_(used_idle_socket_timeout), |
184 connect_job_factory_(connect_job_factory), | 165 connect_job_factory_(connect_job_factory), |
185 connect_backup_jobs_enabled_(false), | 166 connect_backup_jobs_enabled_(false), |
186 pool_generation_number_(0), | 167 pool_generation_number_(0), |
187 pool_(pool), | 168 pool_(pool), |
188 weak_factory_(this) { | 169 weak_factory_(this) { |
189 DCHECK_LE(0, max_sockets_per_group); | 170 DCHECK_LE(0, max_sockets_per_group); |
190 DCHECK_LE(max_sockets_per_group, max_sockets); | 171 DCHECK_LE(max_sockets_per_group, max_sockets); |
191 | 172 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 CHECK(ContainsKey(higher_pools_, higher_pool)); | 257 CHECK(ContainsKey(higher_pools_, higher_pool)); |
277 higher_pools_.erase(higher_pool); | 258 higher_pools_.erase(higher_pool); |
278 } | 259 } |
279 | 260 |
280 int ClientSocketPoolBaseHelper::RequestSocket( | 261 int ClientSocketPoolBaseHelper::RequestSocket( |
281 const std::string& group_name, | 262 const std::string& group_name, |
282 std::unique_ptr<const Request> request) { | 263 std::unique_ptr<const Request> request) { |
283 CHECK(!request->callback().is_null()); | 264 CHECK(!request->callback().is_null()); |
284 CHECK(request->handle()); | 265 CHECK(request->handle()); |
285 | 266 |
286 // Cleanup any timed-out idle sockets if no timer is used. | 267 // Cleanup any timed-out idle sockets. |
287 if (!use_cleanup_timer_) | 268 CleanupIdleSockets(false); |
288 CleanupIdleSockets(false); | |
289 | 269 |
290 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL); | 270 request->net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL); |
291 Group* group = GetOrCreateGroup(group_name); | 271 Group* group = GetOrCreateGroup(group_name); |
292 | 272 |
293 int rv = RequestSocketInternal(group_name, *request); | 273 int rv = RequestSocketInternal(group_name, *request); |
294 if (rv != ERR_IO_PENDING) { | 274 if (rv != ERR_IO_PENDING) { |
295 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); | 275 request->net_log().EndEventWithNetErrorCode(NetLog::TYPE_SOCKET_POOL, rv); |
296 CHECK(!request->handle()->is_initialized()); | 276 CHECK(!request->handle()->is_initialized()); |
297 request.reset(); | 277 request.reset(); |
298 } else { | 278 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
312 return rv; | 292 return rv; |
313 } | 293 } |
314 | 294 |
315 void ClientSocketPoolBaseHelper::RequestSockets( | 295 void ClientSocketPoolBaseHelper::RequestSockets( |
316 const std::string& group_name, | 296 const std::string& group_name, |
317 const Request& request, | 297 const Request& request, |
318 int num_sockets) { | 298 int num_sockets) { |
319 DCHECK(request.callback().is_null()); | 299 DCHECK(request.callback().is_null()); |
320 DCHECK(!request.handle()); | 300 DCHECK(!request.handle()); |
321 | 301 |
322 // Cleanup any timed out idle sockets if no timer is used. | 302 // Cleanup any timed-out idle sockets. |
323 if (!use_cleanup_timer_) | 303 CleanupIdleSockets(false); |
324 CleanupIdleSockets(false); | |
325 | 304 |
326 if (num_sockets > max_sockets_per_group_) { | 305 if (num_sockets > max_sockets_per_group_) { |
327 num_sockets = max_sockets_per_group_; | 306 num_sockets = max_sockets_per_group_; |
328 } | 307 } |
329 | 308 |
330 request.net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, | 309 request.net_log().BeginEvent(NetLog::TYPE_SOCKET_POOL_CONNECTING_N_SOCKETS, |
331 NetLog::IntCallback("num_sockets", num_sockets)); | 310 NetLog::IntCallback("num_sockets", num_sockets)); |
332 | 311 |
333 Group* group = GetOrCreateGroup(group_name); | 312 Group* group = GetOrCreateGroup(group_name); |
334 | 313 |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
749 bool old_value = g_connect_backup_jobs_enabled; | 728 bool old_value = g_connect_backup_jobs_enabled; |
750 g_connect_backup_jobs_enabled = enabled; | 729 g_connect_backup_jobs_enabled = enabled; |
751 return old_value; | 730 return old_value; |
752 } | 731 } |
753 | 732 |
754 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { | 733 void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
755 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; | 734 connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
756 } | 735 } |
757 | 736 |
758 void ClientSocketPoolBaseHelper::IncrementIdleCount() { | 737 void ClientSocketPoolBaseHelper::IncrementIdleCount() { |
759 if (++idle_socket_count_ == 1 && use_cleanup_timer_) | 738 ++idle_socket_count_; |
760 StartIdleSocketTimer(); | |
761 } | 739 } |
762 | 740 |
763 void ClientSocketPoolBaseHelper::DecrementIdleCount() { | 741 void ClientSocketPoolBaseHelper::DecrementIdleCount() { |
764 if (--idle_socket_count_ == 0) | 742 --idle_socket_count_; |
765 timer_.Stop(); | |
766 } | |
767 | |
768 // static | |
769 bool ClientSocketPoolBaseHelper::cleanup_timer_enabled() { | |
770 return g_cleanup_timer_enabled; | |
771 } | |
772 | |
773 // static | |
774 bool ClientSocketPoolBaseHelper::set_cleanup_timer_enabled(bool enabled) { | |
775 bool old_value = g_cleanup_timer_enabled; | |
776 g_cleanup_timer_enabled = enabled; | |
777 return old_value; | |
778 } | |
779 | |
780 void ClientSocketPoolBaseHelper::StartIdleSocketTimer() { | |
781 timer_.Start(FROM_HERE, TimeDelta::FromSeconds(kCleanupInterval), this, | |
782 &ClientSocketPoolBaseHelper::OnCleanupTimerFired); | |
783 } | 743 } |
784 | 744 |
785 void ClientSocketPoolBaseHelper::ReleaseSocket( | 745 void ClientSocketPoolBaseHelper::ReleaseSocket( |
786 const std::string& group_name, | 746 const std::string& group_name, |
787 std::unique_ptr<StreamSocket> socket, | 747 std::unique_ptr<StreamSocket> socket, |
788 int id) { | 748 int id) { |
789 GroupMap::iterator i = group_map_.find(group_name); | 749 GroupMap::iterator i = group_map_.find(group_name); |
790 CHECK(i != group_map_.end()); | 750 CHECK(i != group_map_.end()); |
791 | 751 |
792 Group* group = i->second; | 752 Group* group = i->second; |
(...skipping 556 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1349 // If there are no more requests, kill the backup timer. | 1309 // If there are no more requests, kill the backup timer. |
1350 if (pending_requests_.empty()) | 1310 if (pending_requests_.empty()) |
1351 backup_job_timer_.Stop(); | 1311 backup_job_timer_.Stop(); |
1352 request->CrashIfInvalid(); | 1312 request->CrashIfInvalid(); |
1353 return request; | 1313 return request; |
1354 } | 1314 } |
1355 | 1315 |
1356 } // namespace internal | 1316 } // namespace internal |
1357 | 1317 |
1358 } // namespace net | 1318 } // namespace net |
OLD | NEW |