diff --git a/envoy/router/BUILD b/envoy/router/BUILD index 28de863193e0..e3306bee4907 100644 --- a/envoy/router/BUILD +++ b/envoy/router/BUILD @@ -58,6 +58,20 @@ envoy_cc_library( ], ) +envoy_cc_library( + name = "router_filter_interface", + hdrs = ["router_filter_interface.h"], + external_deps = ["abseil_optional"], + deps = [ + "//envoy/http:filter_interface", + "//envoy/http:header_map_interface", + "//envoy/stats:stats_interface", + "//envoy/stream_info:stream_info_interface", + "//envoy/upstream:cluster_manager_interface", + "//envoy/upstream:host_description_interface", + ], +) + envoy_cc_library( name = "router_interface", hdrs = ["router.h"], diff --git a/envoy/router/router_filter_interface.h b/envoy/router/router_filter_interface.h new file mode 100644 index 000000000000..34c306afc860 --- /dev/null +++ b/envoy/router/router_filter_interface.h @@ -0,0 +1,158 @@ +#pragma once + +#include "envoy/http/filter.h" +#include "envoy/http/header_map.h" +#include "envoy/stats/scope.h" +#include "envoy/stats/stats_macros.h" +#include "envoy/stream_info/stream_info.h" +#include "envoy/upstream/cluster_manager.h" +#include "envoy/upstream/host_description.h" + +namespace Envoy { +namespace Router { + +class UpstreamRequest; + +// This groups various per-stream timeouts conveniently together. +struct TimeoutData { + std::chrono::milliseconds global_timeout_{0}; + std::chrono::milliseconds per_try_timeout_{0}; + std::chrono::milliseconds per_try_idle_timeout_{0}; +}; + +// The interface the UpstreamRequest has to interact with the router filter. +class RouterFilterInterface { +public: + virtual ~RouterFilterInterface() = default; + + /** + * This will be called when upstream 1xx headers are ready to be processed by downstream code. + * @param headers contains the 1xx headers + * @param upstream_request inicates which UpstreamRequest the 1xx headers are from. + * + */ + virtual void onUpstream1xxHeaders(Http::ResponseHeaderMapPtr&& headers, + UpstreamRequest& upstream_request) PURE; + /** + * This will be called when upstream non-1xx headers are ready to be processed by downstream code. + * @param headers contains the headers + * @param upstream_request inicates which UpstreamRequest the headers are from. + * @param end_stream indicates if the response is complete. + * + */ + virtual void onUpstreamHeaders(uint64_t response_code, Http::ResponseHeaderMapPtr&& headers, + UpstreamRequest& upstream_request, bool end_stream) PURE; + /** + * This will be called when upstream data is ready to be processed by downstream code. + * @param data contains the data to process + * @param upstream_request inicates which UpstreamRequest the data is from. + * @param end_stream indicates if the response is complete. + * + */ + virtual void onUpstreamData(Buffer::Instance& data, UpstreamRequest& upstream_request, + bool end_stream) PURE; + /** + * This will be called when upstream trailers are ready to be processed by downstream code. + * @param trailers contains the trailers to process + * @param upstream_request inicates which UpstreamRequest the trailers are from. + * + */ + virtual void onUpstreamTrailers(Http::ResponseTrailerMapPtr&& trailers, + UpstreamRequest& upstream_request) PURE; + /** + * This will be called when upstream metadata is ready to be processed by downstream code. + * @param metadata contains the metadata to process + * @param upstream_request inicates which UpstreamRequest the metadata is from. + * + */ + virtual void onUpstreamMetadata(Http::MetadataMapPtr&& metadata_map) PURE; + + /** + * This will be called when an upstream reset is ready to be processed by downstream code. + * @param reset_reason indicates the reason for the reset. + * @param transport_failure optionally indicates any transport failure. + * @param upstream_request inicates which UpstreamRequest the reset is from. + * + */ + virtual void onUpstreamReset(Http::StreamResetReason reset_reason, + absl::string_view transport_failure, + UpstreamRequest& upstream_request) PURE; + + /** + * This will be called when an upstream host is selected. This is called both + * if the host can accomodate the stream and if the host is selected but unusable. + * @param host the host selected for the request + * @param pool_success indicates if the host can be used for the request. + */ + virtual void onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host, + bool pool_success) PURE; + /* + * This will be called if a per-try timeout fails. + * @param upstream_request inicates which UpstreamRequest which timed out + */ + virtual void onPerTryTimeout(UpstreamRequest& upstream_request) PURE; + + /* + * This will be called if a per-try idle timeout fails. + * @param upstream_request inicates which UpstreamRequest which timed out + */ + virtual void onPerTryIdleTimeout(UpstreamRequest& upstream_request) PURE; + + /* + * This will be called if the max stream duration was reached. + * @param upstream_request inicates which UpstreamRequest which timed out + */ + virtual void onStreamMaxDurationReached(UpstreamRequest& upstream_request) PURE; + + /* + * @returns the Router filter's StreamDecoderFilterCallbacks. + */ + virtual Http::StreamDecoderFilterCallbacks* callbacks() PURE; + /* + * @returns the cluster for this stream. + */ + virtual Upstream::ClusterInfoConstSharedPtr cluster() PURE; + + /* + * @returns the FilterConfig for this stream + */ + virtual FilterConfig& config() PURE; + + /* + * @returns the various timeouts for this stream. + */ + virtual TimeoutData timeout() PURE; + + /* + * @returns the dynamic max stream duraration for this stream, if set. + */ + virtual absl::optional dynamicMaxStreamDuration() const PURE; + + /* + * @returns the request headers for the stream. + */ + virtual Http::RequestHeaderMap* downstreamHeaders() PURE; + + /* + * @returns the request trailers for the stream. + */ + virtual Http::RequestTrailerMap* downstreamTrailers() PURE; + + /* + * @returns true if the downstream response has started. + */ + virtual bool downstreamResponseStarted() const PURE; + + /* + * @returns true if end_stream has been sent from the upstream side to the downstream side. + */ + virtual bool downstreamEndStream() const PURE; + + /* + * @returns the number of attempts (e.g. retries) performed for this stream. + */ + virtual uint32_t attemptCount() const PURE; +}; + +} // namespace Router +} // namespace Envoy diff --git a/source/common/router/BUILD b/source/common/router/BUILD index acd16cbd966f..09c9849e4887 100644 --- a/source/common/router/BUILD +++ b/source/common/router/BUILD @@ -280,7 +280,6 @@ envoy_cc_library( ], hdrs = [ "router.h", - "router_filter_interface.h", "upstream_request.h", ], deps = [ @@ -300,6 +299,7 @@ envoy_cc_library( "//envoy/http:filter_interface", "//envoy/http:stateful_session_interface", "//envoy/local_info:local_info_interface", + "//envoy/router:router_filter_interface", "//envoy/router:shadow_writer_interface", "//envoy/runtime:runtime_interface", "//envoy/server:factory_context_interface", diff --git a/source/common/router/router.h b/source/common/router/router.h index 3af60fcbbaf2..21f8ff251c3c 100644 --- a/source/common/router/router.h +++ b/source/common/router/router.h @@ -16,6 +16,7 @@ #include "envoy/http/hash_policy.h" #include "envoy/http/stateful_session.h" #include "envoy/local_info/local_info.h" +#include "envoy/router/router_filter_interface.h" #include "envoy/router/shadow_writer.h" #include "envoy/runtime/runtime.h" #include "envoy/server/factory_context.h" @@ -38,7 +39,6 @@ #include "source/common/http/utility.h" #include "source/common/router/config_impl.h" #include "source/common/router/context_impl.h" -#include "source/common/router/router_filter_interface.h" #include "source/common/router/upstream_request.h" #include "source/common/stats/symbol_table.h" #include "source/common/stream_info/stream_info_impl.h" diff --git a/source/common/router/router_filter_interface.h b/source/common/router/router_filter_interface.h deleted file mode 100644 index 311ccf80825d..000000000000 --- a/source/common/router/router_filter_interface.h +++ /dev/null @@ -1,58 +0,0 @@ -#pragma once - -#include "envoy/http/filter.h" -#include "envoy/http/header_map.h" -#include "envoy/stats/scope.h" -#include "envoy/stats/stats_macros.h" -#include "envoy/stream_info/stream_info.h" -#include "envoy/upstream/cluster_manager.h" -#include "envoy/upstream/host_description.h" - -namespace Envoy { -namespace Router { - -class UpstreamRequest; - -struct TimeoutData { - std::chrono::milliseconds global_timeout_{0}; - std::chrono::milliseconds per_try_timeout_{0}; - std::chrono::milliseconds per_try_idle_timeout_{0}; -}; - -// The interface the UpstreamRequest has to interact with the router filter. -class RouterFilterInterface { -public: - virtual ~RouterFilterInterface() = default; - - virtual void onUpstream1xxHeaders(Http::ResponseHeaderMapPtr&& headers, - UpstreamRequest& upstream_request) PURE; - virtual void onUpstreamHeaders(uint64_t response_code, Http::ResponseHeaderMapPtr&& headers, - UpstreamRequest& upstream_request, bool end_stream) PURE; - virtual void onUpstreamData(Buffer::Instance& data, UpstreamRequest& upstream_request, - bool end_stream) PURE; - virtual void onUpstreamTrailers(Http::ResponseTrailerMapPtr&& trailers, - UpstreamRequest& upstream_request) PURE; - virtual void onUpstreamMetadata(Http::MetadataMapPtr&& metadata_map) PURE; - virtual void onUpstreamReset(Http::StreamResetReason reset_reason, - absl::string_view transport_failure, - UpstreamRequest& upstream_request) PURE; - virtual void onUpstreamHostSelected(Upstream::HostDescriptionConstSharedPtr host, - bool pool_success) PURE; - virtual void onPerTryTimeout(UpstreamRequest& upstream_request) PURE; - virtual void onPerTryIdleTimeout(UpstreamRequest& upstream_request) PURE; - virtual void onStreamMaxDurationReached(UpstreamRequest& upstream_request) PURE; - - virtual Http::StreamDecoderFilterCallbacks* callbacks() PURE; - virtual Upstream::ClusterInfoConstSharedPtr cluster() PURE; - virtual FilterConfig& config() PURE; - virtual TimeoutData timeout() PURE; - virtual absl::optional dynamicMaxStreamDuration() const PURE; - virtual Http::RequestHeaderMap* downstreamHeaders() PURE; - virtual Http::RequestTrailerMap* downstreamTrailers() PURE; - virtual bool downstreamResponseStarted() const PURE; - virtual bool downstreamEndStream() const PURE; - virtual uint32_t attemptCount() const PURE; -}; - -} // namespace Router -} // namespace Envoy