Skip to content

Commit

Permalink
Merge pull request envoyproxy#7 from qiwzhang/stream_api
Browse files Browse the repository at this point in the history
New transport with reader writer to support stream.
  • Loading branch information
qiwzhang authored Jan 17, 2017
2 parents 9c38034 + 2fe67d8 commit 5241c55
Show file tree
Hide file tree
Showing 7 changed files with 429 additions and 325 deletions.
30 changes: 16 additions & 14 deletions mixerclient/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,22 @@ licenses(["notice"])
load("@protobuf_git//:protobuf.bzl", "cc_proto_library")

cc_library(
name = "mixer_client_lib",
srcs = [
"src/client_impl.h",
"src/client_impl.cc",
],
hdrs = [
"include/client.h",
"include/options.h",
],
visibility = ["//visibility:public"],
deps = [
"//external:boringssl_crypto",
"//external:mixer_api_cc_proto",
],
name = "mixer_client_lib",
srcs = [
"src/client_impl.cc",
"src/client_impl.h",
"src/transport_impl.h",
],
hdrs = [
"include/client.h",
"include/options.h",
"include/transport.h",
],
visibility = ["//visibility:public"],
deps = [
"//external:boringssl_crypto",
"//external:mixer_api_cc_proto",
],
)

cc_library(
Expand Down
27 changes: 3 additions & 24 deletions mixerclient/include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "google/protobuf/stubs/status.h"
#include "mixer/api/v1/service.pb.h"
#include "options.h"
#include "transport.h"

namespace istio {
namespace mixer_client {
Expand All @@ -31,24 +32,6 @@ namespace mixer_client {
// is completed.
using DoneFunc = std::function<void(const ::google::protobuf::util::Status&)>;

// Defines a function prototype to make an asynchronous Check call to
// the mixer server.
using TransportCheckFunc = std::function<void(
const ::istio::mixer::v1::CheckRequest& request,
::istio::mixer::v1::CheckResponse* response, DoneFunc on_done)>;

// Defines a function prototype to make an asynchronous Report call to
// the mixer server.
using TransportReportFunc = std::function<void(
const ::istio::mixer::v1::ReportRequest& request,
::istio::mixer::v1::ReportResponse* response, DoneFunc on_done)>;

// Defines a function prototype to make an asynchronous Quota call to
// the mixer server.
using TransportQuotaFunc = std::function<void(
const ::istio::mixer::v1::QuotaRequest& request,
::istio::mixer::v1::QuotaResponse* response, DoneFunc on_done)>;

// Defines the options to create an instance of MixerClient interface.
struct MixerClientOptions {
// Default constructor with default values.
Expand All @@ -71,12 +54,8 @@ struct MixerClientOptions {
// Quota options.
QuotaOptions quota_options;

// Transport functions are used to send request to mixer server.
// It can be implemented many ways based on the environments.
// If not provided, the GRPC transport will be used.
TransportCheckFunc check_transport;
TransportReportFunc report_transport;
TransportQuotaFunc quota_transport;
// Transport object.
TransportInterface* transport;
};

class MixerClient {
Expand Down
84 changes: 84 additions & 0 deletions mixerclient/include/transport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/* 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 MIXERCLIENT_TRANSPORT_H
#define MIXERCLIENT_TRANSPORT_H

#include <functional>
#include <memory>
#include <string>

#include "google/protobuf/stubs/status.h"
#include "mixer/api/v1/service.pb.h"

namespace istio {
namespace mixer_client {

// A stream write interface implemented by transport layer
// It will be called by Mixer client.
template <class RequestType>
class WriteInterface {
public:
virtual ~WriteInterface() {}
// Write a request message.
virtual void Write(const RequestType &) = 0;
// Half close the write direction.
virtual void WriteDone() = 0;
// If true, write direction is closed.
virtual bool is_write_closed() = 0;
};

// A stream read interface implemented by Mixer client
// to receive response data, or stream close status.
// It will be called by the transport layer.
template <class ResponseType>
class ReadInterface {
public:
virtual ~ReadInterface() {}
// On receive a response message
virtual void OnRead(const ResponseType &) = 0;
// On stream close.
virtual void OnClose(const ::google::protobuf::util::Status &) = 0;
};

typedef std::unique_ptr<WriteInterface<::istio::mixer::v1::CheckRequest>>
CheckWriterPtr;
typedef std::unique_ptr<WriteInterface<::istio::mixer::v1::ReportRequest>>
ReportWriterPtr;
typedef std::unique_ptr<WriteInterface<::istio::mixer::v1::QuotaRequest>>
QuotaWriterPtr;

typedef ReadInterface<::istio::mixer::v1::CheckResponse> *CheckReaderRawPtr;
typedef ReadInterface<::istio::mixer::v1::ReportResponse> *ReportReaderRawPtr;
typedef ReadInterface<::istio::mixer::v1::QuotaResponse> *QuotaReaderRawPtr;

// This is the transport interface needed by Mixer client.
// The callers of the Mixer client need to implement this interface and
// pass it to the client.
class TransportInterface {
public:
virtual ~TransportInterface() {}
// Create a Check stream
virtual CheckWriterPtr NewStream(CheckReaderRawPtr) = 0;
// Create a Report stream
virtual ReportWriterPtr NewStream(ReportReaderRawPtr) = 0;
// Create a Quota stream
virtual QuotaWriterPtr NewStream(QuotaReaderRawPtr) = 0;
};

} // namespace mixer_client
} // namespace istio

#endif // MIXERCLIENT_TRANSPORT_H
43 changes: 14 additions & 29 deletions mixerclient/src/client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,27 @@ using ::google::protobuf::util::error::Code;
namespace istio {
namespace mixer_client {

MixerClientImpl::MixerClientImpl(MixerClientOptions &options)
: options_(options) {}
MixerClientImpl::MixerClientImpl(const MixerClientOptions &options)
: options_(options),
check_transport_(options_.transport),
report_transport_(options_.transport),
quota_transport_(options_.transport) {}

MixerClientImpl::~MixerClientImpl() {}

void MixerClientImpl::Check(const CheckRequest &check_request,
CheckResponse *check_response,
DoneFunc on_check_done) {
if (options_.check_transport == NULL) {
on_check_done(Status(Code::INVALID_ARGUMENT, "transport is NULL."));
return;
}

options_.check_transport(check_request, check_response, on_check_done);
void MixerClientImpl::Check(const CheckRequest &request,
CheckResponse *response, DoneFunc on_done) {
check_transport_.Call(request, response, on_done);
}

void MixerClientImpl::Report(const ReportRequest &report_request,
ReportResponse *report_response,
DoneFunc on_report_done) {
if (options_.report_transport == NULL) {
on_report_done(Status(Code::INVALID_ARGUMENT, "transport is NULL."));
return;
}

options_.report_transport(report_request, report_response, on_report_done);
void MixerClientImpl::Report(const ReportRequest &request,
ReportResponse *response, DoneFunc on_done) {
report_transport_.Call(request, response, on_done);
}

void MixerClientImpl::Quota(const QuotaRequest &quota_request,
QuotaResponse *quota_response,
DoneFunc on_quota_done) {
if (options_.quota_transport == NULL) {
on_quota_done(Status(Code::INVALID_ARGUMENT, "transport is NULL."));
return;
}

options_.quota_transport(quota_request, quota_response, on_quota_done);
void MixerClientImpl::Quota(const QuotaRequest &request,
QuotaResponse *response, DoneFunc on_done) {
quota_transport_.Call(request, response, on_done);
}

// Creates a MixerClient object.
Expand Down
24 changes: 17 additions & 7 deletions mixerclient/src/client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
#define MIXERCLIENT_CLIENT_IMPL_H

#include "include/client.h"
#include "src/transport_impl.h"

namespace istio {
namespace mixer_client {

class MixerClientImpl : public MixerClient {
public:
// Constructor
MixerClientImpl(MixerClientOptions &options);
MixerClientImpl(const MixerClientOptions& options);

// Destructor
virtual ~MixerClientImpl();
Expand All @@ -35,24 +36,33 @@ class MixerClientImpl : public MixerClient {
// check_response is returned from the Controller service.
//
// check_response must be alive until on_check_done is called.
virtual void Check(const ::istio::mixer::v1::CheckRequest &check_request,
::istio::mixer::v1::CheckResponse *check_response,
virtual void Check(const ::istio::mixer::v1::CheckRequest& check_request,
::istio::mixer::v1::CheckResponse* check_response,
DoneFunc on_check_done);

// This is async call. on_report_done is always called when the
// report request is finished.
virtual void Report(const ::istio::mixer::v1::ReportRequest &report_request,
::istio::mixer::v1::ReportResponse *report_response,
virtual void Report(const ::istio::mixer::v1::ReportRequest& report_request,
::istio::mixer::v1::ReportResponse* report_response,
DoneFunc on_report_done);

// This is async call. on_quota_done is always called when the
// quota request is finished.
virtual void Quota(const ::istio::mixer::v1::QuotaRequest &quota_request,
::istio::mixer::v1::QuotaResponse *quota_response,
virtual void Quota(const ::istio::mixer::v1::QuotaRequest& quota_request,
::istio::mixer::v1::QuotaResponse* quota_response,
DoneFunc on_quota_done);

private:
MixerClientOptions options_;
StreamTransport<::istio::mixer::v1::CheckRequest,
::istio::mixer::v1::CheckResponse>
check_transport_;
StreamTransport<::istio::mixer::v1::ReportRequest,
::istio::mixer::v1::ReportResponse>
report_transport_;
StreamTransport<::istio::mixer::v1::QuotaRequest,
::istio::mixer::v1::QuotaResponse>
quota_transport_;

GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MixerClientImpl);
};
Expand Down
Loading

0 comments on commit 5241c55

Please sign in to comment.