| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 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/request_throttler/request_throttler_entry.h" |
| 6 |
| 7 #include <cmath> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/rand_util.h" |
| 11 #include "base/string_util.h" |
| 12 #include "net/request_throttler/request_throttler_header_interface.h" |
| 13 |
| 14 const int RequestThrottlerEntry::kAdditionalConstantMs = 100; |
| 15 const int RequestThrottlerEntry::kEntryLifetimeSec = 120; |
| 16 const double RequestThrottlerEntry::kJitterFactor = 0.4; |
| 17 const int RequestThrottlerEntry::kInitialBackoffMs = 700; |
| 18 const int RequestThrottlerEntry::kMaximumBackoffMs = 24 * 60 * 60 * 1000; |
| 19 const double RequestThrottlerEntry::kMultiplyFactor = 2.0; |
| 20 const char RequestThrottlerEntry::kRetryHeaderName[] = "X-Retry-After"; |
| 21 |
| 22 RequestThrottlerEntry::RequestThrottlerEntry() |
| 23 : release_time_(base::TimeTicks::Now()), |
| 24 num_times_delayed_(0), |
| 25 is_managed_(false) { |
| 26 SaveState(); |
| 27 } |
| 28 |
| 29 RequestThrottlerEntry::~RequestThrottlerEntry() { |
| 30 } |
| 31 |
| 32 bool RequestThrottlerEntry::IsRequestAllowed() const { |
| 33 return (release_time_ <= GetTimeNow()); |
| 34 } |
| 35 |
| 36 void RequestThrottlerEntry::UpdateWithResponse( |
| 37 const RequestThrottlerHeaderInterface* response) { |
| 38 SaveState(); |
| 39 if (response->GetResponseCode() >= 500) { |
| 40 num_times_delayed_++; |
| 41 release_time_ = std::max(CalculateReleaseTime(), release_time_); |
| 42 is_managed_ = true; |
| 43 } else { |
| 44 // We slowly decay the number of times delayed instead of resetting it to 0 |
| 45 // in order to stay stable if we received lots of requests with |
| 46 // malformed bodies at the same time. |
| 47 if (num_times_delayed_ > 0) |
| 48 num_times_delayed_--; |
| 49 is_managed_ = false; |
| 50 // The reason why we are not just cutting release_time to GetTimeNow() is |
| 51 // on the one hand, it would unset delay put by our custom retry-after |
| 52 // header and on the other we would like to push every request up to our |
| 53 // "horizon" when dealing with multiple in-flight request. Ex: If we send |
| 54 // three request and we receive 2 failures and 1 success. The success that |
| 55 // follows those failures will not reset release time further request will |
| 56 // then need to wait the delay caused by the 2 failures. |
| 57 release_time_ = std::max(GetTimeNow(), release_time_); |
| 58 if (response->GetNormalizedValue(kRetryHeaderName) != std::string()) |
| 59 HandleCustomRetryAfter(response->GetNormalizedValue(kRetryHeaderName)); |
| 60 } |
| 61 } |
| 62 |
| 63 bool RequestThrottlerEntry::IsEntryOutdated() const { |
| 64 int64 unused_since_ms = (GetTimeNow() - release_time_).InMilliseconds(); |
| 65 int64 lifespan_ms = kEntryLifetimeSec * 1000; |
| 66 |
| 67 // Release time is further than now, we are managing it. |
| 68 if (unused_since_ms < 0) |
| 69 return false; |
| 70 |
| 71 // There are two cases. First one, when the entry is currently being managed |
| 72 // and should not be collected unless it is older than the maximum allowed |
| 73 // back-off. The other one, when the entry is outdated, unmanaged and should |
| 74 // be collected. |
| 75 if (is_managed_) |
| 76 return unused_since_ms > kMaximumBackoffMs; |
| 77 |
| 78 return unused_since_ms > lifespan_ms; |
| 79 } |
| 80 |
| 81 void RequestThrottlerEntry::ReceivedContentWasMalformed() { |
| 82 // We should never revert to less back-off or else an attacker could put a |
| 83 // malformed body in cache and replay it to decrease delay. |
| 84 num_times_delayed_ = |
| 85 std::max(old_values_.number_of_failed_requests, num_times_delayed_); |
| 86 num_times_delayed_++; |
| 87 is_managed_ = true; |
| 88 release_time_ = std::max(CalculateReleaseTime(), |
| 89 std::max(old_values_.release_time, release_time_)); |
| 90 } |
| 91 |
| 92 base::TimeTicks RequestThrottlerEntry::CalculateReleaseTime() { |
| 93 double delay = kInitialBackoffMs; |
| 94 delay *= pow(kMultiplyFactor, num_times_delayed_); |
| 95 delay += kAdditionalConstantMs; |
| 96 delay -= base::RandDouble() * kJitterFactor * delay; |
| 97 |
| 98 // Ensure that we do not exceed maximum delay |
| 99 int64 delay_int = static_cast<int64>(delay + 0.5); |
| 100 delay_int = std::min(delay_int, static_cast<int64>(kMaximumBackoffMs)); |
| 101 |
| 102 return GetTimeNow() + base::TimeDelta::FromMilliseconds(delay_int); |
| 103 } |
| 104 |
| 105 base::TimeTicks RequestThrottlerEntry::GetTimeNow() const { |
| 106 return base::TimeTicks::Now(); |
| 107 } |
| 108 |
| 109 void RequestThrottlerEntry::HandleCustomRetryAfter( |
| 110 const std::string& header_value) { |
| 111 // Input parameter is the number of seconds to wait in a floating point value. |
| 112 double time_in_sec = 0; |
| 113 bool conversion_is_ok = StringToDouble(header_value, &time_in_sec); |
| 114 |
| 115 // Conversion of custom retry-after header value failed. |
| 116 if (!conversion_is_ok) |
| 117 return; |
| 118 |
| 119 // We must use an int value later so we transform this in milliseconds. |
| 120 int64 value_ms = static_cast<int64>(0.5 + time_in_sec * 1000); |
| 121 |
| 122 if (kMaximumBackoffMs < value_ms || value_ms < 0) |
| 123 return; |
| 124 |
| 125 release_time_ = std::max( |
| 126 (GetTimeNow() + base::TimeDelta::FromMilliseconds(value_ms)), |
| 127 release_time_); |
| 128 } |
| 129 |
| 130 void RequestThrottlerEntry::SaveState() { |
| 131 old_values_.release_time = release_time_; |
| 132 old_values_.number_of_failed_requests = num_times_delayed_; |
| 133 } |
| OLD | NEW |