userver: userver/engine/async.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
async.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/engine/async.hpp
4/// @brief Low-level TaskWithResult creation helpers
5
6#include <userver/engine/deadline.hpp>
7#include <userver/engine/impl/task_context_factory.hpp>
8#include <userver/engine/task/shared_task_with_result.hpp>
9#include <userver/engine/task/task_processor_fwd.hpp>
10#include <userver/engine/task/task_with_result.hpp>
11#include <userver/utils/impl/wrapped_call.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace engine {
16
17namespace impl {
18
19template <template <typename> typename TaskType, typename Function, typename... Args>
20[[nodiscard]] auto MakeTaskWithResult(
21 TaskProcessor& task_processor,
22 Task::Importance importance,
23 Deadline deadline,
24 Function&& f,
25 Args&&... args
26) {
27 using ResultType = typename utils::impl::WrappedCallImplType<Function, Args...>::ResultType;
28 constexpr auto kWaitMode = TaskType<ResultType>::kWaitMode;
29
30 return TaskType<ResultType>{MakeTask(
31 {task_processor, importance, kWaitMode, deadline}, std::forward<Function>(f), std::forward<Args>(args)...
32 )};
33}
34
35} // namespace impl
36
37/// @brief Runs an asynchronous function call using the specified task processor.
38///
39/// @warning If any logs are written in the task function (outside any manual tracing::Span scopes),
40/// then those logs will have no `span_id` (yikes!). **Prefer utils::Async by default instead.**
41///
42/// Also, some clients may call tracing::Span::CurrentSpan unconditionally, so don't be too surprised
43/// if they won't work without a span scope. Do write tests.
44///
45/// `AsyncNoSpan` is useful for implementing periodic tasks (like utils::PeriodicTask), task pools,
46/// and some low-level (e.g. driver) code where logs are definitely not written, or where `span_id` is useless.
47///
48/// @see utils::Async for main documentation on `Async` function family.
49template <typename Function, typename... Args>
50[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
51 return impl::MakeTaskWithResult<TaskWithResult>(
52 task_processor, Task::Importance::kNormal, {}, std::forward<Function>(f), std::forward<Args>(args)...
53 );
54}
55
56/// @overload
57template <typename Function, typename... Args>
58[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
59 return impl::MakeTaskWithResult<SharedTaskWithResult>(
60 task_processor, Task::Importance::kNormal, {}, std::forward<Function>(f), std::forward<Args>(args)...
61 );
62}
63
64/// @overload
65template <typename Function, typename... Args>
66[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Deadline deadline, Function&& f, Args&&... args) {
67 return impl::MakeTaskWithResult<TaskWithResult>(
68 task_processor, Task::Importance::kNormal, deadline, std::forward<Function>(f), std::forward<Args>(args)...
69 );
70}
71
72/// @overload
73template <typename Function, typename... Args>
74[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor, Deadline deadline, Function&& f, Args&&... args) {
75 return impl::MakeTaskWithResult<SharedTaskWithResult>(
76 task_processor, Task::Importance::kNormal, deadline, std::forward<Function>(f), std::forward<Args>(args)...
77 );
78}
79
80/// @overload
81template <typename Function, typename... Args>
82[[nodiscard]] auto AsyncNoSpan(Function&& f, Args&&... args) {
83 return AsyncNoSpan(current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...);
84}
85
86/// @overload
87template <typename Function, typename... Args>
88[[nodiscard]] auto SharedAsyncNoSpan(Function&& f, Args&&... args) {
89 return SharedAsyncNoSpan(current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...);
90}
91
92/// @overload
93template <typename Function, typename... Args>
94[[nodiscard]] auto AsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
95 return AsyncNoSpan(
96 current_task::GetTaskProcessor(), deadline, std::forward<Function>(f), std::forward<Args>(args)...
97 );
98}
99
100/// @overload
101template <typename Function, typename... Args>
102[[nodiscard]] auto SharedAsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
103 return SharedAsyncNoSpan(
104 current_task::GetTaskProcessor(), deadline, std::forward<Function>(f), std::forward<Args>(args)...
105 );
106}
107
108/// @overload
109/// @see Task::Importance::Critical
110template <typename Function, typename... Args>
111[[nodiscard]] auto CriticalAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
112 return impl::MakeTaskWithResult<TaskWithResult>(
113 task_processor, Task::Importance::kCritical, {}, std::forward<Function>(f), std::forward<Args>(args)...
114 );
115}
116
117/// @overload
118/// @see Task::Importance::Critical
119template <typename Function, typename... Args>
120[[nodiscard]] auto SharedCriticalAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
121 return impl::MakeTaskWithResult<SharedTaskWithResult>(
122 task_processor, Task::Importance::kCritical, {}, std::forward<Function>(f), std::forward<Args>(args)...
123 );
124}
125
126/// @overload
127/// @see Task::Importance::Critical
128template <typename Function, typename... Args>
129[[nodiscard]] auto CriticalAsyncNoSpan(Function&& f, Args&&... args) {
130 return CriticalAsyncNoSpan(
131 current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...
132 );
133}
134
135/// @overload
136/// @see Task::Importance::Critical
137template <typename Function, typename... Args>
138[[nodiscard]] auto SharedCriticalAsyncNoSpan(Function&& f, Args&&... args) {
139 return SharedCriticalAsyncNoSpan(
140 current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...
141 );
142}
143
144/// @overload
145/// @see Task::Importance::Critical
146template <typename Function, typename... Args>
147[[nodiscard]] auto CriticalAsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
148 return impl::MakeTaskWithResult<TaskWithResult>(
151 deadline,
152 std::forward<Function>(f),
153 std::forward<Args>(args)...
154 );
155}
156
157} // namespace engine
158
159USERVER_NAMESPACE_END