Skip to content

Commit e73a3eb

Browse files
committed
refactor engine: advise TaskBuilder usage for complex task settings
* Remove some obscure overloads of `engine::Async` and `utils::Async` variants * If you were using an `Async*` overload that has been removed, then please use @ref utils::TaskBuilder instead * Advise usage of @ref utils::TaskBuilder::HideSpan instead of @ref engine::AsyncNoSpan commit_hash:2578bdc78a802c95481cebba0b179dc7c4209309
1 parent 4661929 commit e73a3eb

24 files changed

Lines changed: 161 additions & 332 deletions

core/include/userver/engine/async.hpp

Lines changed: 12 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -8,43 +8,20 @@
88
#include <userver/engine/task/shared_task_with_result.hpp>
99
#include <userver/engine/task/task_processor_fwd.hpp>
1010
#include <userver/engine/task/task_with_result.hpp>
11-
#include <userver/utils/impl/wrapped_call.hpp>
1211

1312
USERVER_NAMESPACE_BEGIN
1413

1514
namespace engine {
1615

17-
namespace impl {
18-
19-
template <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},
32-
std::forward<Function>(f),
33-
std::forward<Args>(args)...
34-
)};
35-
}
36-
37-
} // namespace impl
38-
3916
/// @brief Runs an asynchronous function call using the specified task processor.
4017
///
4118
/// @warning If any logs are written in the task function (outside any manual tracing::Span scopes),
4219
/// then those logs will have no `span_id` (yikes!).
4320
/// If you create a span there manually, it will be disconnected from the outside trace.
4421
/// **Prefer utils::Async by default instead.**
4522
///
46-
/// @warning To hide a spammy span from traces, use @ref tracing::Span::CurrentSpan plus
47-
/// @ref tracing::Span::SetLogLevel instead.
23+
/// @warning To hide a spammy span from traces, use @ref engine::TaskBuilder
24+
/// with @ref engine::TaskBuilder::HideSpan instead.
4825
/// Logs will then be linked to the nearest span that is written out.
4926
///
5027
/// @warning Some clients may call tracing::Span::CurrentSpan unconditionally, so don't be too surprised
@@ -56,49 +33,12 @@ template <template <typename> typename TaskType, typename Function, typename...
5633
/// * some low-level (e.g. driver or filesystem-IO) code where logs are definitely not written;
5734
/// * breaking traces e.g. this is (rarely) wanted for some background tasks.
5835
///
59-
/// @see utils::Async for main documentation on `Async` function family.
36+
/// @see @ref utils::Async for main documentation on `Async` function family.
37+
/// @see @ref engine::TaskBuilder for more `Async` variants.
6038
template <typename Function, typename... Args>
6139
[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
6240
return impl::MakeTaskWithResult<TaskWithResult>(
63-
task_processor,
64-
Task::Importance::kNormal,
65-
{},
66-
std::forward<Function>(f),
67-
std::forward<Args>(args)...
68-
);
69-
}
70-
71-
/// @overload
72-
template <typename Function, typename... Args>
73-
[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
74-
return impl::MakeTaskWithResult<SharedTaskWithResult>(
75-
task_processor,
76-
Task::Importance::kNormal,
77-
{},
78-
std::forward<Function>(f),
79-
std::forward<Args>(args)...
80-
);
81-
}
82-
83-
/// @overload
84-
template <typename Function, typename... Args>
85-
[[nodiscard]] auto AsyncNoSpan(TaskProcessor& task_processor, Deadline deadline, Function&& f, Args&&... args) {
86-
return impl::MakeTaskWithResult<TaskWithResult>(
87-
task_processor,
88-
Task::Importance::kNormal,
89-
deadline,
90-
std::forward<Function>(f),
91-
std::forward<Args>(args)...
92-
);
93-
}
94-
95-
/// @overload
96-
template <typename Function, typename... Args>
97-
[[nodiscard]] auto SharedAsyncNoSpan(TaskProcessor& task_processor, Deadline deadline, Function&& f, Args&&... args) {
98-
return impl::MakeTaskWithResult<SharedTaskWithResult>(
99-
task_processor,
100-
Task::Importance::kNormal,
101-
deadline,
41+
impl::TaskConfig{.task_processor = &task_processor},
10242
std::forward<Function>(f),
10343
std::forward<Args>(args)...
10444
);
@@ -107,58 +47,19 @@ template <typename Function, typename... Args>
10747
/// @overload
10848
template <typename Function, typename... Args>
10949
[[nodiscard]] auto AsyncNoSpan(Function&& f, Args&&... args) {
110-
return AsyncNoSpan(current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...);
111-
}
112-
113-
/// @overload
114-
template <typename Function, typename... Args>
115-
[[nodiscard]] auto SharedAsyncNoSpan(Function&& f, Args&&... args) {
116-
return SharedAsyncNoSpan(current_task::GetTaskProcessor(), std::forward<Function>(f), std::forward<Args>(args)...);
117-
}
118-
119-
/// @overload
120-
template <typename Function, typename... Args>
121-
[[nodiscard]] auto AsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
122-
return AsyncNoSpan(
123-
current_task::GetTaskProcessor(),
124-
deadline,
125-
std::forward<Function>(f),
126-
std::forward<Args>(args)...
127-
);
128-
}
129-
130-
/// @overload
131-
template <typename Function, typename... Args>
132-
[[nodiscard]] auto SharedAsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
133-
return SharedAsyncNoSpan(
134-
current_task::GetTaskProcessor(),
135-
deadline,
136-
std::forward<Function>(f),
137-
std::forward<Args>(args)...
138-
);
50+
return impl::MakeTaskWithResult<
51+
TaskWithResult>(impl::TaskConfig{}, std::forward<Function>(f), std::forward<Args>(args)...);
13952
}
14053

14154
/// @overload
14255
/// @see Task::Importance::Critical
14356
template <typename Function, typename... Args>
14457
[[nodiscard]] auto CriticalAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
14558
return impl::MakeTaskWithResult<TaskWithResult>(
146-
task_processor,
147-
Task::Importance::kCritical,
148-
{},
149-
std::forward<Function>(f),
150-
std::forward<Args>(args)...
151-
);
152-
}
153-
154-
/// @overload
155-
/// @see Task::Importance::Critical
156-
template <typename Function, typename... Args>
157-
[[nodiscard]] auto SharedCriticalAsyncNoSpan(TaskProcessor& task_processor, Function&& f, Args&&... args) {
158-
return impl::MakeTaskWithResult<SharedTaskWithResult>(
159-
task_processor,
160-
Task::Importance::kCritical,
161-
{},
59+
impl::TaskConfig{
60+
.task_processor = &task_processor,
61+
.importance = Task::Importance::kCritical,
62+
},
16263
std::forward<Function>(f),
16364
std::forward<Args>(args)...
16465
);
@@ -168,32 +69,8 @@ template <typename Function, typename... Args>
16869
/// @see Task::Importance::Critical
16970
template <typename Function, typename... Args>
17071
[[nodiscard]] auto CriticalAsyncNoSpan(Function&& f, Args&&... args) {
171-
return CriticalAsyncNoSpan(
172-
current_task::GetTaskProcessor(),
173-
std::forward<Function>(f),
174-
std::forward<Args>(args)...
175-
);
176-
}
177-
178-
/// @overload
179-
/// @see Task::Importance::Critical
180-
template <typename Function, typename... Args>
181-
[[nodiscard]] auto SharedCriticalAsyncNoSpan(Function&& f, Args&&... args) {
182-
return SharedCriticalAsyncNoSpan(
183-
current_task::GetTaskProcessor(),
184-
std::forward<Function>(f),
185-
std::forward<Args>(args)...
186-
);
187-
}
188-
189-
/// @overload
190-
/// @see Task::Importance::Critical
191-
template <typename Function, typename... Args>
192-
[[nodiscard]] auto CriticalAsyncNoSpan(Deadline deadline, Function&& f, Args&&... args) {
19372
return impl::MakeTaskWithResult<TaskWithResult>(
194-
current_task::GetTaskProcessor(),
195-
Task::Importance::kCritical,
196-
deadline,
73+
impl::TaskConfig{.importance = Task::Importance::kCritical},
19774
std::forward<Function>(f),
19875
std::forward<Args>(args)...
19976
);

core/include/userver/engine/impl/task_context_factory.hpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct TaskConfig final {
2424
engine::TaskProcessor* task_processor{nullptr};
2525
Task::Importance importance{Task::Importance::kNormal};
2626
Task::WaitMode wait_mode{Task::WaitMode::kSingleAwaiter};
27-
engine::Deadline deadline;
27+
engine::Deadline deadline{};
2828
};
2929

3030
[[nodiscard]] TaskContext& PlacementNewTaskContext(
@@ -47,7 +47,7 @@ void DeleteFusedTaskContext(std::byte* storage) noexcept;
4747
// managed by boost::intrusive_ptr<TaskContext> through intrusive_ptr_add_ref
4848
// and intrusive_ptr_release hooks.
4949
template <typename Function, typename... Args>
50-
TaskContextHolder MakeTask(TaskConfig config, Function&& f, Args&&... args) {
50+
[[nodiscard]] TaskContextHolder MakeTask(TaskConfig&& config, Function&& f, Args&&... args) {
5151
using WrappedCallType = utils::impl::WrappedCallImplType<Function, Args...>;
5252

5353
constexpr auto kPayloadSize = sizeof(WrappedCallType);
@@ -76,6 +76,15 @@ TaskContextHolder MakeTask(TaskConfig config, Function&& f, Args&&... args) {
7676
return TaskContextHolder::Adopt(context);
7777
}
7878

79+
template <template <typename> typename TaskType, typename Function, typename... Args>
80+
[[nodiscard]] auto MakeTaskWithResult(TaskConfig&& config, Function&& f, Args&&... args) {
81+
using ResultType = typename utils::impl::WrappedCallImplType<Function, Args...>::ResultType;
82+
constexpr auto kWaitMode = TaskType<ResultType>::kWaitMode;
83+
config.wait_mode = kWaitMode;
84+
85+
return TaskType<ResultType>{MakeTask(std::move(config), std::forward<Function>(f), std::forward<Args>(args)...)};
86+
}
87+
7988
} // namespace engine::impl
8089

8190
USERVER_NAMESPACE_END

0 commit comments

Comments
 (0)