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/dns/dns_session.h" | 5 #include "net/dns/dns_session.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include "base/basictypes.h" |
8 | |
9 #include <limits> | |
10 | |
11 #include "base/bind.h" | 8 #include "base/bind.h" |
12 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
13 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
14 #include "base/metrics/sample_vector.h" | 11 #include "base/metrics/sample_vector.h" |
15 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
16 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
17 #include "base/time/time.h" | 14 #include "base/time/time.h" |
18 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
19 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
20 #include "net/dns/dns_config_service.h" | 17 #include "net/dns/dns_config_service.h" |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 DnsSession::SocketLease::~SocketLease() { | 79 DnsSession::SocketLease::~SocketLease() { |
83 session_->FreeSocket(server_index_, socket_.Pass()); | 80 session_->FreeSocket(server_index_, socket_.Pass()); |
84 } | 81 } |
85 | 82 |
86 DnsSession::DnsSession(const DnsConfig& config, | 83 DnsSession::DnsSession(const DnsConfig& config, |
87 scoped_ptr<DnsSocketPool> socket_pool, | 84 scoped_ptr<DnsSocketPool> socket_pool, |
88 const RandIntCallback& rand_int_callback, | 85 const RandIntCallback& rand_int_callback, |
89 NetLog* net_log) | 86 NetLog* net_log) |
90 : config_(config), | 87 : config_(config), |
91 socket_pool_(socket_pool.Pass()), | 88 socket_pool_(socket_pool.Pass()), |
92 rand_callback_(base::Bind(rand_int_callback, | 89 rand_callback_(base::Bind(rand_int_callback, 0, kuint16max)), |
93 0, | |
94 std::numeric_limits<uint16_t>::max())), | |
95 net_log_(net_log), | 90 net_log_(net_log), |
96 server_index_(0) { | 91 server_index_(0) { |
97 socket_pool_->Initialize(&config_.nameservers, net_log); | 92 socket_pool_->Initialize(&config_.nameservers, net_log); |
98 UMA_HISTOGRAM_CUSTOM_COUNTS( | 93 UMA_HISTOGRAM_CUSTOM_COUNTS( |
99 "AsyncDNS.ServerCount", config_.nameservers.size(), 0, 10, 11); | 94 "AsyncDNS.ServerCount", config_.nameservers.size(), 0, 10, 11); |
100 for (size_t i = 0; i < config_.nameservers.size(); ++i) { | 95 for (size_t i = 0; i < config_.nameservers.size(); ++i) { |
101 server_stats_.push_back(make_scoped_ptr( | 96 server_stats_.push_back(make_scoped_ptr( |
102 new ServerStats(config_.timeout, rtt_buckets_.Pointer()))); | 97 new ServerStats(config_.timeout, rtt_buckets_.Pointer()))); |
103 } | 98 } |
104 } | 99 } |
105 | 100 |
106 DnsSession::~DnsSession() { | 101 DnsSession::~DnsSession() { |
107 RecordServerStats(); | 102 RecordServerStats(); |
108 } | 103 } |
109 | 104 |
110 uint16_t DnsSession::NextQueryId() const { | 105 uint16 DnsSession::NextQueryId() const { |
111 return static_cast<uint16_t>(rand_callback_.Run()); | 106 return static_cast<uint16>(rand_callback_.Run()); |
112 } | 107 } |
113 | 108 |
114 unsigned DnsSession::NextFirstServerIndex() { | 109 unsigned DnsSession::NextFirstServerIndex() { |
115 unsigned index = NextGoodServerIndex(server_index_); | 110 unsigned index = NextGoodServerIndex(server_index_); |
116 if (config_.rotate) | 111 if (config_.rotate) |
117 server_index_ = (server_index_ + 1) % config_.nameservers.size(); | 112 server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
118 return index; | 113 return index; |
119 } | 114 } |
120 | 115 |
121 unsigned DnsSession::NextGoodServerIndex(unsigned server_index) { | 116 unsigned DnsSession::NextGoodServerIndex(unsigned server_index) { |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); | 293 timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); |
299 | 294 |
300 // The timeout still doubles every full round. | 295 // The timeout still doubles every full round. |
301 unsigned num_backoffs = attempt / config_.nameservers.size(); | 296 unsigned num_backoffs = attempt / config_.nameservers.size(); |
302 | 297 |
303 return std::min(timeout * (1 << num_backoffs), | 298 return std::min(timeout * (1 << num_backoffs), |
304 base::TimeDelta::FromMilliseconds(kMaxTimeoutMs)); | 299 base::TimeDelta::FromMilliseconds(kMaxTimeoutMs)); |
305 } | 300 } |
306 | 301 |
307 } // namespace net | 302 } // namespace net |
OLD | NEW |