OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/spdy/http2_priority_dependencies.h" | 5 #include "net/spdy/http2_priority_dependencies.h" |
6 | 6 |
7 namespace net { | 7 namespace net { |
8 | 8 |
9 Http2PriorityDependencies::Http2PriorityDependencies() {} | 9 Http2PriorityDependencies::Http2PriorityDependencies() {} |
10 | 10 |
11 Http2PriorityDependencies::~Http2PriorityDependencies() {} | 11 Http2PriorityDependencies::~Http2PriorityDependencies() {} |
12 | 12 |
13 void Http2PriorityDependencies::OnStreamSynSent( | 13 void Http2PriorityDependencies::OnStreamCreation( |
14 SpdyStreamId id, | 14 SpdyStreamId id, |
15 SpdyPriority priority, | 15 SpdyPriority priority, |
16 SpdyStreamId* dependent_stream_id, | 16 SpdyStreamId* dependent_stream_id, |
17 bool* exclusive) { | 17 bool* exclusive) { |
18 DCHECK(entry_by_stream_id_.find(id) == entry_by_stream_id_.end()); | 18 DCHECK(entry_by_stream_id_.find(id) == entry_by_stream_id_.end()); |
19 | 19 |
20 *dependent_stream_id = 0ul; | 20 *dependent_stream_id = 0ul; |
21 *exclusive = true; | 21 *exclusive = true; |
22 | 22 |
23 // Find the next highest entry in total order. | 23 // Dependent on the lowest-priority stream that has a priority >= |priority|. |
24 for (int i = priority; i >= kV3HighestPriority; --i) { | 24 IdList::iterator parent; |
25 if (!id_priority_lists_[i].empty()) { | 25 if (PriorityLowerBound(priority, &parent)) { |
26 *dependent_stream_id = id_priority_lists_[i].back().first; | 26 *dependent_stream_id = parent->first; |
27 break; | |
28 } | |
29 } | 27 } |
30 | 28 |
31 id_priority_lists_[priority].push_back(std::make_pair(id, priority)); | 29 id_priority_lists_[priority].push_back(std::make_pair(id, priority)); |
32 IdList::iterator it = id_priority_lists_[priority].end(); | 30 IdList::iterator it = id_priority_lists_[priority].end(); |
33 --it; | 31 --it; |
34 entry_by_stream_id_[id] = it; | 32 entry_by_stream_id_[id] = it; |
35 } | 33 } |
36 | 34 |
35 bool Http2PriorityDependencies::PriorityLowerBound(SpdyPriority priority, | |
36 IdList::iterator* bound) { | |
37 for (int i = priority; i >= kV3HighestPriority; --i) { | |
38 if (!id_priority_lists_[i].empty()) { | |
39 *bound = id_priority_lists_[i].end(); | |
40 --(*bound); | |
41 return true; | |
42 } | |
43 } | |
44 return false; | |
45 } | |
46 | |
47 bool Http2PriorityDependencies::ParentOfStream(SpdyStreamId id, | |
48 IdList::iterator* parent) { | |
49 EntryMap::iterator entry = entry_by_stream_id_.find(id); | |
50 DCHECK(entry != entry_by_stream_id_.end()); | |
51 | |
52 SpdyPriority priority = entry->second->second; | |
53 IdList::iterator curr = entry->second; | |
54 if (curr != id_priority_lists_[priority].begin()) { | |
55 *parent = curr; | |
56 --(*parent); | |
57 return true; | |
58 } | |
59 | |
60 // id is at the head of its priority list, so its parent is the last | |
Bence
2017/01/05 17:14:45
s/id/|id|/, here and in other comments below. Opt
Tom Bergan
2017/01/06 00:08:40
Done.
| |
61 // entry of the next-highest priority band. | |
62 if (priority == kV3HighestPriority) { | |
63 return false; | |
64 } | |
65 return PriorityLowerBound(priority - 1, parent); | |
66 } | |
67 | |
68 bool Http2PriorityDependencies::ChildOfStream(SpdyStreamId id, | |
69 IdList::iterator* child) { | |
70 EntryMap::iterator entry = entry_by_stream_id_.find(id); | |
71 DCHECK(entry != entry_by_stream_id_.end()); | |
72 | |
73 SpdyPriority priority = entry->second->second; | |
74 *child = entry->second; | |
75 ++(*child); | |
76 if (*child != id_priority_lists_[priority].end()) { | |
77 return true; | |
78 } | |
79 | |
80 // id is at the end of its priority list, so its child is the stream | |
81 // at the front of the next-lowest priority band. | |
82 for (int i = priority + 1; i <= kV3LowestPriority; ++i) { | |
83 if (!id_priority_lists_[i].empty()) { | |
84 *child = id_priority_lists_[i].begin(); | |
85 return true; | |
86 } | |
87 } | |
88 | |
89 return false; | |
90 } | |
91 | |
92 std::vector<Http2PriorityDependencies::DependencyUpdate> | |
93 Http2PriorityDependencies::OnStreamUpdate(SpdyStreamId id, | |
94 SpdyPriority new_priority) { | |
95 std::vector<DependencyUpdate> result; | |
96 result.reserve(2); | |
97 | |
98 EntryMap::iterator curr_entry = entry_by_stream_id_.find(id); | |
99 SpdyPriority old_priority = curr_entry->second->second; | |
100 if (old_priority == new_priority) { | |
101 return result; | |
102 } | |
103 | |
104 IdList::iterator old_parent; | |
105 bool old_has_parent = ParentOfStream(id, &old_parent); | |
106 | |
107 IdList::iterator new_parent; | |
108 bool new_has_parent = PriorityLowerBound(new_priority, &new_parent); | |
109 | |
110 // If we move id from MEDIUM to LOW, where HIGH = {other_id}, MEDIUM = {id}, | |
111 // and LOW = {}, then PriorityLowerBound(LOW) is id. In this corner case, id | |
112 // does not change parents. | |
113 if (new_has_parent && new_parent->first == id) { | |
114 new_has_parent = old_has_parent; | |
115 new_parent = old_parent; | |
116 } | |
117 | |
118 // If the parent has changed, we generate dependency updates. | |
119 if ((old_has_parent != new_has_parent) || | |
120 (old_has_parent && old_parent->first != new_parent->first)) { | |
121 // If id has a child, then that child moves to be dependent on old_parent. | |
122 IdList::iterator old_child; | |
123 if (ChildOfStream(id, &old_child)) { | |
124 if (old_has_parent) { | |
125 result.push_back({old_child->first, old_parent->first, true}); | |
126 } else { | |
127 result.push_back({old_child->first, 0, true}); | |
128 } | |
129 } | |
130 | |
131 // id moves to be dependent on new_parent | |
132 if (new_has_parent) { | |
133 result.push_back({id, new_parent->first, true}); | |
134 } else { | |
135 result.push_back({id, 0, true}); | |
136 } | |
137 } | |
138 | |
139 // Move to the new priority. | |
140 EntryMap::iterator old = entry_by_stream_id_.find(id); | |
141 id_priority_lists_[old->second->second].erase(old->second); | |
142 id_priority_lists_[new_priority].push_back(std::make_pair(id, new_priority)); | |
143 IdList::iterator it = id_priority_lists_[new_priority].end(); | |
144 --it; | |
145 entry_by_stream_id_[id] = it; | |
146 | |
147 return result; | |
148 } | |
149 | |
37 void Http2PriorityDependencies::OnStreamDestruction(SpdyStreamId id) { | 150 void Http2PriorityDependencies::OnStreamDestruction(SpdyStreamId id) { |
38 EntryMap::iterator emit = entry_by_stream_id_.find(id); | 151 EntryMap::iterator emit = entry_by_stream_id_.find(id); |
39 | 152 DCHECK(emit != entry_by_stream_id_.end()); |
40 // This routine may be called without a matching call to | |
41 // OnStreamSynSent above, in the case of server push. In that case, | |
42 // it's a no-op. | |
43 if (emit == entry_by_stream_id_.end()) | |
44 return; | |
45 | 153 |
46 IdList::iterator it = emit->second; | 154 IdList::iterator it = emit->second; |
47 id_priority_lists_[it->second].erase(it); | 155 id_priority_lists_[it->second].erase(it); |
48 entry_by_stream_id_.erase(emit); | 156 entry_by_stream_id_.erase(emit); |
49 } | 157 } |
50 | 158 |
51 } // namespace net | 159 } // namespace net |
OLD | NEW |