Skip to content

Commit 5241c55

Browse files
authored
Merge pull request envoyproxy#7 from qiwzhang/stream_api
New transport with reader writer to support stream.
2 parents 9c38034 + 2fe67d8 commit 5241c55

File tree

7 files changed

+429
-325
lines changed

7 files changed

+429
-325
lines changed

mixerclient/BUILD

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ licenses(["notice"])
1717
load("@protobuf_git//:protobuf.bzl", "cc_proto_library")
1818

1919
cc_library(
20-
name = "mixer_client_lib",
21-
srcs = [
22-
"src/client_impl.h",
23-
"src/client_impl.cc",
24-
],
25-
hdrs = [
26-
"include/client.h",
27-
"include/options.h",
28-
],
29-
visibility = ["//visibility:public"],
30-
deps = [
31-
"//external:boringssl_crypto",
32-
"//external:mixer_api_cc_proto",
33-
],
20+
name = "mixer_client_lib",
21+
srcs = [
22+
"src/client_impl.cc",
23+
"src/client_impl.h",
24+
"src/transport_impl.h",
25+
],
26+
hdrs = [
27+
"include/client.h",
28+
"include/options.h",
29+
"include/transport.h",
30+
],
31+
visibility = ["//visibility:public"],
32+
deps = [
33+
"//external:boringssl_crypto",
34+
"//external:mixer_api_cc_proto",
35+
],
3436
)
3537

3638
cc_library(

mixerclient/include/client.h

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "google/protobuf/stubs/status.h"
2424
#include "mixer/api/v1/service.pb.h"
2525
#include "options.h"
26+
#include "transport.h"
2627

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

34-
// Defines a function prototype to make an asynchronous Check call to
35-
// the mixer server.
36-
using TransportCheckFunc = std::function<void(
37-
const ::istio::mixer::v1::CheckRequest& request,
38-
::istio::mixer::v1::CheckResponse* response, DoneFunc on_done)>;
39-
40-
// Defines a function prototype to make an asynchronous Report call to
41-
// the mixer server.
42-
using TransportReportFunc = std::function<void(
43-
const ::istio::mixer::v1::ReportRequest& request,
44-
::istio::mixer::v1::ReportResponse* response, DoneFunc on_done)>;
45-
46-
// Defines a function prototype to make an asynchronous Quota call to
47-
// the mixer server.
48-
using TransportQuotaFunc = std::function<void(
49-
const ::istio::mixer::v1::QuotaRequest& request,
50-
::istio::mixer::v1::QuotaResponse* response, DoneFunc on_done)>;
51-
5235
// Defines the options to create an instance of MixerClient interface.
5336
struct MixerClientOptions {
5437
// Default constructor with default values.
@@ -71,12 +54,8 @@ struct MixerClientOptions {
7154
// Quota options.
7255
QuotaOptions quota_options;
7356

74-
// Transport functions are used to send request to mixer server.
75-
// It can be implemented many ways based on the environments.
76-
// If not provided, the GRPC transport will be used.
77-
TransportCheckFunc check_transport;
78-
TransportReportFunc report_transport;
79-
TransportQuotaFunc quota_transport;
57+
// Transport object.
58+
TransportInterface* transport;
8059
};
8160

8261
class MixerClient {

mixerclient/include/transport.h

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* Copyright 2017 Google Inc. All Rights Reserved.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#ifndef MIXERCLIENT_TRANSPORT_H
17+
#define MIXERCLIENT_TRANSPORT_H
18+
19+
#include <functional>
20+
#include <memory>
21+
#include <string>
22+
23+
#include "google/protobuf/stubs/status.h"
24+
#include "mixer/api/v1/service.pb.h"
25+
26+
namespace istio {
27+
namespace mixer_client {
28+
29+
// A stream write interface implemented by transport layer
30+
// It will be called by Mixer client.
31+
template <class RequestType>
32+
class WriteInterface {
33+
public:
34+
virtual ~WriteInterface() {}
35+
// Write a request message.
36+
virtual void Write(const RequestType &) = 0;
37+
// Half close the write direction.
38+
virtual void WriteDone() = 0;
39+
// If true, write direction is closed.
40+
virtual bool is_write_closed() = 0;
41+
};
42+
43+
// A stream read interface implemented by Mixer client
44+
// to receive response data, or stream close status.
45+
// It will be called by the transport layer.
46+
template <class ResponseType>
47+
class ReadInterface {
48+
public:
49+
virtual ~ReadInterface() {}
50+
// On receive a response message
51+
virtual void OnRead(const ResponseType &) = 0;
52+
// On stream close.
53+
virtual void OnClose(const ::google::protobuf::util::Status &) = 0;
54+
};
55+
56+
typedef std::unique_ptr<WriteInterface<::istio::mixer::v1::CheckRequest>>
57+
CheckWriterPtr;
58+
typedef std::unique_ptr<WriteInterface<::istio::mixer::v1::ReportRequest>>
59+
ReportWriterPtr;
60+
typedef std::unique_ptr<WriteInterface<::istio::mixer::v1::QuotaRequest>>
61+
QuotaWriterPtr;
62+
63+
typedef ReadInterface<::istio::mixer::v1::CheckResponse> *CheckReaderRawPtr;
64+
typedef ReadInterface<::istio::mixer::v1::ReportResponse> *ReportReaderRawPtr;
65+
typedef ReadInterface<::istio::mixer::v1::QuotaResponse> *QuotaReaderRawPtr;
66+
67+
// This is the transport interface needed by Mixer client.
68+
// The callers of the Mixer client need to implement this interface and
69+
// pass it to the client.
70+
class TransportInterface {
71+
public:
72+
virtual ~TransportInterface() {}
73+
// Create a Check stream
74+
virtual CheckWriterPtr NewStream(CheckReaderRawPtr) = 0;
75+
// Create a Report stream
76+
virtual ReportWriterPtr NewStream(ReportReaderRawPtr) = 0;
77+
// Create a Quota stream
78+
virtual QuotaWriterPtr NewStream(QuotaReaderRawPtr) = 0;
79+
};
80+
81+
} // namespace mixer_client
82+
} // namespace istio
83+
84+
#endif // MIXERCLIENT_TRANSPORT_H

mixerclient/src/client_impl.cc

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,42 +28,27 @@ using ::google::protobuf::util::error::Code;
2828
namespace istio {
2929
namespace mixer_client {
3030

31-
MixerClientImpl::MixerClientImpl(MixerClientOptions &options)
32-
: options_(options) {}
31+
MixerClientImpl::MixerClientImpl(const MixerClientOptions &options)
32+
: options_(options),
33+
check_transport_(options_.transport),
34+
report_transport_(options_.transport),
35+
quota_transport_(options_.transport) {}
3336

3437
MixerClientImpl::~MixerClientImpl() {}
3538

36-
void MixerClientImpl::Check(const CheckRequest &check_request,
37-
CheckResponse *check_response,
38-
DoneFunc on_check_done) {
39-
if (options_.check_transport == NULL) {
40-
on_check_done(Status(Code::INVALID_ARGUMENT, "transport is NULL."));
41-
return;
42-
}
43-
44-
options_.check_transport(check_request, check_response, on_check_done);
39+
void MixerClientImpl::Check(const CheckRequest &request,
40+
CheckResponse *response, DoneFunc on_done) {
41+
check_transport_.Call(request, response, on_done);
4542
}
4643

47-
void MixerClientImpl::Report(const ReportRequest &report_request,
48-
ReportResponse *report_response,
49-
DoneFunc on_report_done) {
50-
if (options_.report_transport == NULL) {
51-
on_report_done(Status(Code::INVALID_ARGUMENT, "transport is NULL."));
52-
return;
53-
}
54-
55-
options_.report_transport(report_request, report_response, on_report_done);
44+
void MixerClientImpl::Report(const ReportRequest &request,
45+
ReportResponse *response, DoneFunc on_done) {
46+
report_transport_.Call(request, response, on_done);
5647
}
5748

58-
void MixerClientImpl::Quota(const QuotaRequest &quota_request,
59-
QuotaResponse *quota_response,
60-
DoneFunc on_quota_done) {
61-
if (options_.quota_transport == NULL) {
62-
on_quota_done(Status(Code::INVALID_ARGUMENT, "transport is NULL."));
63-
return;
64-
}
65-
66-
options_.quota_transport(quota_request, quota_response, on_quota_done);
49+
void MixerClientImpl::Quota(const QuotaRequest &request,
50+
QuotaResponse *response, DoneFunc on_done) {
51+
quota_transport_.Call(request, response, on_done);
6752
}
6853

6954
// Creates a MixerClient object.

mixerclient/src/client_impl.h

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
#define MIXERCLIENT_CLIENT_IMPL_H
1818

1919
#include "include/client.h"
20+
#include "src/transport_impl.h"
2021

2122
namespace istio {
2223
namespace mixer_client {
2324

2425
class MixerClientImpl : public MixerClient {
2526
public:
2627
// Constructor
27-
MixerClientImpl(MixerClientOptions &options);
28+
MixerClientImpl(const MixerClientOptions& options);
2829

2930
// Destructor
3031
virtual ~MixerClientImpl();
@@ -35,24 +36,33 @@ class MixerClientImpl : public MixerClient {
3536
// check_response is returned from the Controller service.
3637
//
3738
// check_response must be alive until on_check_done is called.
38-
virtual void Check(const ::istio::mixer::v1::CheckRequest &check_request,
39-
::istio::mixer::v1::CheckResponse *check_response,
39+
virtual void Check(const ::istio::mixer::v1::CheckRequest& check_request,
40+
::istio::mixer::v1::CheckResponse* check_response,
4041
DoneFunc on_check_done);
4142

4243
// This is async call. on_report_done is always called when the
4344
// report request is finished.
44-
virtual void Report(const ::istio::mixer::v1::ReportRequest &report_request,
45-
::istio::mixer::v1::ReportResponse *report_response,
45+
virtual void Report(const ::istio::mixer::v1::ReportRequest& report_request,
46+
::istio::mixer::v1::ReportResponse* report_response,
4647
DoneFunc on_report_done);
4748

4849
// This is async call. on_quota_done is always called when the
4950
// quota request is finished.
50-
virtual void Quota(const ::istio::mixer::v1::QuotaRequest &quota_request,
51-
::istio::mixer::v1::QuotaResponse *quota_response,
51+
virtual void Quota(const ::istio::mixer::v1::QuotaRequest& quota_request,
52+
::istio::mixer::v1::QuotaResponse* quota_response,
5253
DoneFunc on_quota_done);
5354

5455
private:
5556
MixerClientOptions options_;
57+
StreamTransport<::istio::mixer::v1::CheckRequest,
58+
::istio::mixer::v1::CheckResponse>
59+
check_transport_;
60+
StreamTransport<::istio::mixer::v1::ReportRequest,
61+
::istio::mixer::v1::ReportResponse>
62+
report_transport_;
63+
StreamTransport<::istio::mixer::v1::QuotaRequest,
64+
::istio::mixer::v1::QuotaResponse>
65+
quota_transport_;
5666

5767
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MixerClientImpl);
5868
};

0 commit comments

Comments
 (0)