Skip to content

Commit

Permalink
minor update
Browse files Browse the repository at this point in the history
Signed-off-by: wbpcode <wbphub@live.com>
  • Loading branch information
wbpcode committed Apr 27, 2024
1 parent 89cd114 commit 2f70026
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
19 changes: 12 additions & 7 deletions contrib/generic_proxy/filters/network/source/router/router.cc
Expand Up @@ -94,7 +94,7 @@ void GenericUpstream::onPoolFailure(ConnectionPool::PoolFailureReason reason,
host != nullptr ? host->address()->asStringView() : absl::string_view{});

tcp_pool_handle_ = nullptr;
upstream_host_ = std::move(host);
mayUpdateUpstreamHost(std::move(host));

onPoolFailureImpl(reason, transport_failure_reason);
}
Expand All @@ -106,7 +106,7 @@ void GenericUpstream::onPoolReady(Tcp::ConnectionPool::ConnectionDataPtr&& conn_
host->address()->asStringView());

tcp_pool_handle_ = nullptr;
upstream_host_ = std::move(host);
mayUpdateUpstreamHost(std::move(host));

owned_conn_data_ = std::move(conn_data);
owned_conn_data_->addUpstreamCallbacks(*this);
Expand Down Expand Up @@ -355,15 +355,15 @@ UpstreamRequest::UpstreamRequest(RouterFilter& parent, GenericUpstreamSharedPtr
stream_info_(parent.time_source_, nullptr, StreamInfo::FilterState::LifeSpan::FilterChain),
upstream_info_(std::make_shared<StreamInfo::UpstreamInfoImpl>()) {

// Host is known at this point and set the initial upstream host.
onUpstreamHostSelected(generic_upstream_->upstreamHost());

// Set the upstream info for the stream info.
stream_info_.setUpstreamInfo(upstream_info_);
parent_.callbacks_->streamInfo().setUpstreamInfo(upstream_info_);
stream_info_.healthCheck(parent_.callbacks_->streamInfo().healthCheck());
stream_info_.setUpstreamClusterInfo(parent_.cluster_);

// Host is known at this point.
onUpstreamHostSelected(generic_upstream_->upstreamHost());

// Set request options.
auto options = parent_.request_stream_->frameFlags().streamFlags();
stream_id_ = options.streamId();
Expand Down Expand Up @@ -497,6 +497,7 @@ OptRef<const RouteEntry> UpstreamRequest::routeEntry() const {
void UpstreamRequest::onUpstreamFailure(ConnectionPool::PoolFailureReason reason,
absl::string_view transport_failure_reason) {
ENVOY_LOG(debug, "upstream request: tcp connection (bound or owned) failure");
onUpstreamHostSelected(generic_upstream_->upstreamHost());
onUpstreamConnectionReady();

if (reason == ConnectionPool::PoolFailureReason::Overflow) {
Expand All @@ -509,6 +510,7 @@ void UpstreamRequest::onUpstreamFailure(ConnectionPool::PoolFailureReason reason
void UpstreamRequest::onUpstreamSuccess() {
ENVOY_LOG(debug, "upstream request: {} tcp connection has ready",
parent_.config_->bindUpstreamConnection() ? "bound" : "owned");
onUpstreamHostSelected(generic_upstream_->upstreamHost());
onUpstreamConnectionReady();

const auto upstream_host = upstream_info_->upstream_host_.get();
Expand Down Expand Up @@ -588,9 +590,12 @@ void UpstreamRequest::onConnectionClose(Network::ConnectionEvent event) {
}

void UpstreamRequest::onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host) {
ENVOY_LOG(debug, "upstream request: selected upstream {}", host->address()->asString());
if (host == nullptr || host == upstream_info_->upstream_host_) {
return;
}

upstream_info_->setUpstreamHost(std::move(host));
ENVOY_LOG(debug, "upstream request: selected host {}", host->address()->asStringView());
upstream_info_->upstream_host_ = std::move(host);
}

void UpstreamRequest::onUpstreamConnectionReady() {
Expand Down
20 changes: 13 additions & 7 deletions contrib/generic_proxy/filters/network/source/router/router.h
Expand Up @@ -49,9 +49,9 @@ class GenericUpstream : public ClientCodecCallbacks,
public Envoy::Logger::Loggable<Envoy::Logger::Id::upstream> {
public:
GenericUpstream(Upstream::TcpPoolData&& tcp_pool_data, ClientCodecPtr&& client_codec)
: tcp_pool_data_(std::move(tcp_pool_data)), client_codec_(std::move(client_codec)) {
: tcp_pool_data_(std::move(tcp_pool_data)), client_codec_(std::move(client_codec)),
upstream_host_(tcp_pool_data_.host()) {
client_codec_->setCodecCallbacks(*this);
upstream_host_ = tcp_pool_data_.host();
}
~GenericUpstream() override;

Expand All @@ -71,15 +71,22 @@ class GenericUpstream : public ClientCodecCallbacks,
void onEvent(Network::ConnectionEvent event) override { onEventImpl(event); }

ClientCodec& clientCodec() { return *client_codec_; }
void mayUpdateUpstreamHost(Upstream::HostDescriptionConstSharedPtr real_host) {
if (real_host == nullptr || real_host == upstream_host_) {
return;
}

// Update the upstream host iff the connection pool callbacks provide a different
// value.
upstream_host_ = std::move(real_host);
}
Upstream::HostDescriptionConstSharedPtr upstreamHost() const { return upstream_host_; }

// ResponseDecoderCallback
void writeToConnection(Buffer::Instance& buffer) override;
OptRef<Network::Connection> connection() override;
OptRef<const Upstream::ClusterInfo> upstreamCluster() const override {
if (upstream_host_ == nullptr) {
return {};
}
ASSERT(upstream_host_ != nullptr);
return upstream_host_->cluster();
}

Expand All @@ -93,15 +100,14 @@ class GenericUpstream : public ClientCodecCallbacks,
protected:
Upstream::TcpPoolData tcp_pool_data_;
ClientCodecPtr client_codec_;
Upstream::HostDescriptionConstSharedPtr upstream_host_;

// Whether the upstream connection is created. This will be set to true when the initialize()
// is called.
bool initialized_{};

Tcp::ConnectionPool::Cancellable* tcp_pool_handle_{};
Tcp::ConnectionPool::ConnectionDataPtr owned_conn_data_;

Upstream::HostDescriptionConstSharedPtr upstream_host_;
};
using GenericUpstreamSharedPtr = std::shared_ptr<GenericUpstream>;

Expand Down

0 comments on commit 2f70026

Please sign in to comment.