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/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 kMedianEstimatorLearningParameter; | |
33 static const int kInitialMedianInMS; | |
34 static const int kMedianLifetimeMultiple; | |
35 | |
36 class ThrottleImpl : public NetworkThrottleManager::Throttle { | |
37 public: | |
38 using ThrottleList = std::list<ThrottleImpl*>; | |
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 | |
Charlie Harrison
2016/07/07 20:17:26
nit: Throttle:
Randy Smith (Not in Mondays)
2016/07/08 20:54:24
Done.
| |
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::Time 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::Time 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 | |
Charlie Harrison
2016/07/07 20:17:26
nit: NetworkThrottleManager:
Randy Smith (Not in Mondays)
2016/07/08 20:54:24
Done.
| |
88 std::unique_ptr<Throttle> CreateThrottle(ThrottleDelegate* delegate, | |
89 RequestPriority priority, | |
90 bool ignore_limits) override; | |
91 | |
92 void SetClockForTesting(std::unique_ptr<base::Clock> clock); | |
Charlie Harrison
2016/07/07 20:17:26
Can this be protected?
Randy Smith (Not in Mondays)
2016/07/08 20:54:24
So I think of there as being a couple of ways to d
Charlie Harrison
2016/07/08 22:36:05
Actually, I think I prefer ForTesting too.
| |
93 | |
94 private: | |
95 static bool CompareThrottlesForCreationTime(ThrottleImpl* throttle1, | |
Charlie Harrison
2016/07/07 20:17:26
Can you document this method and its relation to t
Randy Smith (Not in Mondays)
2016/07/08 20:54:24
Good point, done.
| |
96 ThrottleImpl* throttle2); | |
97 | |
98 using CreationOrderingSet = | |
99 std::set<ThrottleImpl*, bool (*)(ThrottleImpl*, ThrottleImpl*)>; | |
100 | |
101 void OnThrottlePriorityChanged(ThrottleImpl* throttle, | |
102 RequestPriority old_priority, | |
103 RequestPriority new_priority); | |
104 void OnThrottleDestroyed(ThrottleImpl* throttle); | |
105 | |
106 // Recompute how many requests "count" as outstanding (i.e. | |
107 // are not older than kMedianLifetimeMultiple * MedianThrottleLifetime()). | |
108 void RecomputeOutstanding(); | |
109 | |
110 // Check to see if any currently blocked throttles should be unblocked, | |
111 // and unblock them if so. Note that unblocking may result in | |
112 // re-entrant calls to this class, so no assumptions about state persistence | |
113 // should be made across this call. | |
114 void MaybeUnblock(); | |
115 | |
116 std::list<ThrottleImpl*>* ListForThrottle(bool blocked, | |
Charlie Harrison
2016/07/07 20:17:26
Can you document these too?
Randy Smith (Not in Mondays)
2016/07/08 20:54:24
Done.
| |
117 RequestPriority priority); | |
118 std::list<ThrottleImpl*>* ListForThrottle(ThrottleImpl* throttle); | |
119 | |
120 base::TimeDelta lifetime_median_estimate_; | |
121 CreationOrderingSet creation_ordering_set_; | |
122 | |
123 size_t num_throttles_blocked_; | |
124 | |
125 // Count of throttles that are considered as outstanding. Throttles that | |
126 // are too old or blocked are not counted. | |
127 size_t num_effective_outstanding_; | |
128 | |
129 // Timer controlling outstanding request recomputation. | |
130 base::Timer outstanding_recomputation_timer_; | |
131 | |
132 // Lists of blocked/unblocked request at each priority level. | |
133 // It is safe to store a raw pointer in these lists because the | |
134 // manager will be notified by OnThrottleDestroyed before a ThrottleImpl | |
135 // is destroyed, and will remove the pointer from the appropriate list | |
136 // at that point. | |
137 std::list<ThrottleImpl*> blocked_requests_[NUM_PRIORITIES]; | |
138 std::list<ThrottleImpl*> unblocked_requests_[NUM_PRIORITIES]; | |
139 | |
140 // For testing. | |
141 std::unique_ptr<base::Clock> clock_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(NetworkThrottleManagerImpl); | |
144 }; | |
145 | |
146 } // namespace net | |
147 | |
148 #endif // NET_BASE_NETWORK_THROTTLE_MANAGER_IMPL_H_ | |
OLD | NEW |