Skip to content

Commit 95bde53

Browse files
author
smorivan
committed
feat core: add plugin service-mesh-proxy
Tests: testing commit_hash:c5307c7e7e1d8d72a974458bcc955c2ccf5fe92e
1 parent 2a96452 commit 95bde53

File tree

10 files changed

+45
-5
lines changed

10 files changed

+45
-5
lines changed

core/include/userver/clients/http/component.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class HttpClient final : public ComponentBase {
6969
static yaml_config::Schema GetStaticConfigSchema();
7070

7171
private:
72-
void OnConfigUpdate(const dynamic_config::Snapshot& config);
72+
void OnConfigUpdate(const dynamic_config::Diff& diff);
7373

7474
void WriteStatistics(utils::statistics::Writer& writer);
7575

core/include/userver/clients/http/plugin.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <vector>
1010

1111
#include <userver/utils/not_null.hpp>
12+
#include <userver/utils/zstring_view.hpp>
1213

1314
USERVER_NAMESPACE_BEGIN
1415

@@ -35,6 +36,10 @@ class PluginRequest final {
3536

3637
void SetTimeout(std::chrono::milliseconds ms);
3738

39+
void SetProxy(utils::zstring_view value);
40+
41+
bool IsProxySet() const;
42+
3843
const std::string& GetOriginalUrl() const;
3944

4045
private:

core/include/userver/dynamic_config/storage/component.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ class DynamicConfig::NoblockSubscriber final {
110110

111111
concurrent::AsyncEventSource<const dynamic_config::Snapshot&>& GetEventSource() noexcept;
112112

113+
concurrent::AsyncEventSource<const dynamic_config::Diff&>& GetDiffSource() noexcept;
114+
113115
private:
114116
DynamicConfig& config_component_;
115117
};

core/src/clients/http/component.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ HttpClient::HttpClient(const ComponentConfig& component_config, const ComponentC
9696
http_client_.SetConfig(bootstrap_config);
9797

9898
auto& config_component = context.FindComponent<components::DynamicConfig>();
99-
subscriber_scope_ = components::DynamicConfig::NoblockSubscriber{config_component}.GetEventSource().AddListener(
99+
subscriber_scope_ = components::DynamicConfig::NoblockSubscriber{config_component}.GetDiffSource().AddListener(
100100
this, kName, &HttpClient::OnConfigUpdate
101101
);
102102

@@ -126,7 +126,19 @@ HttpClient::~HttpClient() {
126126

127127
clients::http::Client& HttpClient::GetHttpClient() { return http_client_; }
128128

129-
void HttpClient::OnConfigUpdate(const dynamic_config::Snapshot& config) {
129+
void HttpClient::OnConfigUpdate(const dynamic_config::Diff& diff) {
130+
const auto& config = diff.current;
131+
const auto& prev_config_opt = diff.previous;
132+
if (prev_config_opt) {
133+
const auto& prev_config = *prev_config_opt;
134+
if (config[::dynamic_config::HTTP_CLIENT_CONNECTION_POOL_SIZE] ==
135+
prev_config[::dynamic_config::HTTP_CLIENT_CONNECTION_POOL_SIZE] &&
136+
config[::dynamic_config::USERVER_HTTP_PROXY] == prev_config[::dynamic_config::USERVER_HTTP_PROXY] &&
137+
config[::dynamic_config::HTTP_CLIENT_CONNECT_THROTTLE] ==
138+
prev_config[::dynamic_config::HTTP_CLIENT_CONNECT_THROTTLE]) {
139+
return;
140+
}
141+
}
130142
http_client_.SetConfig(clients::http::impl::Config{
131143
config[::dynamic_config::HTTP_CLIENT_CONNECTION_POOL_SIZE],
132144
config[::dynamic_config::USERVER_HTTP_PROXY],

core/src/clients/http/plugin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ void PluginRequest::SetTimeout(std::chrono::milliseconds ms) {
3232
state_.SetEasyTimeout(ms);
3333
}
3434

35+
void PluginRequest::SetProxy(utils::zstring_view value) { state_.proxy(value); }
36+
37+
bool PluginRequest::IsProxySet() const { return state_.IsProxySet(); }
38+
3539
const std::string& PluginRequest::GetOriginalUrl() const { return state_.easy().get_original_url(); }
3640

3741
Plugin::Plugin(std::string name) : name_(std::move(name)) {}

core/src/clients/http/request_state.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@ void RequestState::proxy(utils::zstring_view value) {
358358
easy().set_proxy(value);
359359
}
360360

361+
bool RequestState::IsProxySet() const { return proxy_url_.has_value(); }
362+
361363
void RequestState::proxy_auth_type(curl::easy::proxyauth_t value) { easy().set_proxy_auth(value); }
362364

363365
void RequestState::http_auth_type(
@@ -1024,7 +1026,10 @@ void RequestState::WithRequestStats(const Func& func) {
10241026
void RequestState::ResolveTargetAddress(clients::dns::Resolver& resolver) {
10251027
const auto deadline = engine::Deadline::FromDuration(remote_timeout_);
10261028

1027-
const MaybeOwnedUrl target{proxy_url_, easy()};
1029+
const static std::string kEmptyString;
1030+
const std::string& proxy_url = proxy_url_ ? *proxy_url_ : kEmptyString;
1031+
const MaybeOwnedUrl target{proxy_url, easy()};
1032+
10281033
const std::string hostname = target.Get().GetHostPtr().get();
10291034

10301035
// CURLOPT_RESOLV hostnames cannot contain colons (as IPv6 addresses do), skip

core/src/clients/http/request_state.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class RequestState : public std::enable_shared_from_this<RequestState> {
140140

141141
void SetTracingManager(const tracing::TracingManagerBase&);
142142

143+
bool IsProxySet() const;
144+
143145
PluginRequest GetEditableRequestInstance();
144146

145147
private:
@@ -237,7 +239,7 @@ class RequestState : public std::enable_shared_from_this<RequestState> {
237239
std::array<char, CURL_ERROR_SIZE> errorbuffer_{};
238240

239241
clients::dns::Resolver* resolver_{nullptr};
240-
std::string proxy_url_;
242+
std::optional<std::string> proxy_url_;
241243
impl::PluginPipeline plugin_pipeline_;
242244

243245
struct StreamData {

core/src/dynamic_config/storage/component.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class DynamicConfig::Impl final {
5959
dynamic_config::Source GetSource();
6060
auto& GetChannel() { return cache_.GetChannel(); }
6161

62+
auto& GetDiffChannel() { return cache_.GetDiffChannel(); }
63+
6264
const dynamic_config::DocsMap& GetDefaultDocsMap() const;
6365
bool AreUpdatesEnabled() const;
6466

@@ -321,6 +323,10 @@ concurrent::AsyncEventSource<const dynamic_config::Snapshot&>& DynamicConfig::No
321323
return config_component_.impl_->GetChannel();
322324
}
323325

326+
concurrent::AsyncEventSource<const dynamic_config::Diff&>& DynamicConfig::NoblockSubscriber::GetDiffSource() noexcept {
327+
return config_component_.impl_->GetDiffChannel();
328+
}
329+
324330
DynamicConfig::DynamicConfig(const ComponentConfig& config, const ComponentContext& context)
325331
: DynamicConfigUpdatesSinkBase(config, context), impl_(std::make_unique<Impl>(config, context)) {
326332
if (!impl_->AreUpdatesEnabled()) {

core/src/dynamic_config/storage_data.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ void StorageData::Update(SnapshotData config, AfterAssignHook after_assign_hook)
5050

5151
StorageData::SnapshotChannel& StorageData::GetChannel() { return snapshot_channel_; }
5252

53+
StorageData::DiffChannel& StorageData::GetDiffChannel() { return diff_channel_; }
54+
5355
concurrent::AsyncEventSubscriberScope
5456
StorageData::DoUpdateAndListen(concurrent::FunctionId id, std::string_view name, SnapshotChannel::Function&& func) {
5557
auto updater = [&, func_copy = func] { func_copy(GetSnapshot()); };

core/src/dynamic_config/storage_data.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class StorageData final {
2626

2727
SnapshotChannel& GetChannel();
2828

29+
DiffChannel& GetDiffChannel();
30+
2931
concurrent::AsyncEventSubscriberScope
3032
DoUpdateAndListen(concurrent::FunctionId id, std::string_view name, SnapshotChannel::Function&& func);
3133

0 commit comments

Comments
 (0)