[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/dns/dns_session.h" |
| 6 | |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 7 | #include <stdint.h> |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 8 | |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 9 | #include <limits> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 10 | #include <utility> |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 11 | |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.h" |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 16 | #include "base/metrics/field_trial.h" |
asvitkine | c3c9372 | 2015-06-17 14:48:37 | [diff] [blame] | 17 | #include "base/metrics/histogram_macros.h" |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 18 | #include "base/metrics/sample_vector.h" |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 19 | #include "base/rand_util.h" |
| 20 | #include "base/stl_util.h" |
[email protected] | 66e96c4 | 2013-06-28 15:20:31 | [diff] [blame] | 21 | #include "base/time/time.h" |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 22 | #include "base/values.h" |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 23 | #include "net/base/ip_endpoint.h" |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 24 | #include "net/base/net_errors.h" |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 25 | #include "net/dns/dns_config_service.h" |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 26 | #include "net/dns/dns_socket_pool.h" |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 27 | #include "net/dns/dns_util.h" |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 28 | #include "net/log/net_log_event_type.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 29 | #include "net/log/net_log_source.h" |
| 30 | #include "net/log/net_log_with_source.h" |
tfarina | 5dd13c2 | 2016-11-16 12:08:26 | [diff] [blame^] | 31 | #include "net/socket/datagram_client_socket.h" |
[email protected] | bdb6598 | 2012-12-20 20:44:59 | [diff] [blame] | 32 | #include "net/socket/stream_socket.h" |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 33 | |
| 34 | namespace net { |
| 35 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 36 | namespace { |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 37 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 38 | // Set min timeout, in case we are talking to a local DNS proxy. |
| 39 | const unsigned kMinTimeoutMs = 10; |
| 40 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 41 | // Default maximum timeout between queries, even with exponential backoff. |
| 42 | // (Can be overridden by field trial.) |
| 43 | const unsigned kDefaultMaxTimeoutMs = 5000; |
| 44 | |
| 45 | // Maximum RTT that will fit in the RTT histograms. |
| 46 | const int32_t kRTTMaxMs = 30000; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 47 | // Number of buckets in the histogram of observed RTTs. |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 48 | const size_t kRTTBucketCount = 350; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 49 | // Target percentile in the RTT histogram used for retransmission timeout. |
| 50 | const unsigned kRTOPercentile = 99; |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 51 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 52 | } // namespace |
| 53 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 54 | // Runtime statistics of DNS server. |
| 55 | struct DnsSession::ServerStats { |
| 56 | ServerStats(base::TimeDelta rtt_estimate_param, RttBuckets* buckets) |
| 57 | : last_failure_count(0), rtt_estimate(rtt_estimate_param) { |
| 58 | rtt_histogram.reset(new base::SampleVector(buckets)); |
[email protected] | a144bd2 | 2013-07-29 21:53:10 | [diff] [blame] | 59 | // Seed histogram with 2 samples at |rtt_estimate| timeout. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 60 | rtt_histogram->Accumulate( |
| 61 | static_cast<base::HistogramBase::Sample>(rtt_estimate.InMilliseconds()), |
| 62 | 2); |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Count of consecutive failures after last success. |
| 66 | int last_failure_count; |
| 67 | |
| 68 | // Last time when server returned failure or timeout. |
| 69 | base::Time last_failure; |
| 70 | // Last time when server returned success. |
| 71 | base::Time last_success; |
| 72 | |
| 73 | // Estimated RTT using moving average. |
| 74 | base::TimeDelta rtt_estimate; |
| 75 | // Estimated error in the above. |
| 76 | base::TimeDelta rtt_deviation; |
| 77 | |
| 78 | // A histogram of observed RTT . |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 79 | std::unique_ptr<base::SampleVector> rtt_histogram; |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 80 | |
| 81 | DISALLOW_COPY_AND_ASSIGN(ServerStats); |
| 82 | }; |
| 83 | |
| 84 | // static |
| 85 | base::LazyInstance<DnsSession::RttBuckets>::Leaky DnsSession::rtt_buckets_ = |
| 86 | LAZY_INSTANCE_INITIALIZER; |
| 87 | |
| 88 | DnsSession::RttBuckets::RttBuckets() : base::BucketRanges(kRTTBucketCount + 1) { |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 89 | base::Histogram::InitializeBucketRanges(1, kRTTMaxMs, this); |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 90 | } |
| 91 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 92 | DnsSession::SocketLease::SocketLease( |
| 93 | scoped_refptr<DnsSession> session, |
| 94 | unsigned server_index, |
| 95 | std::unique_ptr<DatagramClientSocket> socket) |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 96 | : session_(session), |
| 97 | server_index_(server_index), |
| 98 | socket_(std::move(socket)) {} |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 99 | |
| 100 | DnsSession::SocketLease::~SocketLease() { |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 101 | session_->FreeSocket(server_index_, std::move(socket_)); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 104 | DnsSession::DnsSession(const DnsConfig& config, |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 105 | std::unique_ptr<DnsSocketPool> socket_pool, |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 106 | const RandIntCallback& rand_int_callback, |
| 107 | NetLog* net_log) |
| 108 | : config_(config), |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 109 | socket_pool_(std::move(socket_pool)), |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 110 | rand_callback_(base::Bind(rand_int_callback, |
| 111 | 0, |
| 112 | std::numeric_limits<uint16_t>::max())), |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 113 | net_log_(net_log), |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 114 | server_index_(0) { |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 115 | socket_pool_->Initialize(&config_.nameservers, net_log); |
drbasic | f0d1b26 | 2016-08-23 06:10:42 | [diff] [blame] | 116 | UMA_HISTOGRAM_CUSTOM_COUNTS("AsyncDNS.ServerCount", |
| 117 | config_.nameservers.size(), 1, 10, 11); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 118 | UpdateTimeouts(NetworkChangeNotifier::GetConnectionType()); |
| 119 | InitializeServerStats(); |
| 120 | NetworkChangeNotifier::AddConnectionTypeObserver(this); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 121 | } |
| 122 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 123 | DnsSession::~DnsSession() { |
| 124 | RecordServerStats(); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 125 | NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 126 | } |
| 127 | |
| 128 | void DnsSession::UpdateTimeouts(NetworkChangeNotifier::ConnectionType type) { |
| 129 | initial_timeout_ = GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 130 | "AsyncDnsInitialTimeoutMsByConnectionType", config_.timeout, type); |
| 131 | max_timeout_ = GetTimeDeltaForConnectionTypeFromFieldTrialOrDefault( |
| 132 | "AsyncDnsMaxTimeoutMsByConnectionType", |
| 133 | base::TimeDelta::FromMilliseconds(kDefaultMaxTimeoutMs), type); |
| 134 | } |
| 135 | |
| 136 | void DnsSession::InitializeServerStats() { |
| 137 | server_stats_.clear(); |
| 138 | for (size_t i = 0; i < config_.nameservers.size(); ++i) { |
ricea | 2deef68 | 2016-09-09 08:04:07 | [diff] [blame] | 139 | server_stats_.push_back(base::MakeUnique<ServerStats>( |
| 140 | initial_timeout_, rtt_buckets_.Pointer())); |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
| 144 | void DnsSession::OnConnectionTypeChanged( |
| 145 | NetworkChangeNotifier::ConnectionType type) { |
| 146 | UpdateTimeouts(type); |
| 147 | const char* kTrialName = "AsyncDnsFlushServerStatsOnConnectionTypeChange"; |
| 148 | if (base::FieldTrialList::FindFullName(kTrialName) == "enable") { |
| 149 | RecordServerStats(); |
| 150 | InitializeServerStats(); |
| 151 | } |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 152 | } |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 153 | |
avi | 6516805 | 2015-12-01 19:27:07 | [diff] [blame] | 154 | uint16_t DnsSession::NextQueryId() const { |
| 155 | return static_cast<uint16_t>(rand_callback_.Run()); |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 156 | } |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 157 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 158 | unsigned DnsSession::NextFirstServerIndex() { |
| 159 | unsigned index = NextGoodServerIndex(server_index_); |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 160 | if (config_.rotate) |
| 161 | server_index_ = (server_index_ + 1) % config_.nameservers.size(); |
[email protected] | d0d49dd8 | 2012-01-26 00:03:59 | [diff] [blame] | 162 | return index; |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 165 | unsigned DnsSession::NextGoodServerIndex(unsigned server_index) { |
| 166 | unsigned index = server_index; |
| 167 | base::Time oldest_server_failure(base::Time::Now()); |
| 168 | unsigned oldest_server_failure_index = 0; |
| 169 | |
| 170 | UMA_HISTOGRAM_BOOLEAN("AsyncDNS.ServerIsGood", |
| 171 | server_stats_[server_index]->last_failure.is_null()); |
| 172 | |
| 173 | do { |
| 174 | base::Time cur_server_failure = server_stats_[index]->last_failure; |
| 175 | // If number of failures on this server doesn't exceed number of allowed |
| 176 | // attempts, return its index. |
| 177 | if (server_stats_[server_index]->last_failure_count < config_.attempts) { |
| 178 | return index; |
| 179 | } |
| 180 | // Track oldest failed server. |
| 181 | if (cur_server_failure < oldest_server_failure) { |
| 182 | oldest_server_failure = cur_server_failure; |
| 183 | oldest_server_failure_index = index; |
| 184 | } |
| 185 | index = (index + 1) % config_.nameservers.size(); |
| 186 | } while (index != server_index); |
| 187 | |
| 188 | // If we are here it means that there are no successful servers, so we have |
| 189 | // to use one that has failed oldest. |
| 190 | return oldest_server_failure_index; |
| 191 | } |
| 192 | |
| 193 | void DnsSession::RecordServerFailure(unsigned server_index) { |
drbasic | f0d1b26 | 2016-08-23 06:10:42 | [diff] [blame] | 194 | UMA_HISTOGRAM_CUSTOM_COUNTS("AsyncDNS.ServerFailureIndex", server_index, 1, |
| 195 | 10, 11); |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 196 | ++(server_stats_[server_index]->last_failure_count); |
| 197 | server_stats_[server_index]->last_failure = base::Time::Now(); |
| 198 | } |
| 199 | |
| 200 | void DnsSession::RecordServerSuccess(unsigned server_index) { |
| 201 | if (server_stats_[server_index]->last_success.is_null()) { |
| 202 | UMA_HISTOGRAM_COUNTS_100("AsyncDNS.ServerFailuresAfterNetworkChange", |
| 203 | server_stats_[server_index]->last_failure_count); |
| 204 | } else { |
| 205 | UMA_HISTOGRAM_COUNTS_100("AsyncDNS.ServerFailuresBeforeSuccess", |
| 206 | server_stats_[server_index]->last_failure_count); |
| 207 | } |
| 208 | server_stats_[server_index]->last_failure_count = 0; |
| 209 | server_stats_[server_index]->last_failure = base::Time(); |
| 210 | server_stats_[server_index]->last_success = base::Time::Now(); |
| 211 | } |
| 212 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 213 | void DnsSession::RecordRTT(unsigned server_index, base::TimeDelta rtt) { |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 214 | DCHECK_LT(server_index, server_stats_.size()); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 215 | |
| 216 | // For measurement, assume it is the first attempt (no backoff). |
| 217 | base::TimeDelta timeout_jacobson = NextTimeoutFromJacobson(server_index, 0); |
| 218 | base::TimeDelta timeout_histogram = NextTimeoutFromHistogram(server_index, 0); |
| 219 | UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutErrorJacobson", rtt - timeout_jacobson); |
| 220 | UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutErrorHistogram", |
| 221 | rtt - timeout_histogram); |
| 222 | UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutErrorJacobsonUnder", |
| 223 | timeout_jacobson - rtt); |
| 224 | UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutErrorHistogramUnder", |
| 225 | timeout_histogram - rtt); |
| 226 | |
| 227 | // Jacobson/Karels algorithm for TCP. |
| 228 | // Using parameters: alpha = 1/8, delta = 1/4, beta = 4 |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 229 | base::TimeDelta& estimate = server_stats_[server_index]->rtt_estimate; |
| 230 | base::TimeDelta& deviation = server_stats_[server_index]->rtt_deviation; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 231 | base::TimeDelta current_error = rtt - estimate; |
| 232 | estimate += current_error / 8; // * alpha |
| 233 | base::TimeDelta abs_error = base::TimeDelta::FromInternalValue( |
| 234 | std::abs(current_error.ToInternalValue())); |
| 235 | deviation += (abs_error - deviation) / 4; // * delta |
| 236 | |
| 237 | // Histogram-based method. |
pkasting | 6b68a16 | 2014-12-01 22:10:29 | [diff] [blame] | 238 | server_stats_[server_index]->rtt_histogram->Accumulate( |
| 239 | static_cast<base::HistogramBase::Sample>(rtt.InMilliseconds()), 1); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void DnsSession::RecordLostPacket(unsigned server_index, int attempt) { |
| 243 | base::TimeDelta timeout_jacobson = |
| 244 | NextTimeoutFromJacobson(server_index, attempt); |
| 245 | base::TimeDelta timeout_histogram = |
| 246 | NextTimeoutFromHistogram(server_index, attempt); |
| 247 | UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutSpentJacobson", timeout_jacobson); |
| 248 | UMA_HISTOGRAM_TIMES("AsyncDNS.TimeoutSpentHistogram", timeout_histogram); |
| 249 | } |
| 250 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 251 | void DnsSession::RecordServerStats() { |
| 252 | for (size_t index = 0; index < server_stats_.size(); ++index) { |
| 253 | if (server_stats_[index]->last_failure_count) { |
| 254 | if (server_stats_[index]->last_success.is_null()) { |
| 255 | UMA_HISTOGRAM_COUNTS("AsyncDNS.ServerFailuresWithoutSuccess", |
| 256 | server_stats_[index]->last_failure_count); |
| 257 | } else { |
| 258 | UMA_HISTOGRAM_COUNTS("AsyncDNS.ServerFailuresAfterSuccess", |
| 259 | server_stats_[index]->last_failure_count); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 266 | base::TimeDelta DnsSession::NextTimeout(unsigned server_index, int attempt) { |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 267 | // Respect initial timeout (from config or field trial) if it exceeds max. |
| 268 | if (initial_timeout_ > max_timeout_) |
| 269 | return initial_timeout_; |
[email protected] | a144bd2 | 2013-07-29 21:53:10 | [diff] [blame] | 270 | return NextTimeoutFromHistogram(server_index, attempt); |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 271 | } |
| 272 | |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 273 | // Allocate a socket, already connected to the server address. |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 274 | std::unique_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket( |
| 275 | unsigned server_index, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 276 | const NetLogSource& source) { |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 277 | std::unique_ptr<DatagramClientSocket> socket; |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 278 | |
| 279 | socket = socket_pool_->AllocateSocket(server_index); |
[email protected] | dd946bb | 2013-06-12 22:53:01 | [diff] [blame] | 280 | if (!socket.get()) |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 281 | return std::unique_ptr<SocketLease>(); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 282 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 283 | socket->NetLog().BeginEvent(NetLogEventType::SOCKET_IN_USE, |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 284 | source.ToEventParametersCallback()); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 285 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 286 | SocketLease* lease = new SocketLease(this, server_index, std::move(socket)); |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 287 | return std::unique_ptr<SocketLease>(lease); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 288 | } |
| 289 | |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 290 | std::unique_ptr<StreamSocket> DnsSession::CreateTCPSocket( |
| 291 | unsigned server_index, |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 292 | const NetLogSource& source) { |
[email protected] | bdb6598 | 2012-12-20 20:44:59 | [diff] [blame] | 293 | return socket_pool_->CreateTCPSocket(server_index, source); |
| 294 | } |
| 295 | |
juliatuttle | d7fa585 | 2016-07-29 13:16:57 | [diff] [blame] | 296 | void DnsSession::ApplyPersistentData(const base::Value& data) {} |
| 297 | |
| 298 | std::unique_ptr<const base::Value> DnsSession::GetPersistentData() const { |
| 299 | return std::unique_ptr<const base::Value>(); |
| 300 | } |
| 301 | |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 302 | // Release a socket. |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 303 | void DnsSession::FreeSocket(unsigned server_index, |
danakj | 22f90e7 | 2016-04-16 01:55:40 | [diff] [blame] | 304 | std::unique_ptr<DatagramClientSocket> socket) { |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 305 | DCHECK(socket.get()); |
| 306 | |
mikecirone | 8b85c43 | 2016-09-08 19:11:00 | [diff] [blame] | 307 | socket->NetLog().EndEvent(NetLogEventType::SOCKET_IN_USE); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 308 | |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 309 | socket_pool_->FreeSocket(server_index, std::move(socket)); |
[email protected] | 120d38d | 2012-12-14 01:42:32 | [diff] [blame] | 310 | } |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 311 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 312 | base::TimeDelta DnsSession::NextTimeoutFromJacobson(unsigned server_index, |
| 313 | int attempt) { |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 314 | DCHECK_LT(server_index, server_stats_.size()); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 315 | |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 316 | base::TimeDelta timeout = server_stats_[server_index]->rtt_estimate + |
| 317 | 4 * server_stats_[server_index]->rtt_deviation; |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 318 | |
| 319 | timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); |
| 320 | |
| 321 | // The timeout doubles every full round. |
| 322 | unsigned num_backoffs = attempt / config_.nameservers.size(); |
| 323 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 324 | return std::min(timeout * (1 << num_backoffs), max_timeout_); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | base::TimeDelta DnsSession::NextTimeoutFromHistogram(unsigned server_index, |
| 328 | int attempt) { |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 329 | DCHECK_LT(server_index, server_stats_.size()); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 330 | |
mostynb | 91e0da98 | 2015-01-20 19:17:27 | [diff] [blame] | 331 | static_assert(std::numeric_limits<base::HistogramBase::Count>::is_signed, |
| 332 | "histogram base count assumed to be signed"); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 333 | |
| 334 | // Use fixed percentile of observed samples. |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 335 | const base::SampleVector& samples = |
| 336 | *server_stats_[server_index]->rtt_histogram; |
| 337 | |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 338 | base::HistogramBase::Count total = samples.TotalCount(); |
| 339 | base::HistogramBase::Count remaining_count = kRTOPercentile * total / 100; |
| 340 | size_t index = 0; |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 341 | while (remaining_count > 0 && index < rtt_buckets_.Get().size()) { |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 342 | remaining_count -= samples.GetCountAtIndex(index); |
| 343 | ++index; |
| 344 | } |
| 345 | |
| 346 | base::TimeDelta timeout = |
[email protected] | a6c84f4 | 2013-06-07 20:39:38 | [diff] [blame] | 347 | base::TimeDelta::FromMilliseconds(rtt_buckets_.Get().range(index)); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 348 | |
| 349 | timeout = std::max(timeout, base::TimeDelta::FromMilliseconds(kMinTimeoutMs)); |
| 350 | |
| 351 | // The timeout still doubles every full round. |
| 352 | unsigned num_backoffs = attempt / config_.nameservers.size(); |
| 353 | |
ttuttle | cf1158bf | 2016-03-18 16:37:44 | [diff] [blame] | 354 | return std::min(timeout * (1 << num_backoffs), max_timeout_); |
[email protected] | ae1b30b | 2013-05-23 23:06:03 | [diff] [blame] | 355 | } |
| 356 | |
[email protected] | 7556ea2 | 2011-12-08 19:29:15 | [diff] [blame] | 357 | } // namespace net |