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