Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic proxy: expose the codec error to upper layer #33788

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -31,8 +31,9 @@ class ServerCodecCallbacks {

/**
* If request decoding failure then this method will be called.
* @param reason the reason of decoding failure.
*/
virtual void onDecodingFailure() PURE;
virtual void onDecodingFailure(absl::string_view reason = {}) PURE;

/**
* Write specified data to the downstream connection. This is could be used to write
Expand Down Expand Up @@ -71,8 +72,9 @@ class ClientCodecCallbacks {

/**
* If response decoding failure then this method will be called.
* @param reason the reason of decoding failure.
*/
virtual void onDecodingFailure() PURE;
virtual void onDecodingFailure(absl::string_view reason = {}) PURE;

/**
* Write specified data to the upstream connection. This is could be used to write
Expand Down Expand Up @@ -114,6 +116,12 @@ class EncodingCallbacks {
*/
virtual void onEncodingSuccess(Buffer::Instance& buffer, bool end_stream) PURE;

/**
* If encoding failure then this method will be called.
* @param reason the reason of encoding failure.
*/
virtual void onEncodingFailure(absl::string_view reason = {}) PURE;

/**
* The route that the request is matched to. This is optional when encoding the response
* (by server codec) because the request may not be matched to any route and the
Expand Down
16 changes: 4 additions & 12 deletions contrib/generic_proxy/filters/network/source/interface/stream.h
Expand Up @@ -285,18 +285,10 @@ using ResponseSharedPtr = std::shared_ptr<Response>;

template <class T> class StreamFramePtrHelper {
public:
StreamFramePtrHelper(StreamFramePtr frame) {
auto frame_ptr = frame.release();

auto typed_frame_ptr = dynamic_cast<T*>(frame_ptr);

if (typed_frame_ptr == nullptr) {
// If the frame is not the expected type, wrap it
// in the original StreamFramePtr.
frame_ = StreamFramePtr{frame_ptr};
} else {
// If the frame is the expected type, wrap it
// in the typed frame unique pointer.
StreamFramePtrHelper(StreamFramePtr frame) : frame_(std::move(frame)) {
if (auto typed_frame_ptr = dynamic_cast<T*>(frame_.get()); typed_frame_ptr != nullptr) {
// If the frame is the expected type, wrap it in the typed frame unique pointer.
frame_.release();
typed_frame_ = std::unique_ptr<T>{typed_frame_ptr};
}
}
Expand Down
9 changes: 7 additions & 2 deletions contrib/generic_proxy/filters/network/source/proxy.cc
Expand Up @@ -314,6 +314,11 @@ void ActiveStream::onEncodingSuccess(Buffer::Instance& buffer, bool end_stream)
parent_.deferredStream(*this);
}

void ActiveStream::onEncodingFailure(absl::string_view reason) {
ENVOY_LOG(error, "Generic proxy: response encoding failure: {}", reason);
resetStream(DownstreamStreamResetReason::ProtocolError);
}

void ActiveStream::initializeFilterChain(FilterChainFactory& factory) {
factory.createFilterChain(*this);
// Reverse the encoder filter chain so that the first encoder filter is the last filter in the
Expand Down Expand Up @@ -390,9 +395,9 @@ void Filter::onDecodingSuccess(StreamFramePtr request) {
onDecodingFailure();
}

void Filter::onDecodingFailure() {
void Filter::onDecodingFailure(absl::string_view reason) {
ENVOY_LOG(error, "generic proxy: request decoding failure: {}", reason);
stats_helper_.onRequestDecodingError();

resetDownstreamAllStreams(DownstreamStreamResetReason::ProtocolError);
closeDownstreamConnection();
}
Expand Down
3 changes: 2 additions & 1 deletion contrib/generic_proxy/filters/network/source/proxy.h
Expand Up @@ -263,6 +263,7 @@ class ActiveStream : public FilterChainManager,

// ResponseEncoderCallback
void onEncodingSuccess(Buffer::Instance& buffer, bool end_stream) override;
void onEncodingFailure(absl::string_view reason = {}) override;
OptRef<const RouteEntry> routeEntry() const override {
return makeOptRefFromPtr<const RouteEntry>(cached_route_entry_.get());
}
Expand Down Expand Up @@ -372,7 +373,7 @@ class Filter : public Envoy::Network::ReadFilter,

// RequestDecoderCallback
void onDecodingSuccess(StreamFramePtr request) override;
void onDecodingFailure() override;
void onDecodingFailure(absl::string_view reason = {}) override;
void writeToConnection(Buffer::Instance& buffer) override;
OptRef<Network::Connection> connection() override;

Expand Down