userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
async.hpp
Go to the documentation of this file.
1
#
pragma
once
2
3
/// @file userver/utils/async.hpp
4
/// @brief Utility functions to start asynchronous tasks.
5
6
#
include
<
string
>
7
#
include
<
utility
>
8
9
#
include
<
userver
/
engine
/
impl
/
task_context_factory
.
hpp
>
10
#
include
<
userver
/
engine
/
task
/
shared_task_with_result
.
hpp
>
11
#
include
<
userver
/
engine
/
task
/
task_with_result
.
hpp
>
12
#
include
<
userver
/
utils
/
impl
/
source_location
.
hpp
>
13
#
include
<
userver
/
utils
/
impl
/
span_wrap_call
.
hpp
>
14
15
USERVER_NAMESPACE_BEGIN
16
17
namespace
utils
{
18
19
/// @ingroup userver_concurrency
20
///
21
/// @brief Starts an asynchronous task.
22
///
23
/// By default, arguments are copied or moved inside the resulting
24
/// `TaskWithResult`, like `std::thread` does. To pass an argument by reference,
25
/// wrap it in `std::ref / std::cref` or capture the arguments using a lambda.
26
///
27
/// For more documentation on launching asynchronous tasks:
28
///
29
/// @see @ref intro_tasks
30
///
31
/// ## About this specific overload
32
///
33
/// This is the overload that should be used by default.
34
///
35
/// * The task will be launched on the current TaskProcessor.
36
/// * Only 1 task may call `Wait` or `Get` on this task.
37
/// * The task may be cancelled before the function starts execution
38
/// in case of TaskProcessor overload. Also, if the task is cancelled for any
39
/// reason before the function starts execution, it will not run at all.
40
/// * The task will create a child tracing::Span with the specified name
41
/// * The task will inherit all engine::TaskInheritedVariable instances
42
/// from the current task.
43
///
44
/// For details on the various other overloads:
45
/// @see @ref flavors_of_async
46
///
47
/// For maximum customization of task parameters, see @ref utils::TaskBuilder.
48
///
49
/// @param name Name of the task to show in logs
50
/// @param f Function to execute asynchronously
51
/// @param args Arguments to pass to the function
52
/// @returns engine::TaskWithResult
53
template
<
typename
Function,
typename
... Args>
54
[[nodiscard]]
auto
Async
(std::string name, Function&& f, Args&&... args) {
55
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
56
engine::
impl
::TaskConfig{},
57
utils::impl::SpanLazyPrvalue(std::move(name)),
58
std::forward<Function>(f),
59
std::forward<Args>(args)...
60
);
61
}
62
63
/// @overload
64
/// @ingroup userver_concurrency
65
///
66
/// Task execution may be cancelled before the function starts execution
67
/// in case of TaskProcessor overload.
68
///
69
/// @param task_processor Task processor to run on
70
/// @param name Name of the task to show in logs
71
/// @param f Function to execute asynchronously
72
/// @param args Arguments to pass to the function
73
/// @returns engine::TaskWithResult
74
template
<
typename
Function,
typename
... Args>
75
[[nodiscard]]
auto
Async
(engine::TaskProcessor& task_processor, std::string name, Function&& f, Args&&... args) {
76
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
77
engine::
impl
::TaskConfig{
78
.task_processor = &task_processor,
79
},
80
utils::impl::SpanLazyPrvalue(std::move(name)),
81
std::forward<Function>(f),
82
std::forward<Args>(args)...
83
);
84
}
85
86
/// @overload
87
/// @ingroup userver_concurrency
88
///
89
/// Starts an asynchronous task with a hidden tracing::Span. The span has
90
/// @ref logging::Level::kNone log level, while logs within the span remain
91
/// linked to the nearest visible span.
92
///
93
/// Task execution may be cancelled before the function starts execution
94
/// in case of TaskProcessor overload.
95
///
96
/// @param f Function to execute asynchronously
97
/// @param args Arguments to pass to the function
98
/// @returns engine::TaskWithResult
99
template
<
typename
Function,
typename
... Args>
100
[[nodiscard]]
auto
AsyncHideSpan
(Function&& f, Args&&... args) {
101
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
102
engine::
impl
::TaskConfig{},
103
utils::impl::SpanLazyPrvalue(std::string{},
utils
::impl::SpanWrapCall::HideSpan::kYes),
104
std::forward<Function>(f),
105
std::forward<Args>(args)...
106
);
107
}
108
109
/// @overload
110
/// @ingroup userver_concurrency
111
///
112
/// Starts an asynchronous task with a hidden tracing::Span. The span has
113
/// @ref logging::Level::kNone log level, while logs within the span remain
114
/// linked to the nearest visible span.
115
///
116
/// Task execution may be cancelled before the function starts execution
117
/// in case of TaskProcessor overload.
118
///
119
/// @param task_processor Task processor to run on
120
/// @param f Function to execute asynchronously
121
/// @param args Arguments to pass to the function
122
/// @returns engine::TaskWithResult
123
template
<
typename
Function,
typename
... Args>
124
[[nodiscard]]
auto
AsyncHideSpan
(engine::TaskProcessor& task_processor, Function&& f, Args&&... args) {
125
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
126
engine::
impl
::TaskConfig{
127
.task_processor = &task_processor,
128
},
129
utils::impl::SpanLazyPrvalue(std::string{},
utils
::impl::SpanWrapCall::HideSpan::kYes),
130
std::forward<Function>(f),
131
std::forward<Args>(args)...
132
);
133
}
134
135
/// @overload
136
/// @ingroup userver_concurrency
137
///
138
/// Execution of function is guaranteed to start regardless
139
/// of engine::TaskProcessor load limits. Prefer utils::Async by default.
140
///
141
/// @param task_processor Task processor to run on
142
/// @param name Name for the tracing::Span to use with this task
143
/// @param f Function to execute asynchronously
144
/// @param args Arguments to pass to the function
145
/// @returns engine::TaskWithResult
146
template
<
typename
Function,
typename
... Args>
147
[[nodiscard]]
auto
CriticalAsync
(
148
engine::TaskProcessor& task_processor,
149
std::string name,
150
Function&& f,
151
Args&&... args
152
) {
153
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
154
engine::
impl
::TaskConfig{
155
.task_processor = &task_processor,
156
.importance = engine
::
Task
::
Importance
::
kCritical
,
157
},
158
utils::impl::SpanLazyPrvalue(std::move(name)),
159
std::forward<Function>(f),
160
std::forward<Args>(args)...
161
);
162
}
163
164
/// @overload
165
/// @ingroup userver_concurrency
166
///
167
/// Task execution may be cancelled before the function starts execution
168
/// in case of TaskProcessor overload.
169
///
170
/// @param task_processor Task processor to run on
171
/// @param name Name of the task to show in logs
172
/// @param f Function to execute asynchronously
173
/// @param args Arguments to pass to the function
174
/// @returns engine::SharedTaskWithResult
175
template
<
typename
Function,
typename
... Args>
176
[[nodiscard]]
auto
SharedAsync
(engine::TaskProcessor& task_processor, std::string name, Function&& f, Args&&... args) {
177
return
engine::
impl
::MakeTaskWithResult<engine::
SharedTaskWithResult
>(
178
engine::
impl
::TaskConfig{
179
.task_processor = &task_processor,
180
},
181
utils::impl::SpanLazyPrvalue(std::move(name)),
182
std::forward<Function>(f),
183
std::forward<Args>(args)...
184
);
185
}
186
187
/// @overload
188
/// @ingroup userver_concurrency
189
///
190
/// Execution of function is guaranteed to start regardless
191
/// of engine::TaskProcessor load limits. Prefer utils::Async by default.
192
///
193
/// @param name Name for the tracing::Span to use with this task
194
/// @param f Function to execute asynchronously
195
/// @param args Arguments to pass to the function
196
/// @returns engine::TaskWithResult
197
template
<
typename
Function,
typename
... Args>
198
[[nodiscard]]
auto
CriticalAsync
(std::string name, Function&& f, Args&&... args) {
199
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
200
engine::
impl
::TaskConfig{
201
.importance = engine
::
Task
::
Importance
::
kCritical
,
202
},
203
utils::impl::SpanLazyPrvalue(std::move(name)),
204
std::forward<Function>(f),
205
std::forward<Args>(args)...
206
);
207
}
208
209
/// @overload
210
/// @ingroup userver_concurrency
211
///
212
/// Execution of function is guaranteed to start regardless
213
/// of engine::TaskProcessor load limits. Prefer utils::SharedAsync by default.
214
///
215
/// @param name Name for the tracing::Span to use with this task
216
/// @param f Function to execute asynchronously
217
/// @param args Arguments to pass to the function
218
/// @returns engine::SharedTaskWithResult
219
template
<
typename
Function,
typename
... Args>
220
[[nodiscard]]
auto
SharedCriticalAsync
(std::string name, Function&& f, Args&&... args) {
221
return
engine::
impl
::MakeTaskWithResult<engine::
SharedTaskWithResult
>(
222
engine::
impl
::TaskConfig{
223
.importance = engine
::
Task
::
Importance
::
kCritical
,
224
},
225
utils::impl::SpanLazyPrvalue(std::move(name)),
226
std::forward<Function>(f),
227
std::forward<Args>(args)...
228
);
229
}
230
231
/// @overload
232
/// @ingroup userver_concurrency
233
///
234
/// Task execution may be cancelled before the function starts execution
235
/// in case of TaskProcessor overload.
236
///
237
/// @param name Name of the task to show in logs
238
/// @param f Function to execute asynchronously
239
/// @param args Arguments to pass to the function
240
/// @returns engine::SharedTaskWithResult
241
template
<
typename
Function,
typename
... Args>
242
[[nodiscard]]
auto
SharedAsync
(std::string name, Function&& f, Args&&... args) {
243
return
engine::
impl
::MakeTaskWithResult<engine::
SharedTaskWithResult
>(
244
engine::
impl
::TaskConfig{},
245
utils::impl::SpanLazyPrvalue(std::move(name)),
246
std::forward<Function>(f),
247
std::forward<Args>(args)...
248
);
249
}
250
251
/// @overload
252
/// @ingroup userver_concurrency
253
///
254
/// Task execution may be cancelled before the function starts execution
255
/// in case of TaskProcessor overload.
256
///
257
/// @param name Name of the task to show in logs
258
/// @param deadline Deadline to set for the child task, upon reaching it the task will be cancelled
259
/// @param f Function to execute asynchronously
260
/// @param args Arguments to pass to the function
261
/// @returns engine::TaskWithResult
262
template
<
typename
Function,
typename
... Args>
263
[[nodiscard]]
auto
Async
(std::string name, engine::Deadline deadline, Function&& f, Args&&... args) {
264
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
265
engine::
impl
::TaskConfig{
266
.deadline = deadline,
267
},
268
utils::impl::SpanLazyPrvalue(std::move(name)),
269
std::forward<Function>(f),
270
std::forward<Args>(args)...
271
);
272
}
273
274
/// @ingroup userver_concurrency
275
///
276
/// Starts an asynchronous task that inherits tracing::Span and only the
277
/// engine::TaskInheritedVariable instances with
278
/// engine::TaskInheritedVariablePriority::kBackground, including baggage::Baggage
279
/// and OpenTelemetry tracing headers. Task execution may be cancelled before the
280
/// function starts execution in case of engine::TaskProcessor overload.
281
///
282
/// Typically used from a request handler to launch tasks that outlive the
283
/// request and do not effect its completion.
284
///
285
/// ## Usage example
286
/// Suppose you have some component that runs asynchronous tasks:
287
/// @snippet core/src/utils/async_test.cpp AsyncBackground component
288
/// @snippet core/src/utils/async_test.cpp AsyncBackground handler
289
///
290
/// If the tasks logically belong to the component itself (not to the method
291
/// caller), then they should be launched using utils::AsyncBackground instead
292
/// of the regular utils::Async
293
/// @snippet core/src/utils/async_test.cpp AsyncBackground FooAsync
294
///
295
/// ## Arguments
296
/// By default, arguments are copied or moved inside the resulting
297
/// `TaskWithResult`, like `std::thread` does. To pass an argument by reference,
298
/// wrap it in `std::ref / std::cref` or capture the arguments using a lambda.
299
///
300
/// @param name Name of the task to show in logs
301
/// @param task_processor Task processor to run on
302
/// @param f Function to execute asynchronously
303
/// @param args Arguments to pass to the function
304
/// @returns engine::TaskWithResult
305
template
<
typename
Function,
typename
... Args>
306
[[nodiscard]]
auto
AsyncBackground
(
307
std::string name,
308
engine::TaskProcessor& task_processor,
309
Function&& f,
310
Args&&... args
311
) {
312
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
313
engine::
impl
::TaskConfig{
314
.task_processor = &task_processor,
315
.inherited_variables_priority = engine
::
TaskInheritedVariablePriority
::
kBackground
,
316
},
317
utils::impl::SpanLazyPrvalue(std::move(name)),
318
std::forward<Function>(f),
319
std::forward<Args>(args)...
320
);
321
}
322
323
/// @overload
324
/// @ingroup userver_concurrency
325
///
326
/// Execution of function is guaranteed to start regardless
327
/// of engine::TaskProcessor load limits. Use for background tasks for which
328
/// failing to start not just breaks handling of a single request, but harms
329
/// the whole service instance.
330
///
331
/// @param name Name of the task to show in logs
332
/// @param task_processor Task processor to run on
333
/// @param f Function to execute asynchronously
334
/// @param args Arguments to pass to the function
335
/// @returns engine::TaskWithResult
336
template
<
typename
Function,
typename
... Args>
337
[[nodiscard]]
auto
CriticalAsyncBackground
(
338
std::string name,
339
engine::TaskProcessor& task_processor,
340
Function&& f,
341
Args&&... args
342
) {
343
return
engine::
impl
::MakeTaskWithResult<engine::
TaskWithResult
>(
344
engine::
impl
::TaskConfig{
345
.task_processor = &task_processor,
346
.importance = engine
::
Task
::
Importance
::
kCritical
,
347
.inherited_variables_priority = engine
::
TaskInheritedVariablePriority
::
kBackground
,
348
},
349
utils::impl::SpanLazyPrvalue(std::move(name)),
350
std::forward<Function>(f),
351
std::forward<Args>(args)...
352
);
353
}
354
355
}
// namespace utils
356
357
USERVER_NAMESPACE_END
userver
utils
async.hpp
Generated on
for userver by
Doxygen
1.17.0