Skip to content

Commit

Permalink
generic proxy: expose the codec error to upper layer (#33788)
Browse files Browse the repository at this point in the history
Signed-off-by: wbpcode <wbphub@live.com>
  • Loading branch information
wbpcode committed Apr 29, 2024
1 parent 048d9a2 commit 08f2ec0
Show file tree
Hide file tree
Showing 13 changed files with 261 additions and 182 deletions.
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

0 comments on commit 08f2ec0

Please sign in to comment.