| 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 <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" | 9 #include "base/logging.h" |
| 11 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 12 #include "base/metrics/stats_counters.h" | 11 #include "base/metrics/stats_counters.h" |
| 13 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
| 14 #include "base/string_number_conversions.h" | |
| 15 #include "base/string_util.h" | 13 #include "base/string_util.h" |
| 16 #include "base/time.h" | 14 #include "base/time.h" |
| 17 #include "base/values.h" | 15 #include "base/values.h" |
| 18 #include "net/base/net_log.h" | 16 #include "net/base/net_log.h" |
| 19 #include "net/base/net_errors.h" | 17 #include "net/base/net_errors.h" |
| 20 #include "net/socket/client_socket_handle.h" | 18 #include "net/socket/client_socket_handle.h" |
| 21 | 19 |
| 22 using base::TimeDelta; | 20 using base::TimeDelta; |
| 23 | 21 |
| 24 namespace { | 22 namespace { |
| 25 | 23 |
| 26 // Indicate whether we should enable idle socket cleanup timer. When timer is | 24 // Indicate whether we should enable idle socket cleanup timer. When timer is |
| 27 // disabled, sockets are closed next time a socket request is made. | 25 // disabled, sockets are closed next time a socket request is made. |
| 28 bool g_cleanup_timer_enabled = true; | 26 bool g_cleanup_timer_enabled = true; |
| 29 | 27 |
| 30 // The timeout value, in seconds, used to clean up idle sockets that can't be | 28 // The timeout value, in seconds, used to clean up idle sockets that can't be |
| 31 // reused. | 29 // reused. |
| 32 // | 30 // |
| 33 // Note: It's important to close idle sockets that have received data as soon | 31 // Note: It's important to close idle sockets that have received data as soon |
| 34 // as possible because the received data may cause BSOD on Windows XP under | 32 // as possible because the received data may cause BSOD on Windows XP under |
| 35 // some conditions. See http://crbug.com/4606. | 33 // some conditions. See http://crbug.com/4606. |
| 36 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. | 34 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. |
| 37 | 35 |
| 38 // Indicate whether or not we should establish a new transport layer connection | 36 // Indicate whether or not we should establish a new transport layer connection |
| 39 // after a certain timeout has passed without receiving an ACK. | 37 // after a certain timeout has passed without receiving an ACK. |
| 40 bool g_connect_backup_jobs_enabled = true; | 38 bool g_connect_backup_jobs_enabled = true; |
| 41 | 39 |
| 42 double g_socket_reuse_policy_penalty_exponent = -1; | |
| 43 int g_socket_reuse_policy = -1; | |
| 44 | |
| 45 } // namespace | 40 } // namespace |
| 46 | 41 |
| 47 namespace net { | 42 namespace net { |
| 48 | 43 |
| 49 int GetSocketReusePolicy() { | |
| 50 return g_socket_reuse_policy; | |
| 51 } | |
| 52 | |
| 53 void SetSocketReusePolicy(int policy) { | |
| 54 DCHECK_GE(policy, 0); | |
| 55 DCHECK_LE(policy, 2); | |
| 56 if (policy > 2 || policy < 0) { | |
| 57 LOG(ERROR) << "Invalid socket reuse policy"; | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 double exponents[] = { 0, 0.25, -1 }; | |
| 62 g_socket_reuse_policy_penalty_exponent = exponents[policy]; | |
| 63 g_socket_reuse_policy = policy; | |
| 64 | |
| 65 VLOG(1) << "Setting g_socket_reuse_policy_penalty_exponent = " | |
| 66 << g_socket_reuse_policy_penalty_exponent; | |
| 67 } | |
| 68 | |
| 69 ConnectJob::ConnectJob(const std::string& group_name, | 44 ConnectJob::ConnectJob(const std::string& group_name, |
| 70 base::TimeDelta timeout_duration, | 45 base::TimeDelta timeout_duration, |
| 71 Delegate* delegate, | 46 Delegate* delegate, |
| 72 const BoundNetLog& net_log) | 47 const BoundNetLog& net_log) |
| 73 : group_name_(group_name), | 48 : group_name_(group_name), |
| 74 timeout_duration_(timeout_duration), | 49 timeout_duration_(timeout_duration), |
| 75 delegate_(delegate), | 50 delegate_(delegate), |
| 76 net_log_(net_log), | 51 net_log_(net_log), |
| 77 idle_(true) { | 52 idle_(true) { |
| 78 DCHECK(!group_name.empty()); | 53 DCHECK(!group_name.empty()); |
| (...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 } | 394 } |
| 420 } | 395 } |
| 421 | 396 |
| 422 return rv; | 397 return rv; |
| 423 } | 398 } |
| 424 | 399 |
| 425 bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest( | 400 bool ClientSocketPoolBaseHelper::AssignIdleSocketToRequest( |
| 426 const Request* request, Group* group) { | 401 const Request* request, Group* group) { |
| 427 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); | 402 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
| 428 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); | 403 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
| 429 double max_score = -1; | |
| 430 | 404 |
| 431 // Iterate through the idle sockets forwards (oldest to newest) | 405 // Iterate through the idle sockets forwards (oldest to newest) |
| 432 // * Delete any disconnected ones. | 406 // * Delete any disconnected ones. |
| 433 // * If we find a used idle socket, assign to |idle_socket|. At the end, | 407 // * If we find a used idle socket, assign to |idle_socket|. At the end, |
| 434 // the |idle_socket_it| will be set to the newest used idle socket. | 408 // the |idle_socket_it| will be set to the newest used idle socket. |
| 435 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); | 409 for (std::list<IdleSocket>::iterator it = idle_sockets->begin(); |
| 436 it != idle_sockets->end();) { | 410 it != idle_sockets->end();) { |
| 437 if (!it->socket->IsConnectedAndIdle()) { | 411 if (!it->socket->IsConnectedAndIdle()) { |
| 438 DecrementIdleCount(); | 412 DecrementIdleCount(); |
| 439 delete it->socket; | 413 delete it->socket; |
| 440 it = idle_sockets->erase(it); | 414 it = idle_sockets->erase(it); |
| 441 continue; | 415 continue; |
| 442 } | 416 } |
| 443 | 417 |
| 444 if (it->socket->WasEverUsed()) { | 418 if (it->socket->WasEverUsed()) { |
| 445 // We found one we can reuse! | 419 // We found one we can reuse! |
| 446 double score = 0; | 420 idle_socket_it = it; |
| 447 int64 bytes_read = it->socket->NumBytesRead(); | |
| 448 double num_kb = static_cast<double>(bytes_read) / 1024.0; | |
| 449 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds(); | |
| 450 idle_time_sec = std::max(1, idle_time_sec); | |
| 451 | |
| 452 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) { | |
| 453 score = num_kb / pow(idle_time_sec, | |
| 454 g_socket_reuse_policy_penalty_exponent); | |
| 455 } | |
| 456 | |
| 457 // Equality to prefer recently used connection. | |
| 458 if (score >= max_score) { | |
| 459 idle_socket_it = it; | |
| 460 max_score = score; | |
| 461 } | |
| 462 } | 421 } |
| 463 | 422 |
| 464 ++it; | 423 ++it; |
| 465 } | 424 } |
| 466 | 425 |
| 467 // If we haven't found an idle socket, that means there are no used idle | 426 // If we haven't found an idle socket, that means there are no used idle |
| 468 // sockets. Pick the oldest (first) idle socket (FIFO). | 427 // sockets. Pick the oldest (first) idle socket (FIFO). |
| 469 | 428 |
| 470 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) | 429 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) |
| 471 idle_socket_it = idle_sockets->begin(); | 430 idle_socket_it = idle_sockets->begin(); |
| (...skipping 767 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1239 STLDeleteElements(&jobs_); | 1198 STLDeleteElements(&jobs_); |
| 1240 unassigned_job_count_ = 0; | 1199 unassigned_job_count_ = 0; |
| 1241 | 1200 |
| 1242 // Cancel pending backup job. | 1201 // Cancel pending backup job. |
| 1243 weak_factory_.InvalidateWeakPtrs(); | 1202 weak_factory_.InvalidateWeakPtrs(); |
| 1244 } | 1203 } |
| 1245 | 1204 |
| 1246 } // namespace internal | 1205 } // namespace internal |
| 1247 | 1206 |
| 1248 } // namespace net | 1207 } // namespace net |
| OLD | NEW |