userver: userver/ugrpc/client/response_future.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <userver/ugrpc/client/rpc.hpp>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace ugrpc::client {
11
12template <typename Response>
13class [[nodiscard]] ResponseFuture final {
14public:
15 /// @brief Checks if the asynchronous call has completed
16 /// Note, that once user gets result, IsReady should not be called
17 /// @return true if result ready
18 [[nodiscard]] bool IsReady() const noexcept;
19
20 /// @brief Await response until specified timepoint
21 ///
22 /// @throws ugrpc::client::RpcError on an RPC error
23 [[nodiscard]] engine::FutureStatus WaitUntil(engine::Deadline deadline) const;
24
25 /// @brief Await and read the response
26 ///
27 /// `Get` should not be called multiple times for the same UnaryFuture.
28 ///
29 /// The connection is not closed, it will be reused for new RPCs.
30 ///
31 /// @returns the response on success
32 /// @throws ugrpc::client::RpcError on an RPC error
33 /// @throws ugrpc::client::RpcCancelledError on task cancellation
34 Response Get();
35
36 /// @brief Get original gRPC Call
38
39 /// @cond
40 // For internal use only
41 template <typename PrepareFunc, typename Request>
42 ResponseFuture(impl::CallParams&& params, PrepareFunc prepare_func, const Request& req);
43
44 // For internal use only.
45 engine::impl::ContextAccessor* TryGetContextAccessor() noexcept;
46 /// @endcond
47
48private:
49 impl::UnaryCall<Response> call_;
50 std::unique_ptr<Response> response_;
51 impl::UnaryFuture future_;
52};
53
54template <typename Response>
55template <typename PrepareFunc, typename Request>
56ResponseFuture<Response>::ResponseFuture(impl::CallParams&& params, PrepareFunc prepare_func, const Request& req)
57 : call_(std::move(params), prepare_func, req),
58 response_{std::make_unique<Response>()},
59 future_{call_.FinishAsync(*response_)} {}
60
61template <typename Response>
62bool ResponseFuture<Response>::IsReady() const noexcept {
63 return future_.IsReady();
64}
65
66template <typename Response>
67engine::FutureStatus ResponseFuture<Response>::WaitUntil(engine::Deadline deadline) const {
68 return future_.WaitUntil(deadline);
69}
70
71template <typename Response>
72Response ResponseFuture<Response>::Get() {
73 future_.Get();
74 return std::move(*response_);
75}
76
77template <typename Response>
78CallAnyBase& ResponseFuture<Response>::GetCall() {
79 return call_;
80}
81
82template <typename Response>
83engine::impl::ContextAccessor* ResponseFuture<Response>::TryGetContextAccessor() noexcept {
84 return future_.TryGetContextAccessor();
85}
86
87} // namespace ugrpc::client
88
89USERVER_NAMESPACE_END