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#include <type_traits>
9
10#include <userver/clients/http/config.hpp>
11#include <userver/clients/http/response.hpp>
12#include <userver/compiler/select.hpp>
13#include <userver/engine/deadline.hpp>
14#include <userver/engine/future.hpp>
15#include <userver/engine/impl/context_accessor.hpp>
16#include <userver/utils/impl/source_location.hpp>
17
18USERVER_NAMESPACE_BEGIN
19
20namespace clients::http {
21
22class RequestState;
23
24namespace impl {
25class EasyWrapper;
26} // namespace impl
27
28/// @brief Allows to perform a request concurrently with other work without
29/// creating an extra coroutine for waiting.
30class ResponseFuture final {
31public:
32 ResponseFuture(ResponseFuture&& other) noexcept;
33 ResponseFuture& operator=(ResponseFuture&&) noexcept;
34 ResponseFuture(const ResponseFuture&) = delete;
35 ResponseFuture& operator=(const ResponseFuture&) = delete;
36 ~ResponseFuture();
37
38 /// @brief Cancel the request in flight
39 void Cancel();
40
41 /// @brief Keep executing the request but do not care any more about the result. It is fine to destroy this future
42 /// after Detach(), the request will continue execution.
43 void Detach();
44
45 /// @brief Stops the current task execution until the request finishes
46 /// @throws clients::http::CancelException if the current task is being cancelled
47 /// @returns std::future_status::ready or std::future_status::timeout
48 std::future_status Wait(utils::impl::SourceLocation location = utils::impl::SourceLocation::Current());
49
50 /// @brief Wait for the response and return it
51 std::shared_ptr<Response> Get(utils::impl::SourceLocation location = utils::impl::SourceLocation::Current());
52
53 void SetCancellationPolicy(CancellationPolicy cp);
54
55 /// @cond
56 /// Internal helper for WaitAny/WaitAll
57 engine::impl::ContextAccessor* TryGetContextAccessor() noexcept;
58
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