OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_BASE_BACKOFF_ITEM_H_ |
| 6 #define NET_BASE_BACKOFF_ITEM_H_ |
| 7 #pragma once |
| 8 |
| 9 #include "base/threading/non_thread_safe.h" |
| 10 #include "base/time.h" |
| 11 |
| 12 namespace net { |
| 13 |
| 14 // Provides the core logic needed for randomized exponential back-off |
| 15 // on requests to a given resource, given a back-off policy. |
| 16 // |
| 17 // This utility class knows nothing about network specifics; it is |
| 18 // intended for reuse in various networking scenarios. |
| 19 class BackoffEntry : public base::NonThreadSafe { |
| 20 public: |
| 21 |
| 22 // The set of parameters that define a back-off policy. |
| 23 struct Policy { |
| 24 // Number of initial errors (in sequence) to ignore before applying |
| 25 // exponential back-off rules. |
| 26 int num_errors_to_ignore_; |
| 27 |
| 28 // Initial delay for exponential back-off. |
| 29 int initial_backoff_ms_; |
| 30 |
| 31 // Factor by which the waiting time will be multiplied. |
| 32 double multiply_factor_; |
| 33 |
| 34 // Fuzzing percentage. ex: 10% will spread requests randomly |
| 35 // between 90%-100% of the calculated time. |
| 36 double jitter_factor_; |
| 37 |
| 38 // Maximum amount of time we are willing to delay our request. |
| 39 int maximum_backoff_ms_; |
| 40 |
| 41 // Time to keep an entry from being discarded even when it |
| 42 // has no significant state, -1 to never discard. |
| 43 int entry_lifetime_ms_; |
| 44 }; |
| 45 |
| 46 // Lifetime of policy must enclose lifetime of BackoffEntry. The |
| 47 // pointer must be valid but is not dereferenced during construction. |
| 48 BackoffEntry(const Policy* policy); |
| 49 ~BackoffEntry(); |
| 50 |
| 51 // Inform this item that a request for the network resource it is |
| 52 // tracking was made, and whether it failed or succeeded. |
| 53 void InformOfRequest(bool succeeded); |
| 54 |
| 55 // Returns true if a request for the resource this item tracks should |
| 56 // be rejected at the present time due to exponential backoff policy. |
| 57 bool ShouldRejectRequest() const; |
| 58 |
| 59 // Returns the absolute time after which this entry (given its present |
| 60 // state) will no longer reject requests. |
| 61 base::TimeTicks GetReleaseTime() const; |
| 62 |
| 63 // Causes this object reject requests until the specified absolute time. |
| 64 // This can be used to e.g. implement support for a Retry-After header. |
| 65 void SetCustomReleaseTime(const base::TimeTicks release_time); |
| 66 |
| 67 // Returns true if this object has no significant state (i.e. you could |
| 68 // just as well start with a fresh BackoffEntry object), and hasn't |
| 69 // had for Policy::entry_lifetime_ms_. |
| 70 bool CanDiscard() const; |
| 71 |
| 72 protected: |
| 73 |
| 74 // Equivalent to TimeTicks::Now(), virtual so unit tests can override. |
| 75 virtual base::TimeTicks GetTimeNow() const; |
| 76 |
| 77 // Calculates when requests should again be allowed through. |
| 78 base::TimeTicks CalculateReleaseTime() const; |
| 79 |
| 80 private: |
| 81 |
| 82 // Timestamp calculated by the exponential back-off algorithm at which we are |
| 83 // allowed to start sending requests again. |
| 84 base::TimeTicks exponential_backoff_release_time_; |
| 85 |
| 86 // Counts request errors; reset on success. |
| 87 int failure_count_; |
| 88 |
| 89 const Policy* policy_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(BackoffEntry); |
| 92 }; |
| 93 |
| 94 } // namespace net |
| 95 |
| 96 #endif // NET_BASE_BACKOFF_ITEM_H_ |
OLD | NEW |