OLD | NEW |
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> | |
8 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
9 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
10 #include "base/logging.h" | |
11 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
12 #include "base/metrics/stats_counters.h" | 10 #include "base/metrics/stats_counters.h" |
13 #include "base/stl_util-inl.h" | 11 #include "base/stl_util-inl.h" |
14 #include "base/string_number_conversions.h" | |
15 #include "base/string_util.h" | 12 #include "base/string_util.h" |
16 #include "base/time.h" | 13 #include "base/time.h" |
17 #include "base/values.h" | 14 #include "base/values.h" |
18 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
19 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
20 #include "net/socket/client_socket_handle.h" | 17 #include "net/socket/client_socket_handle.h" |
21 | 18 |
22 using base::TimeDelta; | 19 using base::TimeDelta; |
23 | 20 |
24 namespace { | 21 namespace { |
25 | 22 |
26 // The timeout value, in seconds, used to clean up idle sockets that can't be | 23 // The timeout value, in seconds, used to clean up idle sockets that can't be |
27 // reused. | 24 // reused. |
28 // | 25 // |
29 // Note: It's important to close idle sockets that have received data as soon | 26 // Note: It's important to close idle sockets that have received data as soon |
30 // as possible because the received data may cause BSOD on Windows XP under | 27 // as possible because the received data may cause BSOD on Windows XP under |
31 // some conditions. See http://crbug.com/4606. | 28 // some conditions. See http://crbug.com/4606. |
32 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. | 29 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. |
33 | 30 |
34 // Indicate whether or not we should establish a new transport layer connection | 31 // Indicate whether or not we should establish a new transport layer connection |
35 // after a certain timeout has passed without receiving an ACK. | 32 // after a certain timeout has passed without receiving an ACK. |
36 bool g_connect_backup_jobs_enabled = true; | 33 bool g_connect_backup_jobs_enabled = true; |
37 | 34 |
38 double g_socket_reuse_policy_penalty_exponent = -1; | |
39 int g_socket_reuse_policy = -1; | |
40 | |
41 } // namespace | 35 } // namespace |
42 | 36 |
43 namespace net { | 37 namespace net { |
44 | 38 |
45 int GetSocketReusePolicy() { | |
46 return g_socket_reuse_policy; | |
47 } | |
48 | |
49 void SetSocketReusePolicy(int policy) { | |
50 DCHECK_GE(policy, 0); | |
51 DCHECK_LE(policy, 2); | |
52 if (policy > 2 || policy < 0) { | |
53 LOG(ERROR) << "Invalid socket reuse policy"; | |
54 return; | |
55 } | |
56 | |
57 double exponents[] = { 0, 0.25, -1 }; | |
58 g_socket_reuse_policy_penalty_exponent = exponents[policy]; | |
59 g_socket_reuse_policy = policy; | |
60 | |
61 VLOG(1) << "Setting g_socket_reuse_policy_penalty_exponent = " | |
62 << g_socket_reuse_policy_penalty_exponent; | |
63 } | |
64 | |
65 ConnectJob::ConnectJob(const std::string& group_name, | 39 ConnectJob::ConnectJob(const std::string& group_name, |
66 base::TimeDelta timeout_duration, | 40 base::TimeDelta timeout_duration, |
67 Delegate* delegate, | 41 Delegate* delegate, |
68 const BoundNetLog& net_log) | 42 const BoundNetLog& net_log) |
69 : group_name_(group_name), | 43 : group_name_(group_name), |
70 timeout_duration_(timeout_duration), | 44 timeout_duration_(timeout_duration), |
71 delegate_(delegate), | 45 delegate_(delegate), |
72 net_log_(net_log), | 46 net_log_(net_log), |
73 idle_(true), | 47 idle_(true), |
74 preconnect_state_(NOT_PRECONNECT) { | 48 preconnect_state_(NOT_PRECONNECT) { |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 } | 356 } |
383 } | 357 } |
384 | 358 |
385 return rv; | 359 return rv; |
386 } | 360 } |
387 | 361 |
388 bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( | 362 bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( |
389 const Request* request, Group* group) { | 363 const Request* request, Group* group) { |
390 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); | 364 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
391 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); | 365 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
392 double max_score = -1; | |
393 | 366 |
394 // Iterate through the idle sockets forwards (oldest to newest) | 367 // Iterate through the idle sockets forwards (oldest to newest) |
395 // * Delete any disconnected ones. | 368 // * Delete any disconnected ones. |
396 // * If we find a used idle socket, assign to |idle_socket|. At the end, | 369 // * If we find a used idle socket, assign to |idle_socket|. At the end, |
397 // the |idle_socket_it| will be set to the newest used idle socket. | 370 // the |idle_socket_it| will be set to the newest used idle socket. |
398 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); | 371 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); |
399 it != idle_sockets->end();) { | 372 it != idle_sockets->end();) { |
400 if (!it->socket->IsConnectedAndIdle()) { | 373 if (!it->socket->IsConnectedAndIdle()) { |
401 DecrementIdleCount(); | 374 DecrementIdleCount(); |
402 delete it->socket; | 375 delete it->socket; |
403 it = idle_sockets->erase(it); | 376 it = idle_sockets->erase(it); |
404 continue; | 377 continue; |
405 } | 378 } |
406 | 379 |
407 if (it->socket->WasEverUsed()) { | 380 if (it->socket->WasEverUsed()) { |
408 // We found one we can reuse! | 381 // We found one we can reuse! |
409 double score = 0; | 382 idle_socket_it = it; |
410 int64 bytes_read = it->socket->NumBytesRead(); | |
411 double num_kb = static_cast<double>(bytes_read) / 1024.0; | |
412 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds(); | |
413 idle_time_sec = std::max(1, idle_time_sec); | |
414 | |
415 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) { | |
416 score = num_kb / pow(idle_time_sec, | |
417 g_socket_reuse_policy_penalty_exponent); | |
418 } | |
419 | |
420 // Equality to prefer recently used connection. | |
421 if (score >= max_score) { | |
422 idle_socket_it = it; | |
423 max_score = score; | |
424 } | |
425 } | 383 } |
426 | 384 |
427 ++it; | 385 ++it; |
428 } | 386 } |
429 | 387 |
430 // If we haven't found an idle socket, that means there are no used idle | 388 // If we haven't found an idle socket, that means there are no used idle |
431 // sockets. Pick the oldest (first) idle socket (FIFO). | 389 // sockets. Pick the oldest (first) idle socket (FIFO). |
432 | 390 |
433 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) | 391 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) |
434 idle_socket_it = idle_sockets->begin(); | 392 idle_socket_it = idle_sockets->begin(); |
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 // Delete active jobs. | 1081 // Delete active jobs. |
1124 STLDeleteElements(&jobs_); | 1082 STLDeleteElements(&jobs_); |
1125 | 1083 |
1126 // Cancel pending backup job. | 1084 // Cancel pending backup job. |
1127 method_factory_.RevokeAll(); | 1085 method_factory_.RevokeAll(); |
1128 } | 1086 } |
1129 | 1087 |
1130 } // namespace internal | 1088 } // namespace internal |
1131 | 1089 |
1132 } // namespace net | 1090 } // namespace net |
OLD | NEW |