Skip to content

Commit

Permalink
linked_hash_map
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Hamilton <rch@google.com>
  • Loading branch information
RyanTheOptimist committed Sep 27, 2021
1 parent 6ae877a commit becd093
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
1 change: 1 addition & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ envoy_cc_library(
"alternate_protocols_cache_impl.cc",
"alternate_protocols_cache_manager_impl.cc",
],
external_deps = ["quiche_quic_platform"],
hdrs = [
"alternate_protocols_cache_impl.h",
"alternate_protocols_cache_manager_impl.h",
Expand Down
29 changes: 10 additions & 19 deletions source/common/http/alternate_protocols_cache_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,12 @@ void AlternateProtocolsCacheImpl::setAlternatives(const Origin& origin,
ENVOY_LOG_MISC(trace, "Too many alternate protocols: {}, truncating", protocols.size());
protocols.erase(protocols.begin() + max_protocols, protocols.end());
}
while (protocols_list_.size() >= max_entries_) {
auto iter = protocols_list_.rbegin();
key_value_store_->remove(originToString(iter->origin_));
protocols_map_.erase(protocols_map_.find(iter->origin_));
protocols_list_.erase((++iter).base());
while (protocols_.size() >= max_entries_) {
auto iter = protocols_.begin();
key_value_store_->remove(originToString(iter->first));
protocols_.erase(iter);
}
auto iter = protocols_map_.find(origin);
if (iter != protocols_map_.end()) {
protocols_list_.erase(iter->second);
protocols_map_.erase(iter);
}
std::pair<Origin, std::vector<AlternateProtocol>> value = {origin, protocols};
protocols_list_.emplace_front(origin, protocols);
protocols_map_[origin] = protocols_list_.begin();
protocols_[origin] = protocols;
if (key_value_store_) {
key_value_store_->addOrUpdate(originToString(origin),
protocolsToStringForCache(protocols, time_source_));
Expand All @@ -99,12 +91,12 @@ void AlternateProtocolsCacheImpl::setAlternatives(const Origin& origin,

OptRef<const std::vector<AlternateProtocolsCache::AlternateProtocol>>
AlternateProtocolsCacheImpl::findAlternatives(const Origin& origin) {
auto entry_it = protocols_map_.find(origin);
if (entry_it == protocols_map_.end()) {
auto entry_it = protocols_.find(origin);
if (entry_it == protocols_.end()) {
return makeOptRefFromPtr<const std::vector<AlternateProtocol>>(nullptr);
}

std::vector<AlternateProtocol>& protocols = entry_it->second->protocols_;
std::vector<AlternateProtocol>& protocols = entry_it->second;

auto original_size = protocols.size();
const MonotonicTime now = time_source_.monotonicTime();
Expand All @@ -115,8 +107,7 @@ AlternateProtocolsCacheImpl::findAlternatives(const Origin& origin) {
protocols.end());

if (protocols.empty()) {
protocols_list_.erase(entry_it->second);
protocols_map_.erase(entry_it);
protocols_.erase(entry_it);
if (key_value_store_) {
key_value_store_->remove(originToString(origin));
}
Expand All @@ -130,7 +121,7 @@ AlternateProtocolsCacheImpl::findAlternatives(const Origin& origin) {
return makeOptRef(const_cast<const std::vector<AlternateProtocol>&>(protocols));
}

size_t AlternateProtocolsCacheImpl::size() const { return protocols_list_.size(); }
size_t AlternateProtocolsCacheImpl::size() const { return protocols_.size(); }

} // namespace Http
} // namespace Envoy
21 changes: 11 additions & 10 deletions source/common/http/alternate_protocols_cache_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "envoy/http/alternate_protocols_cache.h"

#include "absl/strings/string_view.h"
#include "quiche/common/quiche_linked_hash_map.h"

namespace Envoy {
namespace Http {
Expand Down Expand Up @@ -49,17 +50,17 @@ class AlternateProtocolsCacheImpl : public AlternateProtocolsCache {
// Time source used to check expiration of entries.
TimeSource& time_source_;

struct OriginProtocols {
OriginProtocols(const Origin& origin, const std::vector<AlternateProtocol>& protocols)
: origin_(origin), protocols_(protocols) {}

Origin origin_;
std::vector<AlternateProtocol> protocols_;
struct OriginHash {
size_t operator()(const Origin& origin) const {
size_t hash = std::hash<std::string>()(origin.scheme_) +
37 * (std::hash<std::string>()(origin.hostname_) +
37 * std::hash<uint32_t>()(origin.port_));
return hash;
}
};
// List of origin, alternate protocol pairs, in insertion order.
std::list<OriginProtocols> protocols_list_;
// Map from hostname to iterator into protocols_list_.
std::map<Origin, std::list<OriginProtocols>::iterator> protocols_map_;

// Map from origin to list of alternate protocols.
quiche::QuicheLinkedHashMap<Origin, std::vector<AlternateProtocol>, OriginHash> protocols_;

// The key value store, if flushing to persistent storage.
std::unique_ptr<KeyValueStore> key_value_store_;
Expand Down

0 comments on commit becd093

Please sign in to comment.