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