userver: userver/clients/http/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/clients/http/response_future.hpp
4/// @brief @copybrief clients::http::ResponseFuture
5
6#include <future>
7#include <memory>
8
9#include <userver/clients/http/cancellation_policy.hpp>
10#include <userver/clients/http/response.hpp>
11#include <userver/compiler/impl/lifetime.hpp>
12#include <userver/engine/awaitable.hpp>
13#include <userver/engine/deadline.hpp>
14#include <userver/engine/future.hpp>
15#include <userver/utils/impl/source_location.hpp>
16
17USERVER_NAMESPACE_BEGIN
18
19namespace clients::http {
20
21class RequestState;
22
23namespace impl {
24class EasyWrapper;
25} // namespace impl
26
27/// @brief Allows to perform a request concurrently with other work without
28/// creating an extra coroutine for waiting.
29class ResponseFuture final {
30public:
31 ResponseFuture(ResponseFuture&& other) noexcept;
32 ResponseFuture& operator=(ResponseFuture&&) noexcept;
33 ResponseFuture(const ResponseFuture&) = delete;
34 ResponseFuture& operator=(const ResponseFuture&) = delete;
35 ~ResponseFuture();
36
37 /// @brief Cancel the request in flight
38 void Cancel();
39
40 /// @brief Keep executing the request but do not care any more about the result. It is fine to destroy this future
41 /// after Detach(), the request will continue execution.
42 void Detach();
43
44 /// @brief Stops the current task execution until the request finishes
45 /// @throws clients::http::CancelException if the current task is being cancelled
46 /// @returns std::future_status::ready or std::future_status::timeout
47 std::future_status Wait(utils::impl::SourceLocation location = utils::impl::SourceLocation::Current());
48
49 /// @brief Returns whether the response is already available.
50 bool IsReady() const;
51
52 /// @brief Wait for the response and return it
53 std::shared_ptr<Response> Get(utils::impl::SourceLocation location = utils::impl::SourceLocation::Current());
54
55 /// Satisfies @ref engine::Awaitable, for use with @ref engine::WaitAnyContext and friends.
56 engine::AwaitableToken GetAwaitableToken() noexcept USERVER_IMPL_LIFETIME_BOUND;
57
58 /// @cond
59 ResponseFuture(engine::Future<std::shared_ptr<Response>>&& future, std::shared_ptr<RequestState> request);
60 /// @endcond
61
62private:
63 void CancelOrDetach();
64
65 engine::Future<std::shared_ptr<Response>> future_;
66 engine::Deadline deadline_;
67 std::shared_ptr<RequestState> request_state_;
68 bool was_deadline_propagated_{false};
69 CancellationPolicy cancellation_policy_;
70};
71
72} // namespace clients::http
73
74USERVER_NAMESPACE_END