From a1882dc7d2c834d0e2a287f5a96ca90018451f43 Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Thu, 1 Jun 2017 14:01:13 -0700 Subject: [PATCH] Use check response status. (#71) --- mixerclient/src/client_impl.cc | 14 +++++--------- mixerclient/utils/protobuf.cc | 4 ++++ mixerclient/utils/protobuf.h | 5 +++++ 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/mixerclient/src/client_impl.cc b/mixerclient/src/client_impl.cc index e06c8a2cf021..f398e1170e2d 100644 --- a/mixerclient/src/client_impl.cc +++ b/mixerclient/src/client_impl.cc @@ -26,11 +26,6 @@ using ::google::protobuf::util::error::Code; namespace istio { namespace mixer_client { -namespace { -bool IsNetworkError(Status status) { - return status.error_code() == Code::UNAVAILABLE; -} -} // namespace MixerClientImpl::MixerClientImpl(const MixerClientOptions &options) : options_(options), converter_({}) { @@ -55,17 +50,18 @@ void MixerClientImpl::Check(const Attributes &attributes, DoneFunc on_done) { auto response = new CheckResponse; options_.check_transport(request, response, [this, signature, response, on_done](const Status &status) { - delete response; - if (IsNetworkError(status)) { + if (!status.ok()) { if (options_.check_options.network_fail_open) { on_done(Status::OK); } else { on_done(status); } } else { - check_cache_->CacheResponse(signature, status); - on_done(status); + Status resp_status = ConvertRpcStatus(response->status()); + check_cache_->CacheResponse(signature, resp_status); + on_done(resp_status); } + delete response; }); } diff --git a/mixerclient/utils/protobuf.cc b/mixerclient/utils/protobuf.cc index e8d543c4dfdc..fd795c0c1956 100644 --- a/mixerclient/utils/protobuf.cc +++ b/mixerclient/utils/protobuf.cc @@ -48,5 +48,9 @@ milliseconds ToMilliseonds(const Duration& duration) { duration_cast(nanoseconds(duration.nanos())); } +Status ConvertRpcStatus(const ::google::rpc::Status& status) { + return Status(static_cast(status.code()), status.message()); +} + } // namespace mixer_client } // namespace istio diff --git a/mixerclient/utils/protobuf.h b/mixerclient/utils/protobuf.h index c2f7d75bb195..52f3f31fd677 100644 --- a/mixerclient/utils/protobuf.h +++ b/mixerclient/utils/protobuf.h @@ -19,6 +19,7 @@ #include "google/protobuf/duration.pb.h" #include "google/protobuf/stubs/status.h" #include "google/protobuf/timestamp.pb.h" +#include "mixer/v1/service.pb.h" #include @@ -36,6 +37,10 @@ ::google::protobuf::Duration CreateDuration(std::chrono::nanoseconds value); std::chrono::milliseconds ToMilliseonds( const ::google::protobuf::Duration& duration); +// Convert from grpc status to protobuf status. +::google::protobuf::util::Status ConvertRpcStatus( + const ::google::rpc::Status& status); + } // namespace mixer_client } // namespace istio