Repository: envoyproxy/envoy
File: source/extensions/filters/http/ext_authz/ext_authz.h (C/C++)

Code before:
  void updateLoggingInfo(const absl::optional<Grpc::Status::GrpcStatus>& grpc_status);

  // This holds a set of flags defined in per-route configuration.
  struct PerRouteFlags {
    const bool skip_check_;
    const envoy::extensions::filters::http::ext_authz::v3::CheckSettings& check_settings_;
  };
  PerRouteFlags getPerRouteFlags(const Router::RouteConstSharedPtr& route) const;

  // State of this filter's communication with the external authorization service.
  // The filter has either not started calling the external service, in the middle of calling
  // it or has completed.
  enum class State { NotStarted, Calling, Complete };

  // FilterReturn is used to capture what the return code should be to the filter chain.
  // if this filter is either in the middle of calling the service or the result is denied then
  // the filter chain should stop. Otherwise, the filter chain can continue to the next filter.
  enum class FilterReturn { ContinueDecoding, StopDecoding };

  Http::HeaderMapPtr getHeaderMap(const Filters::Common::ExtAuthz::ResponsePtr& response);
  FilterConfigSharedPtr config_;
  Filters::Common::ExtAuthz::ClientPtr client_;
  // Per-route gRPC client that overrides the default client when specified.
  Filters::Common::ExtAuthz::ClientPtr per_route_client_;
  // Client currently being used for the authorization check.
  Filters::Common::ExtAuthz::Client* active_client_{nullptr};
  // Server context for creating per-route clients.
  Server::Configuration::ServerFactoryContext* server_context_{nullptr};
  Http::StreamDecoderFilterCallbacks* decoder_callbacks_{};
  Http::StreamEncoderFilterCallbacks* encoder_callbacks_{};
  Http::RequestHeaderMap* request_headers_;
  Http::HeaderVector response_headers_to_add_{};
  Http::HeaderVector response_headers_to_set_{};
  Http::HeaderVector response_headers_to_add_if_absent_{};
  Http::HeaderVector response_headers_to_overwrite_if_exists_{};
  State state_{State::NotStarted};
  FilterReturn filter_return_{FilterReturn::ContinueDecoding};
  Upstream::ClusterInfoConstSharedPtr cluster_;
  // The stats for the filter.
  ExtAuthzFilterStats stats_;
  ExtAuthzLoggingInfo* logging_info_{nullptr};

  // This is used to hold the final configs after we merge them with per-route configs.
  bool allow_partial_message_{};
  uint32_t max_request_bytes_{};

  // Used to identify if the callback to onComplete() is synchronous (on the stack) or asynchronous.
  bool initiating_call_{};
  bool buffer_data_{};
  bool skip_check_{false};
  envoy::service::auth::v3::CheckRequest check_request_{};

Review comment (suggestion):
instead of having 4 variables: `client_`, `per_route_client_`, local `client_to_use`, and new `active_client_`, can we just update the client_ with per route override?

like client_ = std:move(per_route_client).

This will make code simple and easier to maintain.

Code after:
  void updateLoggingInfo(const absl::optional<Grpc::Status::GrpcStatus>& grpc_status);

  // This holds a set of flags defined in per-route configuration.
  struct PerRouteFlags {
    const bool skip_check_;
    const envoy::extensions::filters::http::ext_authz::v3::CheckSettings& check_settings_;
  };
  PerRouteFlags getPerRouteFlags(const Router::RouteConstSharedPtr& route) const;

  // State of this filter's communication with the external authorization service.
  // The filter has either not started calling the external service, in the middle of calling
  // it or has completed.
  enum class State { NotStarted, Calling, Complete };

  // FilterReturn is used to capture what the return code should be to the filter chain.
  // if this filter is either in the middle of calling the service or the result is denied then
  // the filter chain should stop. Otherwise, the filter chain can continue to the next filter.
  enum class FilterReturn { ContinueDecoding, StopDecoding };

  Http::HeaderMapPtr getHeaderMap(const Filters::Common::ExtAuthz::ResponsePtr& response);
  FilterConfigSharedPtr config_;
  Filters::Common::ExtAuthz::ClientPtr client_;
  // Server context for creating per-route clients.
  Server::Configuration::ServerFactoryContext* server_context_{nullptr};
  Http::StreamDecoderFilterCallbacks* decoder_callbacks_{};
  Http::StreamEncoderFilterCallbacks* encoder_callbacks_{};
  Http::RequestHeaderMap* request_headers_;
  Http::HeaderVector response_headers_to_add_{};
  Http::HeaderVector response_headers_to_set_{};
  Http::HeaderVector response_headers_to_add_if_absent_{};
  Http::HeaderVector response_headers_to_overwrite_if_exists_{};
  State state_{State::NotStarted};
  FilterReturn filter_return_{FilterReturn::ContinueDecoding};
  Upstream::ClusterInfoConstSharedPtr cluster_;
  // The stats for the filter.
  ExtAuthzFilterStats stats_;
  ExtAuthzLoggingInfo* logging_info_{nullptr};

  // This is used to hold the final configs after we merge them with per-route configs.
  bool allow_partial_message_{};
  uint32_t max_request_bytes_{};

  // Used to identify if the callback to onComplete() is synchronous (on the stack) or asynchronous.
  bool initiating_call_{};
  bool buffer_data_{};
  bool skip_check_{false};
  envoy::service::auth::v3::CheckRequest check_request_{};
};

} // namespace ExtAuthz
} // namespace HttpFilters