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/time/tick_clock.h" | |
13 #include "base/time/time.h" | |
14 #include "base/timer/timer.h" | |
15 #include "net/base/network_throttle_manager.h" | |
16 | |
17 namespace net { | |
18 | |
19 // The NetworkThrottleManagerImpl implements the following semantics: | |
20 // * All throttles of priority above THROTTLED are created unblocked. | |
21 // * Throttles of priority THROTTLED are created unblocked, unless | |
22 // there are |kActiveRequestThrottlingLimit| or more throttles active, | |
23 // in which case they are created blocked. | |
24 // When that condition is no longer true, throttles of priority | |
25 // THROTTLED are unblocked, in priority order. | |
26 // * Throttles that have been alive for more than |kMedianLifetimeMultiple| | |
27 // times the current estimate of the throttle median lifetime do | |
28 // not count against the |kActiveRequestThrottlingLimit| limit. | |
29 class NetworkThrottleManagerImpl : public NetworkThrottleManager { | |
30 public: | |
31 static const size_t kActiveRequestThrottlingLimit; | |
32 static const double kMedianEstimatorDeltaMS; | |
33 static const int kInitialMedianInMS; | |
34 static const int kMedianLifetimeMultiple; | |
35 | |
36 class ThrottleImpl : public NetworkThrottleManager::Throttle { | |
mmenke
2016/07/11 17:17:20
Doesn't need to be public - nothing depends on Net
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Good point; done.
| |
37 public: | |
38 using ThrottleList = std::list<ThrottleImpl*>; | |
mmenke
2016/07/11 17:17:20
Seems weird to use this here, but to inline std::l
Randy Smith (Not in Mondays)
2016/08/29 19:44:52
Done.
| |
39 using QueuePointer = ThrottleList::iterator; | |
40 | |
41 // Caller must arrange that |*delegate| and |*manager| outlive | |
42 // the ThrottleImpl class. | |
43 ThrottleImpl(bool blocked, | |
44 RequestPriority priority, | |
45 ThrottleDelegate* delegate, | |
46 NetworkThrottleManagerImpl* manager, | |
47 QueuePointer queue_pointer); | |
48 | |
49 ~ThrottleImpl() override; | |
50 | |
51 // Throttle: | |
52 bool IsBlocked() const override; | |
53 RequestPriority Priority() const override; | |
54 void SetPriority(RequestPriority priority) override; | |
55 | |
56 RequestPriority priority() const { return priority_; } | |
57 QueuePointer queue_pointer() const { return queue_pointer_; } | |
58 void set_queue_pointer(const QueuePointer& pointer) { | |
59 queue_pointer_ = pointer; | |
60 } | |
61 base::TimeTicks creation_time() const { return creation_time_; } | |
62 | |
63 // Note that this call calls the delegate, and hence | |
64 // may result in re-entrant calls into the manager or | |
65 // ThrottleImpl. The manager should not rely on | |
66 // any state other than its own existence being persistent | |
67 // across this call. | |
68 void NotifyUnblocked(); | |
69 | |
70 private: | |
71 bool blocked_; | |
72 RequestPriority priority_; | |
73 ThrottleDelegate* const delegate_; | |
74 NetworkThrottleManagerImpl* const manager_; | |
75 | |
76 base::TimeTicks creation_time_; | |
77 | |
78 // For deletion from owning queue. | |
79 QueuePointer queue_pointer_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(ThrottleImpl); | |
82 }; | |
83 | |
84 NetworkThrottleManagerImpl(); | |
85 ~NetworkThrottleManagerImpl() override; | |
86 | |
87 // NetworkThrottleManager: | |
88 std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, | |
89 RequestPriority priority, | |
90 bool ignore_limits) override; | |
91 | |
92 void SetTickClockForTesting(std::unique_ptr<base::TickClock> tick_clock); | |
93 | |
94 // If the |NowTicks()| value of the classes clock_ is greater than the | |
95 // time the outstanding_recomputation_timer_ has set to go off, Stop() | |
96 // the timer and manually run the associated user task. This is to allow | |
97 // "fast-forwarding" of the clock for testing by working around | |
98 // base::Timer's direct use of base::TimeTicks rather than a TickClock. | |
99 void ConditionallyTriggerTimerForTesting(); | |
100 | |
101 private: | |
102 // Comparison function used to define the ordering relationship | |
103 // for |CreationOrderingSet| below. | |
104 static bool CompareThrottlesForCreationTime(ThrottleImpl* throttle1, | |
105 ThrottleImpl* throttle2); | |
106 | |
107 using CreationOrderingSet = | |
108 std::set<ThrottleImpl*, bool (*)(ThrottleImpl*, ThrottleImpl*)>; | |
109 | |
110 void OnThrottlePriorityChanged(ThrottleImpl* throttle, | |
111 RequestPriority old_priority, | |
112 RequestPriority new_priority); | |
113 void OnThrottleDestroyed(ThrottleImpl* throttle); | |
114 | |
115 // Recompute how many requests "count" as outstanding (i.e. | |
116 // are not older than kMedianLifetimeMultiple * MedianThrottleLifetime()). | |
117 void RecomputeOutstanding(); | |
118 | |
119 // Recomputes how many requests count as outstanding, checks to see | |
120 // if any currently blocked throttles should be unblocked, | |
121 // and unblock them if so. Note that unblocking may result in | |
122 // re-entrant calls to this class, so no assumptions about state persistence | |
123 // should be made across this call. | |
124 void MaybeUnblockThrottles(); | |
125 | |
126 // Convenience functions to determine which of the std::list<ThrottleImpl*>s | |
127 // from {blocked,unblocked}_requests_ apply to a give | |
128 // throttle/(blocked, priority) pair. | |
129 std::list<ThrottleImpl*>* ListForThrottle(bool blocked, | |
130 RequestPriority priority); | |
131 std::list<ThrottleImpl*>* ListForThrottle(ThrottleImpl* throttle); | |
132 | |
133 base::TimeDelta lifetime_median_estimate_; | |
134 CreationOrderingSet creation_ordering_set_; | |
135 | |
136 size_t num_throttles_blocked_; | |
mmenke
2016/07/11 17:17:20
Suggest a debug method to count the number of unbl
Randy Smith (Not in Mondays)
2016/08/29 19:44:53
Done. (And thank you--that found some bugs.)
(Co
| |
137 | |
138 // Count of throttles that are considered as outstanding. Throttles that | |
139 // are too old or blocked are not counted. | |
140 size_t num_effective_outstanding_; | |
141 | |
142 // Timer controlling outstanding request recomputation. | |
143 base::Timer outstanding_recomputation_timer_; | |
144 | |
145 // Lists of blocked/unblocked request at each priority level. | |
146 // It is safe to store a raw pointer in these lists because the | |
147 // manager will be notified by OnThrottleDestroyed before a ThrottleImpl | |
148 // is destroyed, and will remove the pointer from the appropriate list | |
149 // at that point. | |
150 std::list<ThrottleImpl*> blocked_requests_[NUM_PRIORITIES]; | |
151 std::list<ThrottleImpl*> unblocked_requests_[NUM_PRIORITIES]; | |
152 | |
153 // For testing. | |
154 std::unique_ptr<base::TickClock> tick_clock_; | |
155 | |
156 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManagerImpl); | |
157 }; | |
158 | |
159 } // namespace net | |
160 | |
161 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_IMPL_H_ | |
OLD | NEW |