Skip to content

Commit

Permalink
golang fitler: use Router::StringAccessor instead of GoStringFilterSt…
Browse files Browse the repository at this point in the history
…ate. (envoyproxy#34764)

Signed-off-by: doujiang24 <[email protected]>
Signed-off-by: antoniovleonti <[email protected]>
  • Loading branch information
doujiang24 authored and antoniovleonti committed Jun 26, 2024
1 parent 4b23fc1 commit 8f3c8b7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 30 deletions.
1 change: 1 addition & 0 deletions contrib/golang/filters/http/source/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ envoy_cc_library(
"//source/common/http:utility_lib",
"//source/common/http/http1:codec_lib",
"//source/common/protobuf:utility_lib",
"//source/common/router:string_accessor_lib",
"//source/extensions/filters/common/expr:cel_state_lib",
"//source/extensions/filters/common/expr:evaluator_lib",
"@com_google_cel_cpp//eval/public:activation",
Expand Down
14 changes: 8 additions & 6 deletions contrib/golang/filters/http/source/golang_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <vector>

#include "envoy/http/codes.h"
#include "envoy/router/string_accessor.h"

#include "source/common/buffer/buffer_impl.h"
#include "source/common/common/base64.h"
Expand All @@ -18,6 +19,7 @@
#include "source/common/grpc/status.h"
#include "source/common/http/headers.h"
#include "source/common/http/http1/codec_impl.h"
#include "source/common/router/string_accessor_impl.h"
#include "source/extensions/filters/common/expr/context.h"

#include "eval/public/cel_value.h"
Expand Down Expand Up @@ -1054,13 +1056,13 @@ CAPIStatus Filter::setStringFilterState(absl::string_view key, absl::string_view

if (isThreadSafe()) {
streamInfo().filterState()->setData(
key, std::make_shared<GoStringFilterState>(value),
key, std::make_shared<Router::StringAccessorImpl>(value),
static_cast<StreamInfo::FilterState::StateType>(state_type),
static_cast<StreamInfo::FilterState::LifeSpan>(life_span),
static_cast<StreamInfo::StreamSharingMayImpactPooling>(stream_sharing));
} else {
auto key_str = std::string(key);
auto filter_state = std::make_shared<GoStringFilterState>(value);
auto filter_state = std::make_shared<Router::StringAccessorImpl>(value);
auto weak_ptr = weak_from_this();
getDispatcher().post(
[this, weak_ptr, key_str, filter_state, state_type, life_span, stream_sharing] {
Expand All @@ -1087,9 +1089,9 @@ CAPIStatus Filter::getStringFilterState(absl::string_view key, uint64_t* value_d
}

if (isThreadSafe()) {
auto go_filter_state = streamInfo().filterState()->getDataReadOnly<GoStringFilterState>(key);
auto go_filter_state = streamInfo().filterState()->getDataReadOnly<Router::StringAccessor>(key);
if (go_filter_state) {
req_->strValue = go_filter_state->value();
req_->strValue = go_filter_state->asString();
*value_data = reinterpret_cast<uint64_t>(req_->strValue.data());
*value_len = req_->strValue.length();
}
Expand All @@ -1099,9 +1101,9 @@ CAPIStatus Filter::getStringFilterState(absl::string_view key, uint64_t* value_d
getDispatcher().post([this, weak_ptr, key_str, value_data, value_len] {
if (!weak_ptr.expired() && !hasDestroyed()) {
auto go_filter_state =
streamInfo().filterState()->getDataReadOnly<GoStringFilterState>(key_str);
streamInfo().filterState()->getDataReadOnly<Router::StringAccessor>(key_str);
if (go_filter_state) {
req_->strValue = go_filter_state->value();
req_->strValue = go_filter_state->asString();
*value_data = reinterpret_cast<uint64_t>(req_->strValue.data());
*value_len = req_->strValue.length();
}
Expand Down
9 changes: 0 additions & 9 deletions contrib/golang/filters/http/source/golang_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,6 @@ struct httpConfigInternal : httpConfig {
std::weak_ptr<FilterConfig> weakFilterConfig() { return config_; }
};

class GoStringFilterState : public StreamInfo::FilterState::Object {
public:
GoStringFilterState(absl::string_view value) : value_(value) {}
const std::string& value() const { return value_; }

private:
const std::string value_;
};

} // namespace Golang
} // namespace HttpFilters
} // namespace Extensions
Expand Down
1 change: 1 addition & 0 deletions contrib/golang/filters/network/source/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ envoy_cc_library(
":cgo",
":upstream",
"//contrib/golang/common/dso:dso_lib",
"//source/common/router:string_accessor_lib",
"@envoy_api//contrib/envoy/extensions/filters/network/golang/v3alpha:pkg_cc_proto",
],
)
Expand Down
14 changes: 8 additions & 6 deletions contrib/golang/filters/network/source/golang.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#include <cstdint>

#include "envoy/network/connection.h"
#include "envoy/router/string_accessor.h"

#include "source/common/common/assert.h"
#include "source/common/router/string_accessor_impl.h"

namespace Envoy {
namespace Extensions {
Expand Down Expand Up @@ -123,13 +125,13 @@ CAPIStatus Filter::setFilterState(absl::string_view key, absl::string_view value

if (dispatcher_->isThreadSafe()) {
read_callbacks_->connection().streamInfo().filterState()->setData(
key, std::make_shared<GoStringFilterState>(value),
key, std::make_shared<Router::StringAccessorImpl>(value),
static_cast<StreamInfo::FilterState::StateType>(state_type),
static_cast<StreamInfo::FilterState::LifeSpan>(life_span),
static_cast<StreamInfo::StreamSharingMayImpactPooling>(stream_sharing));
} else {
auto key_str = std::string(key);
auto filter_state = std::make_shared<GoStringFilterState>(value);
auto filter_state = std::make_shared<Router::StringAccessorImpl>(value);
auto weak_ptr = weak_from_this();
dispatcher_->post(
[this, weak_ptr, key_str, filter_state, state_type, life_span, stream_sharing] {
Expand Down Expand Up @@ -160,9 +162,9 @@ CAPIStatus Filter::getFilterState(absl::string_view key, GoString* value_str) {
auto go_filter_state = read_callbacks_->connection()
.streamInfo()
.filterState()
->getDataReadOnly<GoStringFilterState>(key);
->getDataReadOnly<Router::StringAccessor>(key);
if (go_filter_state) {
wrapper_->str_value_ = go_filter_state->value();
wrapper_->str_value_ = go_filter_state->asString();
value_str->p = wrapper_->str_value_.data();
value_str->n = wrapper_->str_value_.length();
}
Expand All @@ -174,9 +176,9 @@ CAPIStatus Filter::getFilterState(absl::string_view key, GoString* value_str) {
auto go_filter_state = read_callbacks_->connection()
.streamInfo()
.filterState()
->getDataReadOnly<GoStringFilterState>(key_str);
->getDataReadOnly<Router::StringAccessor>(key_str);
if (go_filter_state) {
wrapper_->str_value_ = go_filter_state->value();
wrapper_->str_value_ = go_filter_state->asString();
value_str->p = wrapper_->str_value_.data();
value_str->n = wrapper_->str_value_.length();
}
Expand Down
9 changes: 0 additions & 9 deletions contrib/golang/filters/network/source/golang.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,6 @@ struct FilterWrapper {
std::string str_value_;
};

class GoStringFilterState : public StreamInfo::FilterState::Object {
public:
GoStringFilterState(absl::string_view value) : value_(value) {}
const std::string& value() const { return value_; }

private:
const std::string value_;
};

} // namespace Golang
} // namespace NetworkFilters
} // namespace Extensions
Expand Down

0 comments on commit 8f3c8b7

Please sign in to comment.