Skip to content

Commit

Permalink
make transport cancellable (envoyproxy#102)
Browse files Browse the repository at this point in the history
* make transport cancellable
  • Loading branch information
lizan authored Aug 25, 2017
1 parent 65197e3 commit fb3e1b7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 35 deletions.
11 changes: 7 additions & 4 deletions mixerclient/include/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ namespace mixer_client {
// Uses UNAVAILABLE status code to indicate network failure.
using DoneFunc = std::function<void(const ::google::protobuf::util::Status&)>;

// Defines a function prototype used to cancel an asynchronous transport call.
using CancelFunc = std::function<void()>;

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

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

Expand Down Expand Up @@ -91,8 +94,8 @@ class MixerClient {
// The response data from mixer will be consumed by mixer client.

// A check call.
virtual void Check(const Attributes& attributes, TransportCheckFunc transport,
DoneFunc on_done) = 0;
virtual CancelFunc Check(const Attributes& attributes,
TransportCheckFunc transport, DoneFunc on_done) = 0;

// A report call.
virtual void Report(const Attributes& attributes) = 0;
Expand Down
47 changes: 24 additions & 23 deletions mixerclient/src/client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,15 @@ MixerClientImpl::MixerClientImpl(const MixerClientOptions &options)

MixerClientImpl::~MixerClientImpl() {}

void MixerClientImpl::Check(const Attributes &attributes,
TransportCheckFunc transport, DoneFunc on_done) {
CancelFunc MixerClientImpl::Check(const Attributes &attributes,
TransportCheckFunc transport,
DoneFunc on_done) {
std::unique_ptr<CheckCache::CheckResult> check_result(
new CheckCache::CheckResult);
check_cache_->Check(attributes, check_result.get());
if (check_result->IsCacheHit() && !check_result->status().ok()) {
on_done(check_result->status());
return;
return nullptr;
}

std::unique_ptr<QuotaCache::CheckResult> quota_result(
Expand All @@ -66,7 +67,7 @@ void MixerClientImpl::Check(const Attributes &attributes,
on_done(quota_result->status());
on_done = nullptr;
if (!quota_call) {
return;
return nullptr;
}
}

Expand All @@ -82,26 +83,26 @@ void MixerClientImpl::Check(const Attributes &attributes,
if (!transport) {
transport = options_.check_transport;
}
transport(request, response,
[this, response, raw_check_result, raw_quota_result,
on_done](const Status &status) {
raw_check_result->SetResponse(status, *response);
raw_quota_result->SetResponse(status, *response);
if (on_done) {
if (!raw_check_result->status().ok()) {
on_done(raw_check_result->status());
} else {
on_done(raw_quota_result->status());
}
}
delete raw_check_result;
delete raw_quota_result;
delete response;
return transport(request, response,
[this, response, raw_check_result, raw_quota_result,
on_done](const Status &status) {
raw_check_result->SetResponse(status, *response);
raw_quota_result->SetResponse(status, *response);
if (on_done) {
if (!raw_check_result->status().ok()) {
on_done(raw_check_result->status());
} else {
on_done(raw_quota_result->status());
}
}
delete raw_check_result;
delete raw_quota_result;
delete response;

if (InvalidDictionaryStatus(status)) {
converter_.ShrinkGlobalDictionary();
}
});
if (InvalidDictionaryStatus(status)) {
converter_.ShrinkGlobalDictionary();
}
});
}

void MixerClientImpl::Report(const Attributes &attributes) {
Expand Down
4 changes: 2 additions & 2 deletions mixerclient/src/client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class MixerClientImpl : public MixerClient {
// Destructor
virtual ~MixerClientImpl();

virtual void Check(const Attributes& attributes, TransportCheckFunc transport,
DoneFunc on_done);
virtual CancelFunc Check(const Attributes& attributes,
TransportCheckFunc transport, DoneFunc on_done);
virtual void Report(const Attributes& attributes);

private:
Expand Down
8 changes: 5 additions & 3 deletions mixerclient/src/client_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ class MockCheckTransport {
public:
MOCK_METHOD3(Check, void(const CheckRequest&, CheckResponse*, DoneFunc));
TransportCheckFunc GetFunc() {
return
[this](const CheckRequest& request, CheckResponse* response,
DoneFunc on_done) { this->Check(request, response, on_done); };
return [this](const CheckRequest& request, CheckResponse* response,
DoneFunc on_done) -> CancelFunc {
Check(request, response, on_done);
return nullptr;
};
}
};

Expand Down
8 changes: 5 additions & 3 deletions mixerclient/src/report_batch_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class MockReportTransport {
public:
MOCK_METHOD3(Report, void(const ReportRequest&, ReportResponse*, DoneFunc));
TransportReportFunc GetFunc() {
return
[this](const ReportRequest& request, ReportResponse* response,
DoneFunc on_done) { this->Report(request, response, on_done); };
return [this](const ReportRequest& request, ReportResponse* response,
DoneFunc on_done) -> CancelFunc {
Report(request, response, on_done);
return nullptr;
};
}
};

Expand Down

0 comments on commit fb3e1b7

Please sign in to comment.