Skip to content

Commit

Permalink
Converts mixer server INTERNAL code properly. (envoyproxy#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwzhang authored Aug 17, 2017
1 parent 13d48d3 commit 7e07b5a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
13 changes: 11 additions & 2 deletions mixerclient/src/check_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace mixer_client {
void CheckCache::CacheElem::CacheElem::SetResponse(
const CheckResponse& response, Tick time_now) {
if (response.has_precondition()) {
status_ = ConvertRpcStatus(response.precondition().status());
status_ = parent_.ConvertRpcStatus(response.precondition().status());

if (response.precondition().has_valid_duration()) {
expire_time_ =
Expand Down Expand Up @@ -144,7 +144,7 @@ Status CheckCache::CacheResponse(const std::string& signature,
return lookup.value()->status();
}

CacheElem* cache_elem = new CacheElem(response, time_now);
CacheElem* cache_elem = new CacheElem(*this, response, time_now);
cache_->Insert(signature, cache_elem, 1);
return cache_elem->status();
}
Expand All @@ -160,5 +160,14 @@ Status CheckCache::FlushAll() {
return Status::OK;
}

Status CheckCache::ConvertRpcStatus(const ::google::rpc::Status& status) const {
// If server status code is INTERNAL, check network_fail_open flag.
if (status.code() == Code::INTERNAL && options_.network_fail_open) {
return Status::OK;
} else {
return Status(static_cast<Code>(status.code()), status.message());
}
}

} // namespace mixer_client
} // namespace istio
10 changes: 9 additions & 1 deletion mixerclient/src/check_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,15 @@ class CheckCache {
// Usually called at destructor.
::google::protobuf::util::Status FlushAll();

// Convert from grpc status to protobuf status.
::google::protobuf::util::Status ConvertRpcStatus(
const ::google::rpc::Status& status) const;

class CacheElem {
public:
CacheElem(const ::istio::mixer::v1::CheckResponse& response, Tick time) {
CacheElem(const CheckCache& parent,
const ::istio::mixer::v1::CheckResponse& response, Tick time)
: parent_(parent) {
SetResponse(response, time);
}

Expand All @@ -113,6 +119,8 @@ class CheckCache {
::google::protobuf::util::Status status() const { return status_; }

private:
// To the parent cache object.
const CheckCache& parent_;
// The check status for the last check request.
::google::protobuf::util::Status status_;
// Cache item should not be used after it is expired.
Expand Down
4 changes: 0 additions & 4 deletions mixerclient/utils/protobuf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ milliseconds ToMilliseonds(const Duration& duration) {
duration_cast<milliseconds>(nanoseconds(duration.nanos()));
}

Status ConvertRpcStatus(const ::google::rpc::Status& status) {
return Status(static_cast<Code>(status.code()), status.message());
}

bool InvalidDictionaryStatus(const Status& status) {
return status.error_code() == Code::INVALID_ARGUMENT &&
status.error_message().starts_with(kInvalidDictionaryErrorPrefix);
Expand Down
4 changes: 0 additions & 4 deletions mixerclient/utils/protobuf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ ::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);

bool InvalidDictionaryStatus(const ::google::protobuf::util::Status& status);

} // namespace mixer_client
Expand Down

0 comments on commit 7e07b5a

Please sign in to comment.