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 /// @throws ugrpc::client::RpcSerializationError if the request cannot be serialized
52 Response Get() { return impl_->Get(); }
53
54 /// @brief Cancel call
55 void Cancel() { return impl_->Cancel(); }
56
57 /// @brief Get call context, useful e.g. for accessing metadata.
58 CallContext& GetContext() { return impl_->GetContext(); }
59
60 /// @overload
61 const CallContext& GetContext() const { return impl_->GetContext(); }
62
63 /// Satisfies @ref engine::Awaitable, for use with @ref engine::WaitAnyContext and friends.
64 engine::AwaitableToken GetAwaitableToken() noexcept USERVER_IMPL_LIFETIME_BOUND {
65 return impl_->GetAwaitableToken();
66 }
67
68 /// @cond
69 // For internal use only
70 template <typename Request>
71 ResponseFuture(impl::CallParams&& params, const Request& request)
72 : impl_{std::make_unique<impl::AsyncUnaryCallAdapter<Request, Response>>(std::move(params), request)}
73 {}
74 /// @endcond
75
76private:
77 std::unique_ptr<impl::ResponseFutureImplBase<Response>> impl_;
78};
79
80} // namespace ugrpc::client
81
82USERVER_NAMESPACE_END