From 370f4b743758bab23a8a4bc47493556428ceb99f Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Mon, 20 Mar 2017 17:35:38 -0700 Subject: [PATCH] Support FillProto for quota. (#44) --- mixerclient/include/attribute.h | 5 +++++ mixerclient/src/attribute.cc | 3 +++ mixerclient/src/transport.h | 39 ++++++++++++++++++++++++++++++--- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/mixerclient/include/attribute.h b/mixerclient/include/attribute.h index 3c42398397f1..bf7bb54da1eb 100644 --- a/mixerclient/include/attribute.h +++ b/mixerclient/include/attribute.h @@ -77,6 +77,11 @@ struct Attributes { std::map attributes; }; +// The attribute key to fill "quota" field in the QuotaRequest. +extern const std::string kQuotaName; +// The attribute key to fill "amount" field in the QuotaRequest. +extern const std::string kQuotaAmount; + } // namespace mixer_client } // namespace istio diff --git a/mixerclient/src/attribute.cc b/mixerclient/src/attribute.cc index 0166369b6ea5..471f12bff92c 100644 --- a/mixerclient/src/attribute.cc +++ b/mixerclient/src/attribute.cc @@ -19,6 +19,9 @@ namespace istio { namespace mixer_client { +const std::string kQuotaName = "quota.name"; +const std::string kQuotaAmount = "quota.amount"; + Attributes::Value Attributes::StringValue(const std::string& str) { Attributes::Value v; v.type = Attributes::Value::STRING; diff --git a/mixerclient/src/transport.h b/mixerclient/src/transport.h index 2b5b697277f4..172567e37b2b 100644 --- a/mixerclient/src/transport.h +++ b/mixerclient/src/transport.h @@ -38,11 +38,11 @@ class Transport : public AttributeConverter { stream_.Call(attributes, response, on_done); } - private: + protected: // Convert to a protobuf // This is called by stream_.Call so it is within the mutex lock. void FillProto(StreamID stream_id, const Attributes& attributes, - RequestType* request) { + RequestType* request) override { if (stream_id != last_stream_id_) { attribute_context_.reset(new AttributeContext); last_stream_id_ = stream_id; @@ -52,6 +52,7 @@ class Transport : public AttributeConverter { request->set_request_index(attribute_context_->IncRequestIndex()); } + private: // A stream transport StreamTransport stream_; // Mutex to sync-up stream_.Call, only one at time. @@ -70,7 +71,39 @@ typedef Transport<::istio::mixer::v1::ReportRequest, ReportTransport; typedef Transport<::istio::mixer::v1::QuotaRequest, ::istio::mixer::v1::QuotaResponse> - QuotaTransport; + QuotaBaseTransport; + +// FillProto for Quota needs to be handled differently. +// 1) convert "quota.name" and "quota.amount" to proto fields. +// 2) set deduplication_id and best_effort +class QuotaTransport : public QuotaBaseTransport { + public: + QuotaTransport(TransportInterface* transport) + : QuotaBaseTransport(transport), deduplication_id_(0) {} + + private: + void FillProto(StreamID stream_id, const Attributes& attributes, + ::istio::mixer::v1::QuotaRequest* request) override { + Attributes filtered_attributes; + for (const auto& it : attributes.attributes) { + if (it.first == kQuotaName && + it.second.type == Attributes::Value::STRING) { + request->set_quota(it.second.str_v); + } else if (it.first == kQuotaAmount && + it.second.type == Attributes::Value::INT64) { + request->set_amount(it.second.value.int64_v); + } else { + filtered_attributes.attributes[it.first] = it.second; + } + } + request->set_deduplication_id(std::to_string(deduplication_id_++)); + request->set_best_effort(false); + + QuotaBaseTransport::FillProto(stream_id, filtered_attributes, request); + } + + int64_t deduplication_id_; +}; } // namespace mixer_client } // namespace istio