Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grid: Rename alternate_protocols_cache.{h,cc} to alternate_protocols_cache_impl.{h,cc} #16424

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions include/envoy/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ licenses(["notice"]) # Apache 2

envoy_package()

envoy_cc_library(
name = "alternate_protocols_cache_interface",
hdrs = ["alternate_protocols_cache.h"],
deps = ["//include/envoy/common:time_interface"],
)

envoy_cc_library(
name = "api_listener_interface",
hdrs = ["api_listener.h"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
namespace Envoy {
namespace Http {

// Tracks alternate protocols that can be used to make an HTTP connection to an origin server.
// See https://tools.ietf.org/html/rfc7838 for HTTP Alternate Services and
// https://datatracker.ietf.org/doc/html/draft-ietf-dnsop-svcb-https-04 for the
// "HTTPS" DNS resource record.
/**
* Tracks alternate protocols that can be used to make an HTTP connection to an origin server.
* See https://tools.ietf.org/html/rfc7838 for HTTP Alternate Services and
* https://datatracker.ietf.org/doc/html/draft-ietf-dnsop-svcb-https-04 for the
* "HTTPS" DNS resource record.
*/
class AlternateProtocolsCache {
public:
// Represents an HTTP origin to be connected too.
/**
* Represents an HTTP origin to be connected too.
*/
struct Origin {
public:
Origin(absl::string_view scheme, absl::string_view hostname, uint32_t port);
Expand Down Expand Up @@ -56,7 +60,9 @@ class AlternateProtocolsCache {
uint32_t port_{};
};

// Represents an alternative protocol that can be used to connect to an origin.
/**
* Represents an alternative protocol that can be used to connect to an origin.
*/
struct AlternateProtocol {
public:
AlternateProtocol(absl::string_view alpn, absl::string_view hostname, uint32_t port);
Expand All @@ -73,34 +79,34 @@ class AlternateProtocolsCache {
uint32_t port_;
};

explicit AlternateProtocolsCache(TimeSource& time_source);

// Sets the possible alternative protocols which can be used to connect to the
// specified origin. Expires after the specified expiration time.
void setAlternatives(const Origin& origin, const std::vector<AlternateProtocol>& protocols,
const MonotonicTime& expiration);

// Returns the possible alternative protocols which can be used to connect to the
// specified origin, or nullptr if not alternatives are found. The returned pointer
// is owned by the AlternateProtocolsCache and is valid until the next operation on
// AlternateProtocolsCache.
OptRef<const std::vector<AlternateProtocol>> findAlternatives(const Origin& origin);

// Returns the number of entries in the map.
size_t size() const;

private:
struct Entry {
std::vector<AlternateProtocol> protocols_;
MonotonicTime expiration_;
};

// Time source used to check expiration of entries.
TimeSource& time_source_;

// Map from hostname to list of alternate protocols.
// TODO(RyanTheOptimist): Add a limit to the size of this map and evict based on usage.
std::map<Origin, Entry> protocols_;
virtual ~AlternateProtocolsCache() = default;

/**
* Sets the possible alternative protocols which can be used to connect to the
* specified origin. Expires after the specified expiration time.
* @param origin The origin to set alternate protocols for.
* @param protocols A list of alternate protocols.
* @param expiration The time after which the alternatives are no longer valid.
*/
virtual void setAlternatives(const Origin& origin,
const std::vector<AlternateProtocol>& protocols,
const MonotonicTime& expiration) PURE;

/**
* Returns the possible alternative protocols which can be used to connect to the
* specified origin, or nullptr if not alternatives are found. The returned pointer
* is owned by the AlternateProtocolsCacheImpl and is valid until the next operation on
* AlternateProtocolsCacheImpl.
* @param origin The origin to find alternate protocols for.
* @return An optional list of alternate protocols for the given origin.
*/
virtual OptRef<const std::vector<AlternateProtocol>> findAlternatives(const Origin& origin) PURE;

/**
* Returns the number of entries in the map.
* @return the number if entries in the map.
*/
virtual size_t size() const PURE;
};

} // namespace Http
Expand Down
5 changes: 3 additions & 2 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,11 @@ envoy_cc_library(

envoy_cc_library(
name = "alternate_protocols_cache",
srcs = ["alternate_protocols_cache.cc"],
hdrs = ["alternate_protocols_cache.h"],
srcs = ["alternate_protocols_cache_impl.cc"],
hdrs = ["alternate_protocols_cache_impl.h"],
deps = [
"//include/envoy/common:time_interface",
"//include/envoy/http:alternate_protocols_cache_interface",
],
)

Expand Down
51 changes: 0 additions & 51 deletions source/common/http/alternate_protocols_cache.cc

This file was deleted.

53 changes: 53 additions & 0 deletions source/common/http/alternate_protocols_cache_impl.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include "common/http/alternate_protocols_cache_impl.h"

namespace Envoy {
namespace Http {

AlternateProtocolsCacheImpl::AlternateProtocol::AlternateProtocol(absl::string_view alpn,
absl::string_view hostname,
uint32_t port)
: alpn_(alpn), hostname_(hostname), port_(port) {}

AlternateProtocolsCacheImpl::Origin::Origin(absl::string_view scheme, absl::string_view hostname,
uint32_t port)
: scheme_(scheme), hostname_(hostname), port_(port) {}

AlternateProtocolsCacheImpl::AlternateProtocolsCacheImpl(TimeSource& time_source)
: time_source_(time_source) {}

AlternateProtocolsCacheImpl::~AlternateProtocolsCacheImpl() = default;

void AlternateProtocolsCacheImpl::setAlternatives(const Origin& origin,
const std::vector<AlternateProtocol>& protocols,
const MonotonicTime& expiration) {
Entry& entry = protocols_[origin];
if (entry.protocols_ != protocols) {
entry.protocols_ = protocols;
}
if (entry.expiration_ != expiration) {
entry.expiration_ = expiration;
}
}

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

const Entry& entry = entry_it->second;
if (time_source_.monotonicTime() > entry.expiration_) {
// Expire the entry.
protocols_.erase(entry_it);
return makeOptRefFromPtr<const std::vector<AlternateProtocolsCacheImpl::AlternateProtocol>>(
nullptr);
}
return makeOptRef(entry.protocols_);
}

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

} // namespace Http
} // namespace Envoy
44 changes: 44 additions & 0 deletions source/common/http/alternate_protocols_cache_impl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

#include <map>
#include <string>
#include <tuple>
#include <vector>

#include "envoy/common/optref.h"
#include "envoy/common/time.h"
#include "envoy/http/alternate_protocols_cache.h"

#include "absl/strings/string_view.h"

namespace Envoy {
namespace Http {

// An implementation of AlternateProtocolsCache.
class AlternateProtocolsCacheImpl : public AlternateProtocolsCache {
public:
explicit AlternateProtocolsCacheImpl(TimeSource& time_source);
~AlternateProtocolsCacheImpl() override;

// AlternateProtocolsCache
void setAlternatives(const Origin& origin, const std::vector<AlternateProtocol>& protocols,
const MonotonicTime& expiration) override;
OptRef<const std::vector<AlternateProtocol>> findAlternatives(const Origin& origin) override;
size_t size() const override;

private:
struct Entry {
std::vector<AlternateProtocol> protocols_;
MonotonicTime expiration_;
};

// Time source used to check expiration of entries.
TimeSource& time_source_;

// Map from hostname to list of alternate protocols.
// TODO(RyanTheOptimist): Add a limit to the size of this map and evict based on usage.
std::map<Origin, Entry> protocols_;
};

} // namespace Http
} // namespace Envoy
8 changes: 4 additions & 4 deletions source/common/http/conn_pool_grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ ConnectivityGrid::ConnectivityGrid(
const Network::ConnectionSocket::OptionsSharedPtr& options,
const Network::TransportSocketOptionsSharedPtr& transport_socket_options,
Upstream::ClusterConnectivityState& state, TimeSource& time_source,
OptRef<AlternateProtocolsCache> alternate_protocols,
OptRef<AlternateProtocolsCacheImpl> alternate_protocols,
std::chrono::milliseconds next_attempt_duration, ConnectivityOptions connectivity_options)
: dispatcher_(dispatcher), random_generator_(random_generator), host_(host),
priority_(priority), options_(options), transport_socket_options_(transport_socket_options),
Expand Down Expand Up @@ -351,16 +351,16 @@ bool ConnectivityGrid::shouldAttemptHttp3() {
}
uint32_t port = host_->address()->ip()->port();
// TODO(RyanTheOptimist): Figure out how scheme gets plumbed in here.
AlternateProtocolsCache::Origin origin("https", host_->hostname(), port);
OptRef<const std::vector<AlternateProtocolsCache::AlternateProtocol>> protocols =
AlternateProtocolsCacheImpl::Origin origin("https", host_->hostname(), port);
OptRef<const std::vector<AlternateProtocolsCacheImpl::AlternateProtocol>> protocols =
alternate_protocols_->findAlternatives(origin);
if (!protocols.has_value()) {
ENVOY_LOG(trace, "No alternate protocols available for host '{}', skipping HTTP/3.",
host_->hostname());
return false;
}

for (const AlternateProtocolsCache::AlternateProtocol& protocol : protocols.ref()) {
for (const AlternateProtocolsCacheImpl::AlternateProtocol& protocol : protocols.ref()) {
// TODO(RyanTheOptimist): Handle alternate protocols which change hostname or port.
if (!protocol.hostname_.empty() || protocol.port_ != port) {
ENVOY_LOG(trace,
Expand Down
6 changes: 3 additions & 3 deletions source/common/http/conn_pool_grid.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include "common/http/alternate_protocols_cache.h"
#include "common/http/alternate_protocols_cache_impl.h"
#include "common/http/conn_pool_base.h"
#include "common/http/http3_status_tracker.h"

Expand Down Expand Up @@ -132,7 +132,7 @@ class ConnectivityGrid : public ConnectionPool::Instance,
const Network::ConnectionSocket::OptionsSharedPtr& options,
const Network::TransportSocketOptionsSharedPtr& transport_socket_options,
Upstream::ClusterConnectivityState& state, TimeSource& time_source,
OptRef<AlternateProtocolsCache> alternate_protocols,
OptRef<AlternateProtocolsCacheImpl> alternate_protocols,
std::chrono::milliseconds next_attempt_duration,
ConnectivityOptions connectivity_options);
~ConnectivityGrid() override;
Expand Down Expand Up @@ -192,7 +192,7 @@ class ConnectivityGrid : public ConnectionPool::Instance,
TimeSource& time_source_;
Http3StatusTracker http3_status_tracker_;
// TODO(RyanTheOptimist): Make the alternate_protocols_ member non-optional.
OptRef<AlternateProtocolsCache> alternate_protocols_;
OptRef<AlternateProtocolsCacheImpl> alternate_protocols_;

// Tracks how many drains are needed before calling drain callbacks. This is
// set to the number of pools when the first drain callbacks are added, and
Expand Down
2 changes: 1 addition & 1 deletion source/common/upstream/cluster_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1523,7 +1523,7 @@ Http::ConnectionPool::InstancePtr ProdClusterManagerFactory::allocateConnPool(
{Http::Protocol::Http11, Http::Protocol::Http2, Http::Protocol::Http3}));
#ifdef ENVOY_ENABLE_QUIC
// TODO(RyanTheOptimist): Plumb an actual alternate protocols cache.
auto alternate_protocols = makeOptRefFromPtr<Http::AlternateProtocolsCache>(nullptr);
auto alternate_protocols = makeOptRefFromPtr<Http::AlternateProtocolsCacheImpl>(nullptr);
Envoy::Http::ConnectivityGrid::ConnectivityOptions coptions{protocols};
return std::make_unique<Http::ConnectivityGrid>(
dispatcher, api_.randomGenerator(), host, priority, options, transport_socket_options,
Expand Down
4 changes: 2 additions & 2 deletions test/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -451,8 +451,8 @@ envoy_cc_test(
)

envoy_cc_test(
name = "alternate_protocols_cache_test",
srcs = ["alternate_protocols_cache_test.cc"],
name = "alternate_protocols_cache_impl_test",
srcs = ["alternate_protocols_cache_impl_test.cc"],
deps = [
":common_lib",
"//source/common/http:alternate_protocols_cache",
Expand Down
Loading