Index: net/socket/client_socket_pool_base.cc |
=================================================================== |
--- net/socket/client_socket_pool_base.cc (revision 88789) |
+++ net/socket/client_socket_pool_base.cc (working copy) |
@@ -4,11 +4,16 @@ |
#include "net/socket/client_socket_pool_base.h" |
+#include <math.h> |
+#include <utility> |
+#include "base/command_line.h" |
#include "base/compiler_specific.h" |
#include "base/format_macros.h" |
+#include "base/logging.h" |
#include "base/message_loop.h" |
#include "base/metrics/stats_counters.h" |
#include "base/stl_util-inl.h" |
+#include "base/string_number_conversions.h" |
#include "base/string_util.h" |
#include "base/time.h" |
#include "base/values.h" |
@@ -32,6 +37,9 @@ |
// after a certain timeout has passed without receiving an ACK. |
bool g_connect_backup_jobs_enabled = true; |
+// Decay coef. |
+double g_socket_reuse_policy_penalty_coef = -1; |
+ |
} // namespace |
namespace net { |
@@ -363,7 +371,12 @@ |
const Request* request, Group* group) { |
std::list<IdleSocket>* idle_sockets = group->mutable_idle_sockets(); |
std::list<IdleSocket>::iterator idle_socket_it = idle_sockets->end(); |
+ double max_score = -1; |
+ std::pair<int, int64> best_pair, current_pair; |
+ std::string debug_log(request->handle()->group_name()); |
+ double rtt_ms = 0; |
+ |
// Iterate through the idle sockets forwards (oldest to newest) |
// * Delete any disconnected ones. |
// * If we find a used idle socket, assign to |idle_socket|. At the end, |
@@ -379,11 +392,37 @@ |
if (it->socket->WasEverUsed()) { |
// We found one we can reuse! |
- idle_socket_it = it; |
+ double score = 0; |
+ int64 bytes_read = it->socket->NumBytesRead(); |
+ double num_kb = static_cast<double>(bytes_read) / 1024.0; |
+ |
+ int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds(); |
+ idle_time_sec = std::max(1, idle_time_sec); |
+ |
+ if (g_socket_reuse_policy_penalty_coef >= 0) { |
+ score = num_kb / pow(idle_time_sec, |
+ g_socket_reuse_policy_penalty_coef); |
+ |
+ debug_log = StringPrintf("%s\n%f\t%d\t%f", debug_log.data(), |
+ num_kb, idle_time_sec, score); |
+ } |
+ |
+ // Equality to prefer recently used connection. |
+ if (score >= max_score) { |
+ idle_socket_it = it; |
+ max_score = score; |
+ best_pair = std::make_pair(bytes_read, idle_time_sec); |
+ rtt_ms = it->socket->GetConnectTimeMicros(); |
+ } |
} |
++it; |
} |
+ debug_log = StringPrintf("%s\nChose socket <%d %ld>\nRtt = %f", |
+ debug_log.data(), |
+ best_pair.first, best_pair.second, |
+ rtt_ms); |
+ LOG(ERROR) << "\n\n----------------------------------------" << debug_log; |
// If we haven't found an idle socket, that means there are no used idle |
// sockets. Pick the oldest (first) idle socket (FIFO). |
@@ -649,6 +688,20 @@ |
return old_value; |
} |
+// static |
+double ClientSocketPoolBaseHelper::SetSocketReusePolicy(int policy) { |
+ double old_value = g_socket_reuse_policy_penalty_coef; |
+ g_socket_reuse_policy_penalty_coef = |
+ policy == USE_WARMEST_SOCKET ? 0 : |
+ (policy == USE_WARM_SOCKET ? 0.25 : -1); |
+ DLOG(INFO) << "Setting g_socket_reuse_policy_penalty_coef = " |
+ << g_socket_reuse_policy_penalty_coef; |
+ // TODO(gagan): Remove this. |
+ LOG(ERROR) << "Setting g_socket_reuse_policy_penalty_coef = " |
+ << g_socket_reuse_policy_penalty_coef; |
+ return old_value; |
+} |
+ |
void ClientSocketPoolBaseHelper::EnableConnectBackupJobs() { |
connect_backup_jobs_enabled_ = g_connect_backup_jobs_enabled; |
} |