Skip to content

Commit 3a0ec2c

Browse files
authored
Define interface for client. (istio#2)
* Define interface for client. * Remove per_request transport. * Add quota interface. * Update copyright headers. * Update namespace from google to istio.
1 parent d516112 commit 3a0ec2c

File tree

2 files changed

+238
-0
lines changed

2 files changed

+238
-0
lines changed

mixerclient/include/client.h

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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_CLIENT_H
17+
#define MIXERCLIENT_CLIENT_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+
#include "options.h"
26+
27+
namespace istio {
28+
namespace mixer_client {
29+
30+
// Defines a function prototype used when an asynchronous transport call
31+
// is completed.
32+
using DoneFunc = std::function<void(const ::google::protobuf::util::Status&)>;
33+
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+
52+
// Defines the options to create an instance of MixerClient interface.
53+
struct MixerClientOptions {
54+
// Default constructor with default values.
55+
MixerClientOptions() {}
56+
57+
// Constructor with specified option values.
58+
MixerClientOptions(const CheckOptions& check_options,
59+
const ReportOptions& report_options,
60+
const QuotaOptions& quota_options)
61+
: check_options(check_options),
62+
report_options(report_options),
63+
quota_options(quota_options) {}
64+
65+
// Check options.
66+
CheckOptions check_options;
67+
68+
// Report options.
69+
ReportOptions report_options;
70+
71+
// Quota options.
72+
QuotaOptions quota_options;
73+
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;
80+
};
81+
82+
class MixerClient {
83+
public:
84+
// Destructor
85+
virtual ~MixerClient() {}
86+
87+
// The async call.
88+
// on_check_done is called with the check status after cached
89+
// check_response is returned in case of cache hit, otherwise called after
90+
// check_response is returned from the Controller service.
91+
//
92+
// check_response must be alive until on_check_done is called.
93+
virtual void Check(const ::istio::mixer::v1::CheckRequest& check_request,
94+
::istio::mixer::v1::CheckResponse* check_response,
95+
DoneFunc on_check_done) = 0;
96+
97+
// This is async call. on_report_done is always called when the
98+
// report request is finished.
99+
virtual void Report(const ::istio::mixer::v1::ReportRequest& report_request,
100+
::istio::mixer::v1::ReportResponse* report_response,
101+
DoneFunc on_report_done) = 0;
102+
103+
// This is async call. on_quota_done is always called when the
104+
// quota request is finished.
105+
virtual void Quota(const ::istio::mixer::v1::QuotaRequest& quota_request,
106+
::istio::mixer::v1::QuotaResponse* quota_response,
107+
DoneFunc on_quota_done) = 0;
108+
};
109+
110+
// Creates a MixerClient object.
111+
std::unique_ptr<MixerClient> CreateMixerClient(MixerClientOptions& options);
112+
113+
} // namespace mixer_client
114+
} // namespace istio
115+
116+
#endif // MIXERCLIENT_CLIENT_H

mixerclient/include/options.h

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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_OPTIONS_H
17+
#define MIXERCLIENT_OPTIONS_H
18+
19+
#include <memory>
20+
21+
namespace istio {
22+
namespace mixer_client {
23+
24+
// Options controlling check behavior.
25+
struct CheckOptions {
26+
// Default constructor.
27+
// Default options are chosen from experience.
28+
CheckOptions()
29+
: num_entries(10000), flush_interval_ms(500), expiration_ms(1000) {}
30+
31+
// Constructor.
32+
// cache_entries is the maximum number of cache entries that can be kept in
33+
// the cache. Cache is disabled when cache_entries <= 0.
34+
// flush_cache_entry_interval_ms is the maximum milliseconds before an
35+
// check request needs to send to remote server again.
36+
// response_expiration_ms is the maximum milliseconds before a cached check
37+
// response is invalidated. We make sure that it is at least
38+
// flush_cache_entry_interval_ms + 1.
39+
CheckOptions(int cache_entries, int flush_cache_entry_interval_ms,
40+
int response_expiration_ms)
41+
: num_entries(cache_entries),
42+
flush_interval_ms(flush_cache_entry_interval_ms),
43+
expiration_ms(std::max(flush_cache_entry_interval_ms + 1,
44+
response_expiration_ms)) {}
45+
46+
// Maximum number of cache entries kept in the cache.
47+
// Set to 0 will disable caching.
48+
const int num_entries;
49+
50+
// Maximum milliseconds before check requests are flushed to the
51+
// server. The flush is triggered by a check request.
52+
const int flush_interval_ms;
53+
54+
// Maximum milliseconds before a cached check response should be deleted. The
55+
// deletion is triggered by a timer. This value must be larger than
56+
// flush_interval_ms.
57+
const int expiration_ms;
58+
};
59+
60+
// Options controlling report behavior.
61+
struct ReportOptions {
62+
// Default constructor.
63+
ReportOptions() : num_entries(10000), flush_interval_ms(1000) {}
64+
65+
// Constructor.
66+
// cache_entries is the maximum number of cache entries that can be kept in
67+
// the cache. Cache is disabled when cache_entries <= 0.
68+
// flush_cache_entry_interval_ms is the maximum milliseconds before
69+
// report requests are flushed to the server. The cache entry is deleted after
70+
// the flush.
71+
ReportOptions(int cache_entries, int flush_cache_entry_interval_ms)
72+
: num_entries(cache_entries),
73+
flush_interval_ms(flush_cache_entry_interval_ms) {}
74+
75+
// Maximum number of cache entries kept in the cache.
76+
// Set to 0 will disable caching.
77+
const int num_entries;
78+
79+
// Maximum milliseconds before report requests are flushed to the
80+
// server. The flush is triggered by a timer.
81+
const int flush_interval_ms;
82+
};
83+
84+
// Options controlling quota behavior.
85+
struct QuotaOptions {
86+
// Default constructor.
87+
QuotaOptions()
88+
: num_entries(10000), flush_interval_ms(500), expiration_ms(1000) {}
89+
90+
// Constructor.
91+
// cache_entries is the maximum number of cache entries that can be kept in
92+
// the cache. Cache is disabled when cache_entries <= 0.
93+
// flush_cache_entry_interval_ms is the maximum milliseconds before an
94+
// quota request needs to send to remote server again.
95+
// response_expiration_ms is the maximum milliseconds before a cached quota
96+
// response is invalidated. We make sure that it is at least
97+
// flush_cache_entry_interval_ms + 1.
98+
QuotaOptions(int cache_entries, int flush_cache_entry_interval_ms,
99+
int response_expiration_ms)
100+
: num_entries(cache_entries),
101+
flush_interval_ms(flush_cache_entry_interval_ms),
102+
expiration_ms(std::max(flush_cache_entry_interval_ms + 1,
103+
response_expiration_ms)) {}
104+
105+
// Maximum number of cache entries kept in the cache.
106+
// Set to 0 will disable caching.
107+
const int num_entries;
108+
109+
// Maximum milliseconds before quota requests are flushed to the
110+
// server. The flush is triggered by a quota request.
111+
const int flush_interval_ms;
112+
113+
// Maximum milliseconds before a cached quota response should be deleted. The
114+
// deletion is triggered by a timer. This value must be larger than
115+
// flush_interval_ms.
116+
const int expiration_ms;
117+
};
118+
119+
} // namespace mixer_client
120+
} // namespace istio
121+
122+
#endif // MIXERCLIENT_OPTIONS_H

0 commit comments

Comments
 (0)