userver: userver/ugrpc/client/response_future.hpp Source File
Loading...
Searching...
No Matches
response_future.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/ugrpc/client/response_future.hpp
4/// @brief @copybrief ugrpc::client::ResponseFuture
5
6#include <memory>
7
8#include <userver/ugrpc/client/impl/async_unary_call_adapter.hpp>
9
10USERVER_NAMESPACE_BEGIN
11
12namespace ugrpc::client {
13
14/// @brief Controls a single request -> single response RPC.
15///
16/// This class is not thread-safe, it cannot be used from multiple tasks at the same time.
17///
18/// The RPC is cancelled on destruction unless the RPC is already finished. In
19/// that case the connection is not closed (it will be reused for new RPCs), and
20/// the server receives `RpcInterruptedError` immediately.
21template <typename Response>
22class [[nodiscard]] ResponseFuture final {
23 // Implementation note: further RPC controls (lazy Finish, ReadInitialMetadata), if needed in the future,
24 // should be added to UnaryCall, not to ResponseFuture. In that case UnaryCall should be exposed in GetCall.
25public:
26 ResponseFuture(ResponseFuture&&) noexcept = default;
27 ResponseFuture& operator=(ResponseFuture&&) noexcept = default;
28
29 /// @brief Checks if the asynchronous call has completed
30 /// Note, that once user gets result, IsReady should not be called
31 /// @return true if result ready
32 [[nodiscard]] bool IsReady() const { return impl_->IsReady(); }
33
34 /// @brief Await response until specified timepoint
35 ///
36 /// @throws ugrpc::client::RpcError on an RPC error
37 [[nodiscard]] engine::FutureStatus WaitUntil(engine::Deadline deadline) const noexcept {
38 return impl_->WaitUntil(deadline);
39 }
40
41 /// @brief Await and read the response
42 ///
43 /// `Get` should not be called multiple times for the same UnaryFuture.
44 ///
45 /// The connection is not closed, it will be reused for new RPCs.
46 ///
47 /// @returns the response on success
48 /// @throws ugrpc::client::RpcError on an RPC error
49 /// @throws ugrpc::client::RpcCancelledError on task cancellation
50 Response Get() { return impl_->Get(); }
51
52 /// @brief Cancel call
53 void Cancel() { return impl_->Cancel(); }
54
55 /// @brief Get call context, useful e.g. for accessing metadata.
56 CallContext& GetContext() { return impl_->GetContext(); }
57
58 /// @overload
59 const CallContext& GetContext() const { return impl_->GetContext(); }
60
61 /// @cond
62 // For internal use only
63 template <typename Stub, typename Request>
64 ResponseFuture(
65 impl::CallParams&& params,
66 impl::PrepareUnaryCallProxy<Stub, Request, Response>&& prepare_unary_call,
67 const Request& request
68 )
69 : impl_{std::make_unique<impl::AsyncUnaryCallAdapter<Stub, Request, Response>>(
70 std::move(params),
71 std::move(prepare_unary_call),
72 request
73 )} {}
74
75 // For internal use only.
76 engine::impl::ContextAccessor* TryGetContextAccessor() noexcept { return impl_->TryGetContextAccessor(); }
77 /// @endcond
78
79private:
80 std::unique_ptr<impl::ResponseFutureImplBase<Response>> impl_;
81};
82
83} // namespace ugrpc::client
84
85USERVER_NAMESPACE_END