Skip to content

Commit

Permalink
tcp_proxy_protocol: reduce stats contention by moving the stats to fa…
Browse files Browse the repository at this point in the history
…ctory (envoyproxy#36534)

Commit Message: reduce data plane stats contention by moving the stats
to the factory
Additional Description:
Risk Level: low
Testing:
Docs Changes:
Release Notes:
Platform Specific Features:
[Optional Runtime guard:]
[Optional Fixes #Issue]
[Optional Fixes commit #PR or SHA]
[Optional Deprecated:]
[Optional [API
Considerations](https://github.com/envoyproxy/envoy/blob/main/api/review_checklist.md):]

---------

Signed-off-by: Boteng Yao <[email protected]>
  • Loading branch information
botengyao authored Oct 15, 2024
1 parent 440708f commit 5b0d56d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,12 @@ namespace Extensions {
namespace TransportSockets {
namespace ProxyProtocol {

UpstreamProxyProtocolStats generateUpstreamProxyProtocolStats(Stats::Scope& stats_scope) {
const char prefix[]{"upstream.proxyprotocol."};
return {ALL_PROXY_PROTOCOL_TRANSPORT_SOCKET_STATS(POOL_COUNTER_PREFIX(stats_scope, prefix))};
}

UpstreamProxyProtocolSocket::UpstreamProxyProtocolSocket(
Network::TransportSocketPtr&& transport_socket,
Network::TransportSocketOptionsConstSharedPtr options, ProxyProtocolConfig config,
Stats::Scope& scope)
const UpstreamProxyProtocolStats& stats)
: PassthroughSocket(std::move(transport_socket)), options_(options), version_(config.version()),
stats_(generateUpstreamProxyProtocolStats(scope)),
stats_(stats),
pass_all_tlvs_(config.has_pass_through_tlvs() ? config.pass_through_tlvs().match_type() ==
ProxyProtocolPassThroughTLVs::INCLUDE_ALL
: false) {
Expand Down Expand Up @@ -142,7 +137,8 @@ void UpstreamProxyProtocolSocket::onConnected() {
UpstreamProxyProtocolSocketFactory::UpstreamProxyProtocolSocketFactory(
Network::UpstreamTransportSocketFactoryPtr transport_socket_factory, ProxyProtocolConfig config,
Stats::Scope& scope)
: PassthroughFactory(std::move(transport_socket_factory)), config_(config), scope_(scope) {}
: PassthroughFactory(std::move(transport_socket_factory)), config_(config),
stats_(generateUpstreamProxyProtocolStats(scope)) {}

Network::TransportSocketPtr UpstreamProxyProtocolSocketFactory::createTransportSocket(
Network::TransportSocketOptionsConstSharedPtr options,
Expand All @@ -152,7 +148,7 @@ Network::TransportSocketPtr UpstreamProxyProtocolSocketFactory::createTransportS
return nullptr;
}
return std::make_unique<UpstreamProxyProtocolSocket>(std::move(inner_socket), options, config_,
scope_);
stats_);
}

void UpstreamProxyProtocolSocketFactory::hashKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UpstreamProxyProtocolSocket : public TransportSockets::PassthroughSocket,
public:
UpstreamProxyProtocolSocket(Network::TransportSocketPtr&& transport_socket,
Network::TransportSocketOptionsConstSharedPtr options,
ProxyProtocolConfig config, Stats::Scope& scope);
ProxyProtocolConfig config, const UpstreamProxyProtocolStats& stats);

void setTransportSocketCallbacks(Network::TransportSocketCallbacks& callbacks) override;
Network::IoResult doWrite(Buffer::Instance& buffer, bool end_stream) override;
Expand All @@ -49,7 +49,7 @@ class UpstreamProxyProtocolSocket : public TransportSockets::PassthroughSocket,
Network::TransportSocketCallbacks* callbacks_{};
Buffer::OwnedImpl header_buffer_{};
ProxyProtocolConfig_Version version_{ProxyProtocolConfig_Version::ProxyProtocolConfig_Version_V1};
UpstreamProxyProtocolStats stats_;
const UpstreamProxyProtocolStats& stats_;
const bool pass_all_tlvs_;
absl::flat_hash_set<uint8_t> pass_through_tlvs_{};
};
Expand All @@ -67,9 +67,14 @@ class UpstreamProxyProtocolSocketFactory : public PassthroughFactory {
void hashKey(std::vector<uint8_t>& key,
Network::TransportSocketOptionsConstSharedPtr options) const override;

static UpstreamProxyProtocolStats generateUpstreamProxyProtocolStats(Stats::Scope& stats_scope) {
const char prefix[]{"upstream.proxyprotocol."};
return {ALL_PROXY_PROTOCOL_TRANSPORT_SOCKET_STATS(POOL_COUNTER_PREFIX(stats_scope, prefix))};
}

private:
ProxyProtocolConfig config_;
Stats::Scope& scope_;
UpstreamProxyProtocolStats stats_;
};

} // namespace ProxyProtocol
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ namespace {

class ProxyProtocolTest : public testing::Test {
public:
ProxyProtocolTest()
: stats_(UpstreamProxyProtocolSocketFactory::generateUpstreamProxyProtocolStats(
*stats_store_.rootScope())) {}
void initialize(ProxyProtocolConfig& config,
Network::TransportSocketOptionsConstSharedPtr socket_options) {
auto inner_socket = std::make_unique<NiceMock<Network::MockTransportSocket>>();
inner_socket_ = inner_socket.get();
ON_CALL(transport_callbacks_, ioHandle()).WillByDefault(ReturnRef(io_handle_));
proxy_protocol_socket_ = std::make_unique<UpstreamProxyProtocolSocket>(
std::move(inner_socket), socket_options, config, *stats_store_.rootScope());
std::move(inner_socket), socket_options, config, stats_);
proxy_protocol_socket_->setTransportSocketCallbacks(transport_callbacks_);
proxy_protocol_socket_->onConnected();
}
Expand All @@ -51,6 +54,7 @@ class ProxyProtocolTest : public testing::Test {
std::unique_ptr<UpstreamProxyProtocolSocket> proxy_protocol_socket_;
NiceMock<Network::MockTransportSocketCallbacks> transport_callbacks_;
Stats::TestUtil::TestStore stats_store_;
UpstreamProxyProtocolStats stats_;
};

// Test injects PROXY protocol header only once
Expand Down

0 comments on commit 5b0d56d

Please sign in to comment.