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 <utility> | |
| 9 #include "base/command_line.h" | |
| 7 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 8 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
| 12 #include "base/logging.h" | |
| 9 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 10 #include "base/metrics/stats_counters.h" | 14 #include "base/metrics/stats_counters.h" |
| 11 #include "base/stl_util-inl.h" | 15 #include "base/stl_util-inl.h" |
| 16 #include "base/string_number_conversions.h" | |
| 12 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 13 #include "base/time.h" | 18 #include "base/time.h" |
| 14 #include "base/values.h" | 19 #include "base/values.h" |
| 15 #include "net/base/net_log.h" | 20 #include "net/base/net_log.h" |
| 16 #include "net/base/net_errors.h" | 21 #include "net/base/net_errors.h" |
| 17 #include "net/socket/client_socket_handle.h" | 22 #include "net/socket/client_socket_handle.h" |
| 18 | 23 |
| 19 using base::TimeDelta; | 24 using base::TimeDelta; |
| 20 | 25 |
| 21 namespace { | 26 namespace { |
| 22 | 27 |
| 23 // 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 |
| 24 // reused. | 29 // reused. |
| 25 // | 30 // |
| 26 // 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 |
| 27 // 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 |
| 28 // some conditions. See http://crbug.com/4606. | 33 // some conditions. See http://crbug.com/4606. |
| 29 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. | 34 const int kCleanupInterval = 10; // DO NOT INCREASE THIS TIMEOUT. |
| 30 | 35 |
| 31 // 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 |
| 32 // after a certain timeout has passed without receiving an ACK. | 37 // after a certain timeout has passed without receiving an ACK. |
| 33 bool g_connect_backup_jobs_enabled = true; | 38 bool g_connect_backup_jobs_enabled = true; |
| 34 | 39 |
| 40 // Decay coef. | |
| 41 double g_socket_reuse_policy_penalty_coef = -1; | |
|
willchan no longer on Chromium
2011/06/14 12:01:22
Why is this called a coefficient? It looks like an
Gagan
2011/06/14 18:25:02
Done.
| |
| 42 | |
| 35 } // namespace | 43 } // namespace |
| 36 | 44 |
| 37 namespace net { | 45 namespace net { |
| 38 | 46 |
| 47 // static | |
| 48 double SetSocketReusePolicy(int policy) { | |
| 49 using internal::ClientSocketPoolBaseHelper; | |
| 50 double old_value = g_socket_reuse_policy_penalty_coef; | |
| 51 g_socket_reuse_policy_penalty_coef = | |
| 52 policy == ClientSocketPoolBaseHelper::USE_WARMEST_SOCKET ? 0 : | |
|
willchan no longer on Chromium
2011/06/14 12:01:22
Chaining a ternary operator is not very readable.
Gagan
2011/06/14 18:25:02
Done.
| |
| 53 (policy == ClientSocketPoolBaseHelper::USE_WARM_SOCKET ? 0.25 : -1); | |
| 54 DLOG(INFO) << "Setting g_socket_reuse_policy_penalty_coef = " | |
| 55 << g_socket_reuse_policy_penalty_coef; | |
| 56 // TODO(gagan): Remove this. | |
| 57 LOG(ERROR) << "Setting g_socket_reuse_policy_penalty_coef = " | |
| 58 << g_socket_reuse_policy_penalty_coef; | |
| 59 return old_value; | |
| 60 } | |
| 61 | |
| 39 ConnectJob::ConnectJob(const std::string& group_name, | 62 ConnectJob::ConnectJob(const std::string& group_name, |
| 40 base::TimeDelta timeout_duration, | 63 base::TimeDelta timeout_duration, |
| 41 Delegate* delegate, | 64 Delegate* delegate, |
| 42 const BoundNetLog& net_log) | 65 const BoundNetLog& net_log) |
| 43 : group_name_(group_name), | 66 : group_name_(group_name), |
| 44 timeout_duration_(timeout_duration), | 67 timeout_duration_(timeout_duration), |
| 45 delegate_(delegate), | 68 delegate_(delegate), |
| 46 net_log_(net_log), | 69 net_log_(net_log), |
| 47 idle_(true), | 70 idle_(true), |
| 48 preconnect_state_(NOT_PRECONNECT) { | 71 preconnect_state_(NOT_PRECONNECT) { |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 } | 379 } |
| 357 } | 380 } |
| 358 | 381 |
| 359 return rv; | 382 return rv; |
| 360 } | 383 } |
| 361 | 384 |
| 362 bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( | 385 bool ClientSocketPoolBaseHelper::AssignIdleSocketToGroup( |
| 363 const Request* request, Group* group) { | 386 const Request* request, Group* group) { |
| 364 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); | 387 std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
| 365 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); | 388 std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
| 389 double max_score = -1; | |
| 390 std::pair<int, int64> best_pair, current_pair; | |
| 391 | |
| 392 std::string debug_log(request->handle()->group_name()); | |
| 393 double rtt_ms = 0; | |
| 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(); | |
| 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_coef >= 0) { | |
| 418 score = num_kb / pow(idle_time_sec, | |
| 419 g_socket_reuse_policy_penalty_coef); | |
| 420 | |
| 421 debug_log = StringPrintf("%s\n%f\t%d\t%f", debug_log.data(), | |
| 422 num_kb, idle_time_sec, score); | |
| 423 } | |
| 424 | |
| 425 // Equality to prefer recently used connection. | |
| 426 if (score >= max_score) { | |
| 427 idle_socket_it = it; | |
| 428 max_score = score; | |
| 429 best_pair = std::make_pair(bytes_read, idle_time_sec); | |
| 430 rtt_ms = it->socket->GetConnectTimeMicros(); | |
| 431 } | |
| 383 } | 432 } |
| 384 | 433 |
| 385 ++it; | 434 ++it; |
| 386 } | 435 } |
| 436 debug_log = StringPrintf("%s\nChose socket <%d %ld>\nRtt = %f", | |
| 437 debug_log.data(), | |
| 438 best_pair.first, best_pair.second, | |
| 439 rtt_ms); | |
| 440 LOG(ERROR) << "\n\n----------------------------------------" << debug_log; | |
| 387 | 441 |
| 388 // If we haven't found an idle socket, that means there are no used idle | 442 // If we haven't found an idle socket, that means there are no used idle |
| 389 // sockets. Pick the oldest (first) idle socket (FIFO). | 443 // sockets. Pick the oldest (first) idle socket (FIFO). |
| 390 | 444 |
| 391 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) | 445 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) |
| 392 idle_socket_it = idle_sockets->begin(); | 446 idle_socket_it = idle_sockets->begin(); |
| 393 | 447 |
| 394 if (idle_socket_it != idle_sockets->end()) { | 448 if (idle_socket_it != idle_sockets->end()) { |
| 395 DecrementIdleCount(); | 449 DecrementIdleCount(); |
| 396 base::TimeDelta idle_time = | 450 base::TimeDelta idle_time = |
| (...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1081 // Delete active jobs. | 1135 // Delete active jobs. |
| 1082 STLDeleteElements(&jobs_); | 1136 STLDeleteElements(&jobs_); |
| 1083 | 1137 |
| 1084 // Cancel pending backup job. | 1138 // Cancel pending backup job. |
| 1085 method_factory_.RevokeAll(); | 1139 method_factory_.RevokeAll(); |
| 1086 } | 1140 } |
| 1087 | 1141 |
| 1088 } // namespace internal | 1142 } // namespace internal |
| 1089 | 1143 |
| 1090 } // namespace net | 1144 } // namespace net |
| OLD | NEW |