Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions contrib/endpoints/src/api_manager/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ cc_library(
"method_impl.cc",
"path_matcher.cc",
"path_matcher_node.cc",
"quota_control.cc",
"quota_control.h",
"request_handler.cc",
],
linkopts = select({
Expand Down
3 changes: 3 additions & 0 deletions contrib/endpoints/src/api_manager/check_workflow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "contrib/endpoints/src/api_manager/check_auth.h"
#include "contrib/endpoints/src/api_manager/check_service_control.h"
#include "contrib/endpoints/src/api_manager/fetch_metadata.h"
#include "contrib/endpoints/src/api_manager/quota_control.h"

using ::google::api_manager::utils::Status;

Expand All @@ -33,6 +34,8 @@ void CheckWorkflow::RegisterAll() {
Register(CheckAuth);
// Checks service control.
Register(CheckServiceControl);
// Quota control
Register(QuotaControl);
}

void CheckWorkflow::Register(CheckHandler handler) {
Expand Down
17 changes: 17 additions & 0 deletions contrib/endpoints/src/api_manager/context/request_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ void RequestContext::FillCheckRequestInfo(
info->allow_unregistered_calls = method()->allow_unregistered_calls();
}

void RequestContext::FillAllocateQuotaRequestInfo(
service_control::QuotaRequestInfo *info) {
FillOperationInfo(info);

info->client_ip = request_->GetClientIP();
info->method_name = this->method_call_.method_info->name();

info->labels["servicecontrol.googleapis.com/caller_ip"] =
request_->GetClientIP();
info->labels["servicecontrol.googleapis.com/referer"] = this->http_referer_;
info->labels["servicecontrol.googleapis.com/user"] = "integration_test_user";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see info->labels get used.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function was moved to Proto::FillAllocateQuotaRequest

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


// TODO(jaebong) need to set quota rule and metric rule
info->quota_rule_ = nullptr;
info->metric_rule_ = nullptr;
}

void RequestContext::FillReportRequestInfo(
Response *response, service_control::ReportRequestInfo *info) {
FillOperationInfo(info);
Expand Down
3 changes: 3 additions & 0 deletions contrib/endpoints/src/api_manager/context/request_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class RequestContext {
// Fill CheckRequestInfo
void FillCheckRequestInfo(service_control::CheckRequestInfo *info);

// Fill AllocateQuotaRequestInfo
void FillAllocateQuotaRequestInfo(service_control::QuotaRequestInfo *info);

// Fill ReportRequestInfo
void FillReportRequestInfo(Response *response,
service_control::ReportRequestInfo *info);
Expand Down
19 changes: 19 additions & 0 deletions contrib/endpoints/src/api_manager/proto/server_config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ message ServiceControlConfig {
// The intermediate reports for streaming calls should not be more frequent
// than this value (in seconds)
int32 intermediate_report_min_interval = 7;

// Quota aggregator config
QuotaAggregatorConfig quota_aggregator_config = 8;

// Timeout in milliseconds on service control allocate quota requests.
// If the value is <= 0, default timeout is 5000 milliseconds.
int32 quota_timeout_ms = 9;

}

// Check aggregator config
Expand All @@ -82,6 +90,17 @@ message CheckAggregatorConfig {
int32 response_expiration_ms = 3;
}

// Quota aggregator config
message QuotaAggregatorConfig {
// The maximum number of cache entries that can be kept in the aggregation
// cache. Cache is disabled when entries <= 0.
int32 cache_entries = 1;

// The maximum milliseconds before aggregated quota requests are refreshed to
// the server.
int32 refresh_interval_ms = 2;
}

// Report aggregator config
message ReportAggregatorConfig {
// The maximum number of cache entries that can be kept in the aggregation
Expand Down
72 changes: 72 additions & 0 deletions contrib/endpoints/src/api_manager/quota_control.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
//
#include <iostream>

#include "contrib/endpoints/src/api_manager/cloud_trace/cloud_trace.h"
#include "contrib/endpoints/src/api_manager/quota_control.h"
#include "google/protobuf/stubs/status.h"

using ::google::api_manager::utils::Status;
using ::google::protobuf::util::error::Code;

namespace google {
namespace api_manager {

void QuotaControl(std::shared_ptr<context::RequestContext> context,
std::function<void(Status status)> continuation) {
std::shared_ptr<cloud_trace::CloudTraceSpan> trace_span(
CreateSpan(context->cloud_trace(), "QuotaControl"));

if (!context->service_context()->service_control()) {
TRACE(trace_span) << "Service control check is not needed";
continuation(Status::OK);
return;
}

if (context->api_key().empty()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to call quota for all requests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the paragraph

if (context->method()->allow_unregistered_calls()) {
// Not need to call Check.
TRACE(trace_span) << "Service control check is not needed";
continuation(Status::OK);
return;
}

TRACE(trace_span) << "Failed at checking caller identity.";
continuation(
Status(Code::UNAUTHENTICATED,
"Method doesn't allow unregistered callers (callers without "
"established identity). Please use API Key or other form of "
"API consumer identity to call this API.",
Status::SERVICE_CONTROL));
return;
}

service_control::QuotaRequestInfo info;
context->FillAllocateQuotaRequestInfo(&info);
context->service_context()->service_control()->Quota(
info, trace_span.get(),
[context, continuation, trace_span](utils::Status status) {

TRACE(trace_span) << "Quota service control request returned with "
<< "status " << status.ToString();

continuation(status);
});
}

} // namespace service_control_client
} // namespace google
33 changes: 33 additions & 0 deletions contrib/endpoints/src/api_manager/quota_control.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
//
#ifndef API_MANAGER_QUOTA_CONTROL_H_
#define API_MANAGER_QUOTA_CONTROL_H_

#include "contrib/endpoints/include/api_manager/utils/status.h"
#include "contrib/endpoints/src/api_manager/context/request_context.h"

namespace google {
namespace api_manager {

// Call service control quota.
void QuotaControl(std::shared_ptr<context::RequestContext>,
std::function<void(utils::Status)>);

} // namespace api_manager
} // namespace google

#endif // API_MANAGER_QUOTA_CONTROL_H_
Loading