feat(discovery): implement P3 S-DISCOVERY-01..04 Discovery catalog and optional DiscoveryRpc adapter
This commit is contained in:
1 parent
0cf187d664
commit
ec8c0bd40a
12 files changed
+2686
No files matched your search
@@ -0,0 +1,144 @@
|
||||
#include <Void/DiscoveryRpc/EndpointAdapter.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace Void::DiscoveryRpc
|
||||
{
|
||||
namespace
|
||||
{
|
||||
struct OutstandingGuard {
|
||||
std::atomic<std::size_t>& counter;
|
||||
~OutstandingGuard() noexcept
|
||||
{
|
||||
counter.fetch_sub(1, std::memory_order_acq_rel);
|
||||
}
|
||||
};
|
||||
} // namespace
|
||||
|
||||
EndpointResolverClient::EndpointResolverClient(
|
||||
const Void::Discovery::Catalog& catalog,
|
||||
EndpointResolverOptions options)
|
||||
: m_catalog(&catalog), m_options(std::move(options))
|
||||
{
|
||||
if (m_options.clock == nullptr) {
|
||||
m_options.clock = &Void::Time::SystemMonotonicClock::instance();
|
||||
}
|
||||
if (m_options.require_tls) {
|
||||
m_options.http_options.verify_tls = true;
|
||||
}
|
||||
}
|
||||
|
||||
Void::Discovery::Result<SelectedEndpoint>
|
||||
EndpointResolverClient::select_endpoint(
|
||||
std::optional<std::string_view> affinity_key)
|
||||
{
|
||||
if (m_catalog == nullptr) {
|
||||
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||
Void::Discovery::DiscoveryErrorCode::InvalidArgument,
|
||||
"EndpointResolverClient: null catalog"));
|
||||
}
|
||||
|
||||
auto snap =
|
||||
m_catalog->resolve(m_options.service, m_options.resolve_policy);
|
||||
if (!snap.has_value()) {
|
||||
return Void::Result::fail(snap.error());
|
||||
}
|
||||
|
||||
auto selected =
|
||||
affinity_key.has_value()
|
||||
? Void::Discovery::AffinitySelector::select(*snap, *affinity_key)
|
||||
: m_rr.select(*snap);
|
||||
if (!selected.has_value()) {
|
||||
return Void::Result::fail(selected.error());
|
||||
}
|
||||
|
||||
if (m_options.require_tls && selected->endpoint.scheme != "https") {
|
||||
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||
Void::Discovery::DiscoveryErrorCode::TlsIdentityMismatch,
|
||||
"EndpointResolverClient: refusing non-TLS endpoint when "
|
||||
"require_tls is enabled"));
|
||||
}
|
||||
|
||||
const std::string effective_tls{
|
||||
selected->endpoint.effective_tls_name()};
|
||||
if (!m_options.expected_tls_name.empty() &&
|
||||
effective_tls != m_options.expected_tls_name) {
|
||||
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||
Void::Discovery::DiscoveryErrorCode::TlsIdentityMismatch,
|
||||
"EndpointResolverClient: endpoint TLS identity mismatch "
|
||||
"(expected '" +
|
||||
m_options.expected_tls_name + "', got '" + effective_tls +
|
||||
"')"));
|
||||
}
|
||||
|
||||
SelectedEndpoint out{};
|
||||
out.origin_url = selected->endpoint.to_origin_url();
|
||||
out.effective_tls_name = effective_tls;
|
||||
out.record = std::move(*selected);
|
||||
return out;
|
||||
}
|
||||
|
||||
Void::Result::Result<Void::RPC::OwnedBytes>
|
||||
EndpointResolverClient::call(
|
||||
Void::RPC::MethodId method, Void::RPC::OwnedBytes payload,
|
||||
Void::RPC::CallOptions options,
|
||||
std::optional<std::string_view> affinity_key)
|
||||
{
|
||||
// Enforce bounded outstanding operations.
|
||||
std::size_t cur = m_outstanding.load(std::memory_order_acquire);
|
||||
for (;;) {
|
||||
if (cur >= m_options.max_outstanding_ops) {
|
||||
return Void::Result::fail(Void::Discovery::make_discovery_error(
|
||||
Void::Discovery::DiscoveryErrorCode::LimitExceeded,
|
||||
"EndpointResolverClient: max_outstanding_ops reached"));
|
||||
}
|
||||
if (m_outstanding.compare_exchange_weak(
|
||||
cur, cur + 1, std::memory_order_acq_rel,
|
||||
std::memory_order_acquire)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
OutstandingGuard guard{m_outstanding};
|
||||
|
||||
auto selected = select_endpoint(affinity_key);
|
||||
if (!selected.has_value()) {
|
||||
return Void::Result::fail(selected.error());
|
||||
}
|
||||
|
||||
Void::RPC::ClientOptions copts{};
|
||||
copts.endpoint_base_url = selected->origin_url;
|
||||
copts.http_options = m_options.http_options;
|
||||
if (m_options.require_tls) {
|
||||
copts.http_options.verify_tls = true;
|
||||
}
|
||||
copts.limits = m_options.rpc_limits;
|
||||
copts.clock = m_options.clock;
|
||||
|
||||
if (options.timeout <= std::chrono::milliseconds{0} ||
|
||||
options.timeout > m_options.total_deadline) {
|
||||
options.timeout = m_options.total_deadline;
|
||||
}
|
||||
// Retain peer identity header when address refreshes to an IP while
|
||||
// expected_tls_name preserves the logical authority.
|
||||
if (!selected->record.endpoint.expected_tls_name.empty()) {
|
||||
bool has_authority = false;
|
||||
for (const auto& m : options.metadata) {
|
||||
if (m.key == "void-expected-authority") {
|
||||
has_authority = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!has_authority) {
|
||||
options.metadata.push_back(Void::RPC::MetadataEntry{
|
||||
.key = "void-expected-authority",
|
||||
.value = selected->effective_tls_name,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Void::RPC::Client client{std::move(copts)};
|
||||
return client.call(method, std::move(payload), options);
|
||||
}
|
||||
|
||||
} // namespace Void::DiscoveryRpc
|
||||
Reference in new issue
Block a user