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