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