OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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_NETWORK_THROTTLE_MANAGER_IMPL_H_ |
| 6 #define NET_BASE_NETWORK_THROTTLE_MANAGER_IMPL_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <memory> |
| 10 #include <set> |
| 11 |
| 12 #include "base/memory/weak_ptr.h" |
| 13 #include "base/time/tick_clock.h" |
| 14 #include "base/time/time.h" |
| 15 #include "base/timer/timer.h" |
| 16 #include "net/base/network_throttle_manager.h" |
| 17 #include "net/base/percentile_estimator.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 // The NetworkThrottleManagerImpl implements the following semantics: |
| 22 // * All throttles of priority above THROTTLED are created unblocked. |
| 23 // * Throttles of priority THROTTLED are created unblocked, unless |
| 24 // there are |kActiveRequestThrottlingLimit| or more throttles active, |
| 25 // in which case they are created blocked. |
| 26 // When that condition is no longer true, throttles of priority |
| 27 // THROTTLED are unblocked, in FIFO order. |
| 28 // * Throttles that have been alive for more than |kMedianLifetimeMultiple| |
| 29 // times the current estimate of the throttle median lifetime do |
| 30 // not count against the |kActiveRequestThrottlingLimit| limit. |
| 31 class NET_EXPORT NetworkThrottleManagerImpl : public NetworkThrottleManager { |
| 32 public: |
| 33 // Maximum number of active requests before new THROTTLED throttles |
| 34 // are created blocked. Throttles are unblocked as the active requests |
| 35 // fall below this limit. |
| 36 static const size_t kActiveRequestThrottlingLimit; |
| 37 |
| 38 // Note that the following constants are implementation details exposed in the |
| 39 // header file only for testing, and should not be relied on by consumers. |
| 40 |
| 41 // Constants used for the running estimate of the median lifetime |
| 42 // for throttles created by this class. That estimate is used to detect |
| 43 // throttles that are "unusually old" and hence may represent hanging GETs |
| 44 // or long-running streams. Such throttles should not be considered |
| 45 // "active" for the purposes of determining whether THROTTLED throttles |
| 46 // should be created in a blocked state. |
| 47 // Note that the precise details of this algorithm aren't very important; |
| 48 // specifically, if it takes a while for the median estimate to reach the |
| 49 // "actual" median of a request stream, the consequence is either a bit more |
| 50 // of a delay in unblocking THROTTLED requests or more THROTTLED requests |
| 51 // being unblocked than would be ideal (i.e. performance tweaks at |
| 52 // the margins). |
| 53 |
| 54 // Multiple of the current median lifetime beyond which a throttle is |
| 55 // considered "unusually old" and not considered in counting active |
| 56 // requests. This is used instead of a percentile estimate because the goal |
| 57 // is eliminating requests that are qualitatively different |
| 58 // (e.g. hanging gets, streams), and the percentage of all requests |
| 59 // that are in that category can vary greatly. |
| 60 static const int kMedianLifetimeMultiple; |
| 61 |
| 62 // The median lifetime estimate starts at class creation at |
| 63 // |kInitialMedianInMs|. |
| 64 static const int kInitialMedianInMs; |
| 65 |
| 66 NetworkThrottleManagerImpl(); |
| 67 ~NetworkThrottleManagerImpl() override; |
| 68 |
| 69 // NetworkThrottleManager: |
| 70 std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, |
| 71 RequestPriority priority, |
| 72 bool ignore_limits) override; |
| 73 |
| 74 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock); |
| 75 |
| 76 // If the |NowTicks()| value of |tick_clock_| is greater than the |
| 77 // time the outstanding_recomputation_timer_ has set to go off, Stop() |
| 78 // the timer and manually run the associated user task. This is to allow |
| 79 // "fast-forwarding" of the clock for testing by working around |
| 80 // base::Timer's direct use of base::TimeTicks rather than a base::TickClock. |
| 81 // |
| 82 // Note specifically that base::Timer::Start takes a time delta into the |
| 83 // future and adds it to base::TimeTicks::Now() to get |
| 84 // base::Timer::desired_run_time(), which is what this method compares |
| 85 // |tick_clock_->NowTicks()| against. So tests should be written so that |
| 86 // the timer Start() routine whose callback should be run is called |
| 87 // with |tick_clock_| in accord with wallclock time. This routine can then |
| 88 // be called with |tick_clock_| set into the future. |
| 89 // |
| 90 // Returns true if there was a timer running and it was triggerred |
| 91 // (|tick_clock_->NowTicks() > |
| 92 // outstanding_recomputation_timer_.desired_run_time()|). |
| 93 bool ConditionallyTriggerTimerForTesting(); |
| 94 |
| 95 private: |
| 96 class ThrottleImpl; |
| 97 using ThrottleList = std::list<ThrottleImpl*>; |
| 98 |
| 99 void OnThrottlePriorityChanged(ThrottleImpl* throttle, |
| 100 RequestPriority old_priority, |
| 101 RequestPriority new_priority); |
| 102 void OnThrottleDestroyed(ThrottleImpl* throttle); |
| 103 |
| 104 // Recompute how many requests count as outstanding (i.e. |
| 105 // are not older than kMedianLifetimeMultiple * MedianThrottleLifetime()). |
| 106 // If outstanding_recomputation_timer_ is not set, it will be set |
| 107 // to the earliest a throttle might "age out" of the outstanding list. |
| 108 void RecomputeOutstanding(); |
| 109 |
| 110 // Unblock the specified throttle. May result in re-entrant calls |
| 111 // into NetworkThrottleManagerImpl. |
| 112 void UnblockThrottle(ThrottleImpl* throttle); |
| 113 |
| 114 // Recomputes how many requests count as outstanding, checks to see |
| 115 // if any currently blocked throttles should be unblocked, |
| 116 // and unblock them if so. Note that unblocking may result in |
| 117 // re-entrant calls to this class, so no assumptions about state persistence |
| 118 // should be made across this call. |
| 119 void MaybeUnblockThrottles(); |
| 120 |
| 121 PercentileEstimator lifetime_median_estimate_; |
| 122 |
| 123 // base::Timer controlling outstanding request recomputation. |
| 124 // |
| 125 // This is started whenever it is not running and a new throttle is |
| 126 // added to |outstanding_throttles_|, and is never cleared except by |
| 127 // execution, which re-starts it if there are any |
| 128 // outstanding_throttles_. So it should always be running if any |
| 129 // throttles are outstanding. This guarantees that the class will |
| 130 // eventually detect aging out of outstanding throttles and unblock |
| 131 // throttles blocked on those outstanding throttles. |
| 132 base::Timer outstanding_recomputation_timer_; |
| 133 |
| 134 // FIFO of OUTSTANDING throttles (ordered by time of entry into the |
| 135 // OUTSTANDING state). |
| 136 ThrottleList outstanding_throttles_; |
| 137 |
| 138 // FIFO list of BLOCKED throttles. This is a list so that the |
| 139 // throttles can store iterators to themselves. |
| 140 ThrottleList blocked_throttles_; |
| 141 |
| 142 // For testing. |
| 143 std::unique_ptr<base::TickClock> tick_clock_; |
| 144 |
| 145 base::WeakPtrFactory<NetworkThrottleManagerImpl> weak_ptr_factory_; |
| 146 |
| 147 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManagerImpl); |
| 148 }; |
| 149 |
| 150 } // namespace net |
| 151 |
| 152 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_IMPL_H_ |
OLD | NEW |