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 |
| 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(new_priority) is |id|. In this corner |
| 112 // case, |id| 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 |
| 122 // |old_parent|. |
| 123 IdList::iterator old_child; |
| 124 if (ChildOfStream(id, &old_child)) { |
| 125 if (old_has_parent) { |
| 126 result.push_back({old_child->first, old_parent->first, true}); |
| 127 } else { |
| 128 result.push_back({old_child->first, 0, true}); |
| 129 } |
| 130 } |
| 131 |
| 132 // |id| moves to be dependent on |new_parent|. |
| 133 if (new_has_parent) { |
| 134 result.push_back({id, new_parent->first, true}); |
| 135 } else { |
| 136 result.push_back({id, 0, true}); |
| 137 } |
| 138 } |
| 139 |
| 140 // Move to the new priority. |
| 141 EntryMap::iterator old = entry_by_stream_id_.find(id); |
| 142 id_priority_lists_[old->second->second].erase(old->second); |
| 143 id_priority_lists_[new_priority].push_back(std::make_pair(id, new_priority)); |
| 144 IdList::iterator it = id_priority_lists_[new_priority].end(); |
| 145 --it; |
| 146 entry_by_stream_id_[id] = it; |
| 147 |
| 148 return result; |
| 149 } |
| 150 |
37 void Http2PriorityDependencies::OnStreamDestruction(SpdyStreamId id) { | 151 void Http2PriorityDependencies::OnStreamDestruction(SpdyStreamId id) { |
38 EntryMap::iterator emit = entry_by_stream_id_.find(id); | 152 EntryMap::iterator emit = entry_by_stream_id_.find(id); |
39 | 153 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 | 154 |
46 IdList::iterator it = emit->second; | 155 IdList::iterator it = emit->second; |
47 id_priority_lists_[it->second].erase(it); | 156 id_priority_lists_[it->second].erase(it); |
48 entry_by_stream_id_.erase(emit); | 157 entry_by_stream_id_.erase(emit); |
49 } | 158 } |
50 | 159 |
51 } // namespace net | 160 } // namespace net |
OLD | NEW |