Skip to content

Commit

Permalink
Support FillProto for quota. (envoyproxy#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
qiwzhang committed Mar 21, 2017
1 parent c043b22 commit 370f4b7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
5 changes: 5 additions & 0 deletions mixerclient/include/attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ struct Attributes {
std::map<std::string, Value> 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

Expand Down
3 changes: 3 additions & 0 deletions mixerclient/src/attribute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
39 changes: 36 additions & 3 deletions mixerclient/src/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ class Transport : public AttributeConverter<RequestType> {
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;
Expand All @@ -52,6 +52,7 @@ class Transport : public AttributeConverter<RequestType> {
request->set_request_index(attribute_context_->IncRequestIndex());
}

private:
// A stream transport
StreamTransport<RequestType, ResponseType> stream_;
// Mutex to sync-up stream_.Call, only one at time.
Expand All @@ -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
Expand Down

0 comments on commit 370f4b7

Please sign in to comment.