Skip to content

Commit

Permalink
Invoke ClientInterceptors for future and semifuture interaction methods
Browse files Browse the repository at this point in the history
Summary: This method implements ClientInterceptor support for `future_*` and `semifuture_*` interaction methods (other variants are already supported but previously untested). See added unit tests.

Reviewed By: sazonovkirill

Differential Revision: D62657751

fbshipit-source-id: f8caa8f6e192ff1482a63c3d437da027b55b8ba2
  • Loading branch information
praihan authored and facebook-github-bot committed Sep 19, 2024
1 parent f304c34 commit 8e550d7
Show file tree
Hide file tree
Showing 20 changed files with 351 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ folly::coro::Task<{{ > types/return_type_client}}> {{ > service_common/client_cl
auto wrappedCallback = apache::thrift::createSinkClientCallback(
apache::thrift::RequestClientCallback::Ptr(apache::thrift::RequestClientCallback::Ptr(cancellableCallback ? (apache::thrift::RequestClientCallback*)cancellableCallback.get() : &callback)));
{{#function:creates_interaction?}}
{{function:created_interaction}} handle(channel_, "{{function:created_interaction}}");
{{function:created_interaction}} interactionHandle(channel_, "{{function:created_interaction}}", interceptors_);
{{/function:creates_interaction?}}

fbthrift_serialize_and_send_{{function:cpp_name}}(rpcOptions, std::move(header), ctx.get(), wrappedCallback{{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
fbthrift_serialize_and_send_{{function:cpp_name}}(rpcOptions, std::move(header), ctx.get(), wrappedCallback{{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});

if (cancellable) {
folly::CancellationCallback cb(cancelToken, [&] { CancellableCallback::cancel(std::move(cancellableCallback)); });
Expand All @@ -55,7 +55,7 @@ folly::coro::Task<{{ > types/return_type_client}}> {{ > service_common/client_cl
{{/function:creates_interaction?}}
{{#function:creates_interaction?}}
co_return std::make_pair(
std::move(handle),
std::move(interactionHandle),
recv_{{function:cpp_name}}(returnState)
);
{{/function:creates_interaction?}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,33 +39,30 @@ folly::SemiFuture<{{ > types/return_type_client_lift_unit}}> {{ > service_common
auto wrappedCallback = std::move(wrappedCallbackAndContextStack.first);
{{/function:stream?}}
{{#function:creates_interaction?}}
{{function:created_interaction}} interactionHandle(channel_, "{{function:created_interaction}}");
{{function:created_interaction}} interactionHandle(channel_, "{{function:created_interaction}}", interceptors_);
{{/function:creates_interaction?}}
{{^function:creates_interaction?}}
if (contextStack != nullptr) {
if (auto exTry = contextStack->processClientInterceptorsOnRequest(); exTry.hasException()) {
return folly::makeSemiFuture<{{ > types/return_type_client_no_handle_lift_unit}}>(std::move(exTry).exception());
return folly::makeSemiFuture<{{ > types/return_type_client_lift_unit}}>(std::move(exTry).exception());
}
}
{{/function:creates_interaction?}}
apache::thrift::SerializedRequest request = fbthrift_serialize_{{function:cpp_name}}(rpcOptions, *header, contextStack{{function:comma}}{{ > service_common/param_list}});
fbthrift_send_{{function:cpp_name}}(std::move(request), rpcOptions, std::move(header), std::move(wrappedCallback){{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}});
{{^function:creates_interaction?}}
{{#unless function:creates_interaction?}}
return std::move(semifuture).deferValue(CallbackHelper::processClientInterceptorsAndExtractResult);
{{/function:creates_interaction?}}
{{#function:creates_interaction?}}
{{else}}
return std::move(semifuture)
.deferValue(
[interactionHandle = std::move(interactionHandle)](CallbackHelper::PromiseResult&& result) mutable {
{{^type:void?}}
return std::pair{std::move(interactionHandle), CallbackHelper::extractResult(std::move(result))};
{{/type:void?}}
{{#type:void?}}
std::ignore = CallbackHelper::extractResult(std::move(result));
auto returnValue = CallbackHelper::processClientInterceptorsAndExtractResult(std::move(result));
returnValue.throwUnlessValue();
{{#unless type:void?}}
return std::pair{std::move(interactionHandle), std::move(*returnValue)};
{{else}}
return std::move(interactionHandle);
{{/type:void?}}
{{/unless}}
});
{{/function:creates_interaction?}}
{{/unless}}
}

{{^service:reduced_client?}}{{^function:creates_interaction?}}
Expand Down Expand Up @@ -112,10 +109,10 @@ std::pair<
folly::SemiFuture<{{ > types/return_type_client_no_handle_lift_unit}}>
> {{ > service_common/client_class_name}}::eager_semifuture_{{function:cpp_name}}(apache::thrift::RpcOptions& rpcOptions{{function:comma}}{{ > service_common/function_param_list_client}}) {
auto callbackAndFuture = makeSemiFutureCallback(recv_wrapped_{{function:cpp_name}}, channel_);
{{function:created_interaction}} handle(channel_, "{{function:created_interaction}}");
{{function:created_interaction}} interactionHandle(channel_, "{{function:created_interaction}}", interceptors_);
auto callback = std::move(callbackAndFuture.first);
{{function:cpp_name}}(rpcOptions, std::move(callback){{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
return std::make_pair(std::move(handle), std::move(callbackAndFuture.second));
{{function:cpp_name}}(rpcOptions, std::move(callback){{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
return std::make_pair(std::move(interactionHandle), std::move(callbackAndFuture.second));
}

{{/function:creates_interaction?}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ void {{ > service_common/client_class_name}}::sync_{{function:cpp_name}}(apache:
rpcOptions.getBufferOptions());
{{/unless}}
{{#if function:creates_interaction?}}
{{function:created_interaction}} handle(channel_, "{{function:created_interaction}}");
{{function:created_interaction}} interactionHandle(channel_, "{{function:created_interaction}}", interceptors_);
{{/if}}
auto* contextStack = ctxAndHeader.first.get();
if (contextStack != nullptr) {
Expand All @@ -59,7 +59,7 @@ void {{ > service_common/client_class_name}}::sync_{{function:cpp_name}}(apache:
callback.waitUntilDone(
evb,
[&] {
fbthrift_serialize_and_send_{{function:cpp_name}}(rpcOptions, std::move(ctxAndHeader.second), ctxAndHeader.first.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
fbthrift_serialize_and_send_{{function:cpp_name}}(rpcOptions, std::move(ctxAndHeader.second), ctxAndHeader.first.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
});
if (contextStack != nullptr) {
contextStack->processClientInterceptorsOnResponse().throwUnlessValue();
Expand All @@ -85,12 +85,12 @@ void {{ > service_common/client_class_name}}::sync_{{function:cpp_name}}(apache:
{{else}}
{{#unless type:void?}}
return std::make_pair(
std::move(handle),
std::move(interactionHandle),
recv_{{function:cpp_name}}(returnState)
);
{{else}}
recv_{{function:cpp_name}}(returnState);
return std::move(handle);
return std::move(interactionHandle);
{{/unless}}
{{/unless}}
{{/if}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void apache::thrift::Client<{{service:qualified_name}}>::{{function:cpp_name}}(f
{{/function:oneway?}}
{{/function:starts_interaction?}}{{#function:starts_interaction?}}
apache::thrift::Client<{{service:parent_service_qualified_name}}>::{{type:name}} apache::thrift::Client<{{service:qualified_name}}>::{{function:cpp_name}}() {
return {{type:name}}(channel_, "{{type:name}}");
return {{type:name}}(channel_, "{{type:name}}", interceptors_);
}
{{/function:starts_interaction?}}
{{/function:return_type}}{{/service:functions}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,15 @@
hasRpcOptions ? rpcOptions->getBufferOptions() : defaultRpcOptions->getBufferOptions());
{{/function:stream?}}
{{#function:creates_interaction?}}
{{function:created_interaction}} handle(channel_, "{{function:created_interaction}}");
{{function:created_interaction}} interactionHandle(channel_, "{{function:created_interaction}}", ctx ? ctx->getClientInterceptors() : nullptr);
{{/function:creates_interaction?}}
if (ctx != nullptr) {
ctx->processClientInterceptorsOnRequest().throwUnlessValue();
}
if constexpr (hasRpcOptions) {
fbthrift_serialize_and_send_{{function:cpp_name}}(*rpcOptions, std::move(header), ctx.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
fbthrift_serialize_and_send_{{function:cpp_name}}(*rpcOptions, std::move(header), ctx.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
} else {
fbthrift_serialize_and_send_{{function:cpp_name}}(*defaultRpcOptions, std::move(header), ctx.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, handle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
fbthrift_serialize_and_send_{{function:cpp_name}}(*defaultRpcOptions, std::move(header), ctx.get(), std::move(wrappedCallback){{#function:creates_interaction?}}, interactionHandle{{/function:creates_interaction?}}{{function:comma}}{{ > service_common/param_list}});
}
if (cancellable) {
folly::CancellationCallback cb(cancelToken, [&] { CancellableCallback::cancel(std::move(cancellableCallback)); });
Expand Down Expand Up @@ -188,10 +188,10 @@
{{/function:creates_interaction?}}
{{#function:creates_interaction?}}
{{^type:void?}}
co_return std::make_pair(std::move(handle), std::move(_return));
co_return std::make_pair(std::move(interactionHandle), std::move(_return));
{{/type:void?}}
{{#type:void?}}
co_return handle;
co_return interactionHandle;
{{/type:void?}}
{{/function:creates_interaction?}}
{{/function:oneway?}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void apache::thrift::Client<::cpp2::GoodService>::fbthrift_send_bar(apache::thri


apache::thrift::Client<::cpp2::GoodService>::BadInteraction apache::thrift::Client<::cpp2::GoodService>::createBadInteraction() {
return BadInteraction(channel_, "BadInteraction");
return BadInteraction(channel_, "BadInteraction", interceptors_);
}
void apache::thrift::Client<::cpp2::GoodService>::bar(std::unique_ptr<apache::thrift::RequestCallback> callback) {
::apache::thrift::RpcOptions rpcOptions;
Expand Down
Loading

0 comments on commit 8e550d7

Please sign in to comment.