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