Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(376)

Side by Side Diff: net/socket/client_socket_pool_base.cc

Issue 6990036: Deciding best connection to schedule requests on based on cwnd and idle time (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Reuploading from google.com account Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/socket/client_socket_pool_base.h ('k') | net/socket/socks5_client_socket.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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();
412 double num_kb = static_cast<double>(bytes_read) / 1024.0;
413 DCHECK_GE(num_kb, 0);
414
415 int idle_time_sec = (base::TimeTicks::Now() - it->start_time).InSeconds();
416 idle_time_sec = std::max(1, idle_time_sec);
417
418 if (g_socket_reuse_policy_penalty_exponent >= 0 && num_kb >= 0) {
419 score = num_kb / pow(idle_time_sec,
420 g_socket_reuse_policy_penalty_exponent);
421 }
422
423 // Equality to prefer recently used connection.
424 if (score >= max_score) {
425 idle_socket_it = it;
426 max_score = score;
427 }
383 } 428 }
384 429
385 ++it; 430 ++it;
386 } 431 }
387 432
388 // If we haven't found an idle socket, that means there are no used idle 433 // If we haven't found an idle socket, that means there are no used idle
389 // sockets. Pick the oldest (first) idle socket (FIFO). 434 // sockets. Pick the oldest (first) idle socket (FIFO).
390 435
391 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty()) 436 if (idle_socket_it == idle_sockets->end() && !idle_sockets->empty())
392 idle_socket_it = idle_sockets->begin(); 437 idle_socket_it = idle_sockets->begin();
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after
1081 // Delete active jobs. 1126 // Delete active jobs.
1082 STLDeleteElements(&jobs_); 1127 STLDeleteElements(&jobs_);
1083 1128
1084 // Cancel pending backup job. 1129 // Cancel pending backup job.
1085 method_factory_.RevokeAll(); 1130 method_factory_.RevokeAll();
1086 } 1131 }
1087 1132
1088 } // namespace internal 1133 } // namespace internal
1089 1134
1090 } // namespace net 1135 } // namespace net
OLDNEW
« no previous file with comments | « net/socket/client_socket_pool_base.h ('k') | net/socket/socks5_client_socket.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698